private void RunRequestLineTest(string expectedValue) { WebRequestClient wrClient = new WebRequestClient(); TrafficViewerFile dataStore = new TrafficViewerFile(); TrafficViewerFile mockSite = new TrafficViewerFile(); MockProxy mockProxy = new MockProxy(dataStore, mockSite); mockProxy.Start(); HttpRequestInfo expectedRequest = new HttpRequestInfo(expectedValue); expectedRequest.Host = mockProxy.Host; expectedRequest.Port = mockProxy.Port; //set the webrequest to use a proxy HttpResponseInfo respInfo = wrClient.SendRequest(expectedRequest); mockProxy.Stop(); if (!expectedRequest.IsConnect) { Assert.AreEqual(1, dataStore.RequestCount); byte[] receivedReqBytes = dataStore.LoadRequestData(0); HttpRequestInfo receivedRequest = new HttpRequestInfo(receivedReqBytes); Assert.AreEqual(expectedValue, receivedRequest.RequestLine); } else { Assert.AreEqual("HTTP/1.1 200 Connection established", respInfo.StatusLine); } }
public void Test_HTTP_ReusingClient() { string testRequest1 = "GET http://site.com/ HTTP/1.1\r\n\r\n"; string testResponse1 = "HTTP/1.1 302 Redirect\r\nLocation: http://site.com/a\r\n\r\n"; string testRequest2 = "GET http://site.com/a HTTP/1.1\r\n\r\n"; string testResponse2 = "HTTP/1.1 200 OK\r\n\r\n"; WebRequestClient client = new WebRequestClient(); TrafficViewerFile mockSite = new TrafficViewerFile(); mockSite.AddRequestResponse(testRequest1, testResponse1); mockSite.AddRequestResponse(testRequest2, testResponse2); TrafficViewerFile dataStore = new TrafficViewerFile(); MockProxy mockProxy = new MockProxy(dataStore, mockSite); mockProxy.Start(); client.SetProxySettings(mockProxy.Host, mockProxy.Port, null); CheckResponseStatus(testRequest1, client, 302); CheckResponseStatus(testRequest2, client, 200); mockProxy.Stop(); }
protected void SendTestRequestThroughMockProxy(HttpRequestInfo testRequest, HttpResponseInfo testResponse, out HttpRequestInfo receivedRequest, out HttpResponseInfo receivedResponse, ClientType clientType = ClientType.WebRequestClient, int proxyPort = 0) { TrafficViewerFile mockSite = new TrafficViewerFile(); mockSite.AddRequestResponse(testRequest.ToArray(true), testResponse.ToArray()); TrafficViewerFile dataStore = new TrafficViewerFile(); MockProxy mockProxy = new MockProxy(dataStore, mockSite, "127.0.0.1", proxyPort, 0); mockProxy.Start(); IHttpClient client = GetHttpClient(mockProxy.Port); receivedResponse = client.SendRequest(testRequest); // check what was received in the proxy byte [] receivedRequestBytes = dataStore.LoadRequestData(0); if (receivedRequestBytes == null) { receivedRequest = null; } else { receivedRequest = new HttpRequestInfo(receivedRequestBytes); } mockProxy.Stop(); }
protected void SendTestRequestToMockProxy(HttpRequestInfo testRequest, HttpResponseInfo testResponse, out HttpRequestInfo receivedRequest, out HttpResponseInfo receivedResponse, int proxyPort = 0) { TrafficViewerFile mockSite = new TrafficViewerFile(); TrafficViewerFile dataStore = new TrafficViewerFile(); MockProxy mockProxy = new MockProxy(dataStore, mockSite, "127.0.0.1", proxyPort, 0); mockProxy.Start(); //change the requests host and port to be the ones of the mock proxy testRequest.Host = mockProxy.Host; testRequest.Port = mockProxy.Port; mockSite.AddRequestResponse(testRequest.ToArray(false), testResponse.ToArray()); IHttpClient client = GetHttpClient(); receivedResponse = client.SendRequest(testRequest); // check what was received in the proxy byte[] receivedRequestBytes = dataStore.LoadRequestData(0); if (receivedRequestBytes == null) { receivedRequest = null; } else { receivedRequest = new HttpRequestInfo(receivedRequestBytes); } mockProxy.Stop(); }
public void TestDataStoreHasRequestAndResponse() { TrafficViewerFile dataStore = new TrafficViewerFile(); TrafficViewerFile mockSite = new TrafficViewerFile(); string testRequest = "GET http://site.com/a HTTP/1.1\r\n"; string expectedResponseLine = "HTTP/1.1 200 OK\r\n\r\n<body>"; mockSite.AddRequestResponse(testRequest, expectedResponseLine); MockProxy proxy = new MockProxy(dataStore, mockSite); proxy.Start(); IHttpClient httpClient = GetHttpClient(proxy.Port); HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest); httpClient.SendRequest(testRequestInfo); byte[] testRequestBytes = dataStore.LoadRequestData(0); byte[] testResponseBytes = dataStore.LoadResponseData(0); HttpRequestInfo reqInfo = new HttpRequestInfo(testRequestBytes); Assert.AreEqual(testRequestInfo.FullUrl, reqInfo.FullUrl); HttpResponseInfo respInfo = new HttpResponseInfo(testResponseBytes); Assert.AreEqual(200, respInfo.Status); Assert.AreEqual("<body>", respInfo.ResponseBody.ToString()); proxy.Stop(); }
public void TestPOSTRequestToProxy() { TrafficViewerFile dataStore = new TrafficViewerFile(); TrafficViewerFile mockSite = new TrafficViewerFile(); string expectedResponseLine = "HTTP/1.1 200 OK"; mockSite.AddRequestResponse(Resources.POSTRequest, expectedResponseLine); MockProxy proxy = new MockProxy(dataStore, mockSite); proxy.Start(); IHttpClient httpClient = GetHttpClient(proxy.Port); HttpRequestInfo testRequestInfo = new HttpRequestInfo(Resources.POSTRequest); HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo); Assert.AreEqual(200, respInfo.Status); HttpRequestInfo storedRequestInfo = new HttpRequestInfo(mockSite.LoadRequestData(0)); Assert.AreEqual("uid=jsmith&passwd=Demo1234", storedRequestInfo.ContentDataString); proxy.Stop(); }
public void TestProxyStart() { MockProxy proxy = new MockProxy(null, null, "", 8888, 18888); proxy.Start(); Assert.AreEqual(8888, proxy.Port); Assert.AreEqual(18888, proxy.SecurePort); proxy.Stop(); }
private static MockProxy SetupMockProxy(string testRequest, string testResponse, TrafficViewerFile dataStore) { MockProxy mockProxy; TrafficViewerFile mockSite = new TrafficViewerFile(); mockSite.AddRequestResponse(testRequest, testResponse); mockProxy = new MockProxy(dataStore, mockSite); return(mockProxy); }
public void TestRemovingCachedHeaders() { //setup a mock web server TrafficViewerFile serverdataStore = new TrafficViewerFile(); serverdataStore.Profile.SetExclusions(new string[0] { }); TrafficViewerFile mockSiteData = new TrafficViewerFile(); string testRequest = "GET /a HTTP/1.1\r\nIf-Modified-Since: 10-10-2012\r\nIf-None-Match: 123\r\nProxy-Connection: keep-alive\r\nAccept-Encoding: gzip\r\n\r\n"; string testResponse = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n"; mockSiteData.AddRequestResponse(testRequest, testResponse); MockProxy mockServer = new MockProxy(serverdataStore, mockSiteData); mockServer.Start(); //setup a mock proxy TrafficViewerFile proxyDataStore = new TrafficViewerFile(); proxyDataStore.Profile.SetExclusions(new string[1] { @".*\.gif" }); ManualExploreProxy meProxy = new ManualExploreProxy("127.0.0.1", 17777, proxyDataStore); meProxy.Start(); IHttpClient httpClient = GetHttpClient(ClientType.TrafficViewerHttpClient, meProxy.Port); //need to use the traffic viewer client here //the webrequestclient does not allow requests to localhost through a proxy on localhost HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest); testRequestInfo.Host = mockServer.Host; testRequestInfo.Port = mockServer.Port; httpClient.SendRequest(testRequestInfo); HttpRequestInfo savedReqInfo = new HttpRequestInfo(serverdataStore.LoadRequestData(0)); Assert.IsNull(savedReqInfo.Headers["If-Modified-Since"]); Assert.IsNull(savedReqInfo.Headers["If-None-Match"]); Assert.IsNull(savedReqInfo.Headers["Accept-Encoding"]); Assert.IsNull(savedReqInfo.Headers["Proxy-Connection"]); meProxy.Stop(); mockServer.Stop(); }
public void Test_Proxy_ConnectionCloseResponseHeader() { string testRequest = "GET http://site.com/ HTTP/1.1\r\n\r\n"; MockProxy proxy = new MockProxy(testRequest, "HTTP/1.1 200 OK\r\nConnection: close\r\n"); proxy.Start(); IHttpClient client = GetHttpClient(proxy.Port); HttpRequestInfo testReqInfo = new HttpRequestInfo(testRequest); client.SendRequest(testReqInfo); Assert.IsTrue(proxy.CurrentConnection.Closed); proxy.Stop(); }
public void Test_Proxy_ConnectionBusyProperty() { string testRequest = "GET http://site.com/ HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"; MockProxy proxy = new MockProxy(testRequest, "HTTP/1.1 200 OK\r\n\r\n"); proxy.Start(); IHttpClient client = GetHttpClient(proxy.Port); HttpRequestInfo testReqInfo = new HttpRequestInfo(testRequest); client.SendRequest(testReqInfo); Assert.IsFalse(proxy.CurrentConnection.IsBusy); proxy.Stop(); }
public void TestExclusions() { TrafficViewerFile dataStore = new TrafficViewerFile(); dataStore.Profile.SetExclusions(new string[1] { @".*\.gif" }); TrafficViewerFile mockSite = new TrafficViewerFile(); string nonExcludedRequest = "GET http://site.com/a HTTP/1.1\r\n\r\n"; string excludedRequest = "GET http://site.com/image.gif HTTP/1.1\r\n\r\n"; string testResponse = "HTTP/1.1 200 OK"; mockSite.AddRequestResponse(nonExcludedRequest, testResponse); mockSite.AddRequestResponse(excludedRequest, testResponse); MockProxy proxy = new MockProxy(dataStore, mockSite); proxy.Start(); IHttpClient httpClient = GetHttpClient(proxy.Port); HttpRequestInfo testRequestInfo = new HttpRequestInfo(excludedRequest); HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo); Assert.AreEqual(200, respInfo.Status); //verify that nothing was added to the file Assert.AreEqual(0, dataStore.RequestCount); //verify that when sending a request that is not excluded the request is being added testRequestInfo = new HttpRequestInfo(nonExcludedRequest); respInfo = httpClient.SendRequest(testRequestInfo); Assert.AreEqual(200, respInfo.Status); //verify that the request was added to the file Assert.AreEqual(1, dataStore.RequestCount); HttpRequestInfo savedReqInfo = new HttpRequestInfo(dataStore.LoadRequestData(0)); Assert.AreEqual(testRequestInfo.FullUrl, savedReqInfo.FullUrl); proxy.Stop(); }
private void SendManyConnectionsToProxy(bool keepAlive) { TrafficViewerFile dataStore = new TrafficViewerFile(); TrafficViewerFile mockSite = new TrafficViewerFile(); string testRequest; if (keepAlive) { testRequest = "GET http://site.com/a HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"; } else { testRequest = "GET http://site.com/a HTTP/1.1\r\nConnection: close\r\n\r\n"; } string expectedResponseLine = "HTTP/1.1 200 OK"; mockSite.AddRequestResponse(testRequest, expectedResponseLine); MockProxy proxy = new MockProxy(dataStore, mockSite); //set a lower connection limit proxy.ConnectionLimit = 2; proxy.Start(); int connCount = 500; //open 500 connections and expect a good response for (int i = 0; i < connCount; i++) { IHttpClient httpClient = GetHttpClient(proxy.Port); HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest); HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo); Assert.AreEqual(200, respInfo.Status); } proxy.Stop(); Assert.AreEqual(connCount, dataStore.RequestCount); }
public void TestGETRequestToProxy() { TrafficViewerFile dataStore = new TrafficViewerFile(); TrafficViewerFile mockSite = new TrafficViewerFile(); string testRequest = "GET http://site.com/a HTTP/1.1\r\n"; string expectedResponseLine = "HTTP/1.1 200 OK"; mockSite.AddRequestResponse(testRequest, expectedResponseLine); MockProxy proxy = new MockProxy(dataStore, mockSite); proxy.Start(); IHttpClient httpClient = GetHttpClient(proxy.Port); HttpRequestInfo testRequestInfo = new HttpRequestInfo(testRequest); HttpResponseInfo respInfo = httpClient.SendRequest(testRequestInfo); Assert.AreEqual(200, respInfo.Status); proxy.Stop(); }
public void Test_HTTP_WebRequestClient_Cookies() { string[] testRequestList = new string[5]; string[] testResponseList = new string[5]; testRequestList[0] = "GET http://site.com/a/1 HTTP/1.1\r\n\r\n"; testResponseList[0] = "HTTP/1.1 302 Redirect\r\nSet-Cookie:a=1; Path=/a\r\nLocation: http://site.com/a\r\n\r\n"; testRequestList[1] = "GET http://site.com/a/2 HTTP/1.1\r\n\r\n"; testResponseList[1] = "HTTP/1.1 302 OK\r\n\r\n"; testRequestList[2] = "GET http://site.com/b HTTP/1.1\r\nCookie:b=2\r\n\r\n"; testResponseList[2] = "HTTP/1.1 302 OK\r\n\r\n"; testRequestList[3] = "GET http://site.com/a/3 HTTP/1.1\r\n\r\n"; testResponseList[3] = "HTTP/1.1 302 Redirect\r\nSet-Cookie:a=2; Path=/a; Expires=Thu, 01-Jan-1970 00:00:01 GMT;\r\nLocation: http://site.com/a\r\n\r\n"; testRequestList[4] = "GET http://site.com/a/4 HTTP/1.1\r\n\r\n"; testResponseList[4] = "HTTP/1.1 200 OK\r\n\r\n"; WebRequestClient client = new WebRequestClient(); client.ShouldHandleCookies = true; TrafficViewerFile mockSite = new TrafficViewerFile(); for (int idx = 0; idx < testRequestList.Length; idx++) { mockSite.AddRequestResponse(testRequestList[idx], testResponseList[idx]); } TrafficViewerFile dataStore = new TrafficViewerFile(); MockProxy mockProxy = new MockProxy(dataStore, mockSite); mockProxy.Start(); client.SetProxySettings(mockProxy.Host, mockProxy.Port, null); for (int idx = 0; idx < testRequestList.Length; idx++) { client.SendRequest(new HttpRequestInfo(testRequestList[idx])); } //second request should have the extra cookie byte[] receivedRequestBytes = dataStore.LoadRequestData(1);//index starts from 0 Assert.IsNotNull(receivedRequestBytes, "Missing second request"); HttpRequestInfo receivedRequest = new HttpRequestInfo(receivedRequestBytes, true); Assert.IsNotNull(receivedRequest.Cookies); Assert.AreEqual(1, receivedRequest.Cookies.Count); Assert.IsTrue(receivedRequest.Cookies.ContainsKey("a")); //third request should not have the a cookie it's sent to /b but should have the b cookie receivedRequestBytes = dataStore.LoadRequestData(2); Assert.IsNotNull(receivedRequestBytes, "Missing third request"); receivedRequest = new HttpRequestInfo(receivedRequestBytes, true); Assert.IsNotNull(receivedRequest.Cookies); Assert.AreEqual(1, receivedRequest.Cookies.Count, "Request to /b should have 1 cookie"); Assert.IsTrue(receivedRequest.Cookies.ContainsKey("b")); //last request should have no cookies because the a cookie is expired receivedRequestBytes = dataStore.LoadRequestData(4); Assert.IsNotNull(receivedRequestBytes, "Missing fifth request"); receivedRequest = new HttpRequestInfo(receivedRequestBytes, true); Assert.IsNotNull(receivedRequest.Cookies); Assert.AreEqual(0, receivedRequest.Cookies.Count, "Last request should have no cookies"); mockProxy.Stop(); }