Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementation of whitelist/blacklist in client, general fixes/tidy up

  • Loading branch information...
commit fffc023513ceab9060c36142e384d64cbc65c79e 1 parent a85a633
@AdaTheDev AdaTheDev authored
View
5 AutomatedTester.BrowserMob/AutomatedTester.BrowserMob.csproj
@@ -10,8 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AutomatedTester.BrowserMob</RootNamespace>
<AssemblyName>AutomatedTester.BrowserMob</AssemblyName>
- <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
+ <TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -36,9 +37,9 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
+ <Reference Include="System.Web" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
- <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
View
76 AutomatedTester.BrowserMob/Client.cs
@@ -3,6 +3,7 @@
using System.IO;
using System.Net;
using System.Text;
+using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -13,19 +14,16 @@ public class Client
private readonly string _url;
private readonly Int16 _port;
private readonly string _proxy;
+ private readonly string _baseUrlProxy;
- /// <summary>
- ///
- /// </summary>
- /// <param name="url"></param>
public Client(string url)
{
- if (String.IsNullOrWhiteSpace(url))
+ if (String.IsNullOrEmpty(url))
throw new ArgumentException("url not supplied", "url");
_url = url;
-
- using (var response = MakeRequest(String.Format("{0}/proxy", url), "POST"))
+ _baseUrlProxy = String.Format("{0}/proxy", _url);
+ using (var response = MakeRequest(_baseUrlProxy, "POST"))
{
var responseStream = response.GetResponseStream();
if (responseStream == null)
@@ -46,14 +44,10 @@ public Client(string url)
var parts = url.Split(':');
_proxy = parts[1].TrimStart('/') + ":" + _port;
}
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="reference"></param>
+
public void NewHar(string reference = null)
{
- MakeRequest(String.Format("{0}/proxy/{1}/har", _url, _port), "PUT", reference);
+ MakeRequest(String.Format("{0}/{1}/har", _baseUrlProxy, _port), "PUT", reference);
}
private static WebResponse MakeRequest(string url, string method, string reference = null)
@@ -68,44 +62,36 @@ private static WebResponse MakeRequest(string url, string method, string referen
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}
+
+ request.ContentType = "application/x-www-form-urlencoded";
}
else
request.ContentLength = 0;
-
+
return request.GetResponse();
}
- /// <summary>
- ///
- /// </summary>
- /// <param name="reference"></param>
public void NewPage(string reference)
- {
- MakeRequest(String.Format("{0}/proxy/{1}/har/pageRef", _url, _port), "PUT", reference);
+ {
+ MakeRequest(String.Format("{0}/{1}/har/pageRef", _baseUrlProxy, _port), "PUT", reference);
}
- /// <summary>
- ///
- /// </summary>
- public string Har
+ public string GetHar()
{
- get
+ var response = MakeRequest(String.Format("{0}/{1}/har", _baseUrlProxy, _port), "GET");
+ using (var responseStream = response.GetResponseStream())
{
- var response = MakeRequest(String.Format("{0}/proxy/{1}/har", _url, _port), "GET");
- using (var responseStream = response.GetResponseStream())
- {
- if (responseStream == null)
- return null;
+ if (responseStream == null)
+ return null;
- using (var responseStreamReader = new StreamReader(responseStream))
- {
- return responseStreamReader.ReadToEnd();
- }
+ using (var responseStreamReader = new StreamReader(responseStream))
+ {
+ return responseStreamReader.ReadToEnd();
}
}
}
- public void Limits(Dictionary<String, int> options)
+ public void Limits(Dictionary<string, int> options)
{
}
@@ -113,23 +99,31 @@ public void Limits(Dictionary<String, int> options)
public string SeleniumProxy
{
get { return _proxy; }
- }
+ }
- public void WhiteList(String regexp, int statusCode)
+ public void WhiteList(string regexp, int statusCode)
{
-
+ string data = FormatBlackOrWhiteListFormData(regexp, statusCode);
+ MakeRequest(String.Format("{0}/{1}/whitelist", _baseUrlProxy, _port), "PUT", data);
}
- public void Blacklist(String regexp, int statusCode)
- {
+ public void Blacklist(string regexp, int statusCode)
+ {
+ string data = FormatBlackOrWhiteListFormData(regexp, statusCode);
+ MakeRequest(String.Format("{0}/{1}/blacklist", _baseUrlProxy, _port), "PUT", data);
}
+ private static string FormatBlackOrWhiteListFormData(string regexp, int statusCode)
+ {
+ return String.Format("regex={0}&status={1}", HttpUtility.UrlEncode(regexp), statusCode);
+ }
+
/// <summary>
/// shuts down the proxy and closes the port
/// </summary>
public void Close()
{
- MakeRequest(String.Format("{0}/proxy/{1}", _url, _port), "DELETE");
+ MakeRequest(String.Format("{0}/{1}", _baseUrlProxy, _port), "DELETE");
}
}
View
80 AutomatedTester.BrowserMob/Server.cs
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using System.Globalization;
-using System.Net;
using System.Net.Sockets;
using System.Threading;
@@ -9,38 +8,36 @@ namespace AutomatedTester.BrowserMob
{
public class Server
{
- private Process _server;
+ private Process _serverProcess;
private readonly int _port;
private readonly String _path = string.Empty;
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="path"></param>
- /// <param name="port"></param>
- public Server(String path, int port = 8080)
+ private const string Host = "localhost";
+
+ public Server(string path) : this(path, 8080)
+ {}
+
+ public Server(string path, int port)
{
_path = path;
_port = port;
}
- /// <summary>
- ///
- /// </summary>
public void Start()
{
- Process builder = new Process();
- builder.StartInfo.FileName = _path;
+ _serverProcess = new Process
+ {
+ StartInfo = {FileName = _path}
+ };
if (_port != 0)
{
- builder.StartInfo.Arguments = String.Format("--port={0}", _port);
+ _serverProcess.StartInfo.Arguments = String.Format("--port={0}", _port);
}
- _server = builder;
+
try
{
- _server.Start();
+ _serverProcess.Start();
int count = 0;
- while (!IsListening)
+ while (!IsListening())
{
Thread.Sleep(1000);
count++;
@@ -50,22 +47,25 @@ public void Start()
}
}
}
- finally
+ catch
{
- builder.Dispose();
- }
+ _serverProcess.Dispose();
+ _serverProcess = null;
+ throw;
+ }
}
/// <summary>
///
/// </summary>
public void Stop()
- {
- if (!_server.HasExited)
+ {
+ if (_serverProcess != null && !_serverProcess.HasExited)
{
- _server.Kill();
- }
- _server.Dispose();
+ _serverProcess.CloseMainWindow();
+ _serverProcess.Dispose();
+ _serverProcess = null;
+ }
}
/// <summary>
@@ -79,29 +79,27 @@ public void Stop()
/// <summary>
///
/// </summary>
- public String Url
+ public string Url
{
- get { return String.Format("http://localhost:{0}", _port.ToString(CultureInfo.InvariantCulture)); }
+ get { return String.Format("http://{0}:{1}", Host, _port.ToString(CultureInfo.InvariantCulture)); }
}
/// <summary>
///
/// </summary>
- private bool IsListening
+ private bool IsListening()
{
- get {
- try
- {
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socket.Connect(IPAddress.Parse("127.0.0.1"), _port);
- socket.Close();
- return true;
- }
- catch (Exception)
- {
- return false;
- }
+ try
+ {
+ var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ socket.Connect(Host, _port);
+ socket.Close();
+ return true;
}
+ catch (Exception)
+ {
+ return false;
+ }
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.