public string SendTest(string mutatedRequest, string host, int port, bool useSSL) { IHttpClient client = new WebRequestClient(); HttpRequestInfo reqInfo = new HttpRequestInfo(mutatedRequest); reqInfo.IsSecure = useSSL; reqInfo.Host = host; reqInfo.Port = port; HttpResponseInfo resp = null; try { resp = client.SendRequest(reqInfo); } catch { } string response; if (resp != null) { PatternTracker.Instance.UpdatePatternValues(resp); response = resp.ToString(); } else { response = String.Empty; } return(response); }
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); } }
protected void CheckResponseStatus(string testRequest, WebRequestClient client, int expectedStatus) { HttpRequestInfo reqInfo = new HttpRequestInfo(testRequest); HttpResponseInfo receivedResponse = client.SendRequest(reqInfo); Assert.AreEqual(expectedStatus, receivedResponse.Status); }
/// <summary> /// Gets an account from the servers. /// </summary> /// <param name="userId">The account ID of the user.</param> /// <returns>An account.</returns> public static string GetString(int userId) => WebRequestClient.SendRequest(new WebRequest { Url = "http://boomlings.com/database/getGJUserInfo20.php", Content = new FormUrlEncodedContent(new Dictionary <string, string> { { "targetAccountID", userId.ToString() }, { "secret", "Wmfd2893gb7" } }) });
/// <summary> /// Gets an account from the servers. /// </summary> /// <param name="userId">The account ID of the user.</param> /// <returns>An account.</returns> public static Account Get(int userId) => RobtopAnalyzer.DeserializeObject <Account>(WebRequestClient.SendRequest(new WebRequest { Url = "http://boomlings.com/database/getGJUserInfo20.php", Content = new FormUrlEncodedContent(new Dictionary <string, string> { { "targetAccountID", userId.ToString() }, { "secret", "Wmfd2893gb7" } }) }));
public void TestSendDirectRequest() { string funky = WebRequestClient.SendRequest(new WebRequest { Url = @"http://boomlings.com/database/accounts/loginGJAccount.php", Method = HttpMethod.Post }, new WebRequestClientOptions() { IgnoreGdExceptions = true }); Assert.AreEqual(funky, "-1", "HTTP Requests are working."); }
public void TestSendQueuedRequest() { // add request var req = new WebRequest { Url = @"http://boomlings.com/database/accounts/loginGJAccount.php", Method = HttpMethod.Post }; client.AddRequest(req); // send request var funky = client.SendRequest(); Assert.AreEqual(funky, "-1", "HTTP Requests are working."); }
/// <summary> /// A method to login to an account, creating a <see cref="UserAccount" /> object. /// </summary> /// <param name="username">The username.</param> /// <param name="password">The password</param> /// <returns>A user account, if successful.</returns> public static UserAccount Login(string username, string password) { var response = WebRequestClient.SendRequest(new WebRequest { Url = @"http://boomlings.com/database/accounts/loginGJAccount.php", Content = new FormUrlEncodedContent(new Dictionary <string, string> { { "userName", username }, { "password", password }, { "secret", "Wmfv3899gc9" }, { "udid", "GDNET" } }), Method = HttpMethod.Post }); return(RobtopAnalyzer.DeserializeObject <UserAccount>(GetString(int.Parse(response.Split(',')[0])))); }
public void Test_NetworkSettings_ProxyUsesAProxy() { MockProxy mockProxy; string testRequest = "GET http://site.com/ HTTP/1.1\r\n\r\n"; string testResponse = "HTTP/1.1 200 OK\r\n\r\n"; TrafficViewerFile dataStore = new TrafficViewerFile(); mockProxy = SetupMockProxy(testRequest, testResponse, dataStore); mockProxy.Start(); ManualExploreProxy meProxy = new ManualExploreProxy("127.0.0.1", 0, null); //use a random port meProxy.NetworkSettings.WebProxy = new WebProxy(mockProxy.Host, mockProxy.Port); meProxy.Start(); WebRequestClient client = new WebRequestClient(); INetworkSettings networkSettings = new DefaultNetworkSettings(); networkSettings.WebProxy = new WebProxy(meProxy.Host, meProxy.Port); client.SetNetworkSettings(networkSettings); HttpRequestInfo testReqInfo = new HttpRequestInfo(testRequest); Assert.AreEqual(0, dataStore.RequestCount); HttpResponseInfo respInfo = client.SendRequest(testReqInfo); meProxy.Stop(); mockProxy.Stop(); //test that the request goes through the mock proxy by checking the data store Assert.AreEqual(200, respInfo.Status); Assert.AreEqual(1, dataStore.RequestCount); }
/// <summary> /// Sends the specified request info /// </summary> /// <param name="source"></param> /// <param name="info"></param> private void SendRequest(ITrafficDataAccessor source, TVRequestInfo info) { byte[] reqBytes = source.LoadRequestData(info.Id); string updatedRequest = PatternTracker.Instance.UpdateRequest(Constants.DefaultEncoding.GetString(reqBytes)); HttpRequestInfo reqInfo = new HttpRequestInfo(updatedRequest); reqInfo.IsSecure = info.IsHttps; _sessionIdHelper.UpdateSessionIds(reqInfo, _prevRequest, _prevResponse); info.RequestTime = DateTime.Now; //save the request that will be sent source.SaveRequest(info.Id, Constants.DefaultEncoding.GetBytes(updatedRequest)); try { _prevResponse = _httpClient.SendRequest(reqInfo); _prevRequest = reqInfo; //save the request and response if (_prevResponse != null) { info.ResponseTime = DateTime.Now; PatternTracker.Instance.UpdatePatternValues(_prevResponse); source.SaveResponse(info.Id, _prevResponse.ToArray()); } else { source.SaveResponse(info.Id, Constants.DefaultEncoding.GetBytes(_communicationError)); } } catch (Exception ex) { SdkSettings.Instance.Logger.Log(TraceLevel.Error, "Error playing back request {0}", ex.Message); source.SaveResponse(info.Id, Constants.DefaultEncoding.GetBytes(Resources.CommunicationError)); } }
//[TestMethod] public void Test_ReverseProxy() { string testRequest = "GET / HTTP/1.1\r\n"; string site1Response = "HTTP/1.1 200 OK\r\n\r\nThis is site1"; string site2Response = "HTTP/1.1 200 OK\r\n\r\nThis is site2"; //create two mock sites each on a different port and a http client that send a request to the first but in fact gets redirected to the other TrafficViewerFile site1Source = new TrafficViewerFile(); site1Source.AddRequestResponse(testRequest, site1Response); TrafficViewerFile site2Source = new TrafficViewerFile(); site2Source.AddRequestResponse(testRequest, site2Response); TrafficStoreProxy mockSite1 = new TrafficStoreProxy( site1Source, null, "127.0.0.1", 0, 0); mockSite1.Start(); TrafficStoreProxy mockSite2 = new TrafficStoreProxy( site2Source, null, "127.0.0.1", 0, 0); mockSite2.Start(); HttpRequestInfo reqInfo = new HttpRequestInfo(testRequest); //request will be sent to site 1 reqInfo.Host = mockSite1.Host; reqInfo.Port = mockSite1.Port; ReverseProxy revProxy = new ReverseProxy("127.0.0.1", 0, 0, null); revProxy.ExtraOptions[ReverseProxy.FORWARDING_HOST_OPT] = mockSite2.Host; revProxy.ExtraOptions[ReverseProxy.FORWARDING_PORT_OPT] = mockSite2.Port.ToString(); revProxy.Start(); //make an http client IHttpClient client = new WebRequestClient(); DefaultNetworkSettings settings = new DefaultNetworkSettings(); settings.WebProxy = new WebProxy(revProxy.Host, revProxy.Port); client.SetNetworkSettings(settings); //send the request Http and verify the target site received it HttpResponseInfo respInfo = client.SendRequest(reqInfo); string respBody = respInfo.ResponseBody.ToString(); Assert.IsTrue(respBody.Contains("This is site2")); //check over ssl reqInfo.IsSecure = true; respInfo = client.SendRequest(reqInfo); respBody = respInfo.ResponseBody.ToString(); Assert.IsTrue(respBody.Contains("This is site2")); mockSite1.Stop(); mockSite2.Stop(); revProxy.Stop(); }
/// <summary> /// Gets an array of levels. /// </summary> /// <param name="search">A search query</param> /// <param name="options">Level search options.</param> /// <returns>The array.</returns> public static Level[] GetLevels(string search = "", LevelSearchOptions options = null) { if (options == null) { options = new LevelSearchOptions(); } Dictionary <string, string> content = new Dictionary <string, string>(); content = new Dictionary <string, string> { { "gameVersion", "21" }, { "binaryVersion", "35" }, { "type", ((int)options.SearchType).ToString() }, { "str", search }, { "len", (options.Length.Length > 0) ? options.Length.FormatEnum(",") : "-" }, { "diff", (options.Difficulty.Length > 0) ? options.Difficulty.FormatEnum(",") : "-" }, { "coins", Convert.ToInt32(options.HasCoins).ToString() }, { "noStar", Convert.ToInt32(options.IsUnrated).ToString() }, { "twoPlayer", Convert.ToInt32(options.TwoPlayer).ToString() }, { "featured", Convert.ToInt32(options.Featured).ToString() }, { "epic", Convert.ToInt32(options.Epic).ToString() }, { "original", Convert.ToInt32(options.Original).ToString() }, { "secret", "Wmfd2893gb7" } }; // extraneous/optional params if (options.DemonType != DemonType.None) { content["diff"] = "-2"; content.Add("demonFilter", ((int)options.DemonType).ToString()); } if (options.CustomSongId > 0 && options.Song > 0) { throw new ArgumentException("You may not use NewGrounds Song ID's and In-game Song enums at once."); } if (options.CustomSongId > 0 || options.Song > 0) { if (options.CustomSongId > 0) { content.Add("customSong", options.CustomSongId.ToString()); } else if (options.Song > 0) { content.Add("song", ((int)options.Song).ToString()); } } string result = WebRequestClient.SendRequest(new WebRequest { Url = "http://boomlings.com/database/getGJLevels21.php", Content = new FormUrlEncodedContent(content) }); List <Level> levels = RobtopAnalyzer.DeserializeObjectList <Level>(result.Split("#")[0]); return(levels.ToArray()); }
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(); }