/// <summary> /// Downloads the update configurations from the server. /// </summary> /// <param name="configFileUri">The url of the configuration file.</param> /// <param name="credentials">The HTTP authentication credentials.</param> /// <param name="proxy">The optional proxy to use.</param> /// <param name="timeout">The timeout for the download request. In milliseconds. Default 10000.</param> /// <returns>Returns an <see cref="IEnumerable{UpdateConfiguration}" /> containing the package configurations.</returns> public static IEnumerable <UpdateConfiguration> Download(Uri configFileUri, NetworkCredential credentials, WebProxy proxy, int timeout = 10000) { using (var wc = new WebClientWrapper(timeout)) { wc.Encoding = Encoding.UTF8; if (credentials != null) { wc.Credentials = credentials; } if (proxy != null) { wc.Proxy = proxy; } // Check for SSL and ignore it ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); }; var source = wc.DownloadString(configFileUri); if (!string.IsNullOrEmpty(source)) { return(Serializer.Deserialize <IEnumerable <UpdateConfiguration> >(source)); } } return(Enumerable.Empty <UpdateConfiguration>()); }
/// <summary> /// Downloads the update configurations from the server. /// </summary> /// <param name="configFileUri">The url of the configuration file.</param> /// <param name="credentials">The HTTP authentication credentials.</param> /// <param name="proxy">The optional proxy to use.</param> /// <param name="timeout">The timeout for the download request. In milliseconds. Default 10000.</param> /// <returns>Returns an <see cref="IEnumerable{UpdateConfiguration}" /> containing the package configurations.</returns> public static IEnumerable <UpdateConfiguration> Download(Uri configFileUri, NetworkCredential credentials, WebProxy proxy, int timeout = 10000) { using (var wc = new WebClientWrapper(timeout)) { wc.Encoding = Encoding.UTF8; if (credentials != null) { wc.Credentials = credentials; } if (proxy != null) { wc.Proxy = proxy; } ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; var source = wc.DownloadString(configFileUri); if (!string.IsNullOrEmpty(source)) { return(Serializer.Deserialize <IEnumerable <UpdateConfiguration> >(source)); } } return(Enumerable.Empty <UpdateConfiguration>()); }
/// <summary> /// IWebClientを実装したオブジェクト(WebClientWrapper)を返す /// </summary> /// <remarks>生成したオブジェクトの破棄はユーザー側の責任</remarks> /// <returns>生成したオブジェクト</returns> public IWebClient CreateWebClient() { WebClientWrapper client = new WebClientWrapper(); client.Encoding = _encoding; return(client); }
protected virtual string ProcessJavaScript(string url) { RRTracer.Trace("Beginning Processing {0}", url); string jsContent = string.Empty; url = url.Replace("&", "&"); RRTracer.Trace("Beginning to Download {0}", url); using (var response = WebClientWrapper.Download <JavaScriptResource>(url)) { if (response == null) { RRTracer.Trace("Response is null for {0}", url); return(null); } var expires = response.Headers["Expires"]; try { if (!string.IsNullOrEmpty(expires) && DateTime.Parse(expires) < DateTime.Now.AddDays(6)) { RRTracer.Trace("{0} expires in less than a week", url); AddUrlToIgnores(url); } } catch (FormatException) { RRTracer.Trace("Format exception thrown parsing expires of {0}", url); } var cacheControl = response.Headers["Cache-Control"]; if (!string.IsNullOrEmpty(cacheControl)) { var cacheControlVals = cacheControl.ToLower().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var val in cacheControlVals) { try { if ((val.Contains("no-") || (val.Contains("max-age") && Int32.Parse(val.Trim().Remove(0, 8)) < (60 * 60 * 24 * 7)))) { RRTracer.Trace("{0} max-age in less than a week", url); AddUrlToIgnores(url); } } catch (FormatException) { RRTracer.Trace("Format exception thrown parsing max-age of {0}", url); } } } using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { using (var streamDecoder = responseDecoder.GetDecodableStream(responseStream, response.Headers["Content-Encoding"])) { using (var streameader = new StreamReader(streamDecoder, Encoding.UTF8)) { jsContent = streameader.ReadToEnd(); RRTracer.Trace("content of {0} read and has {1} bytes and a length of {2}", url, response.ContentLength, jsContent.Length); } } } } } return(jsContent); }
public void given_the_sut() { _sut = new WebClientWrapper(); _address = "http://192.168.23.171:8080/api/respones.json"; _optionalHeaders = new Dictionary <string, string> { { "Content-Type", "application/xml" } }; }
public async Task <PropertiesResponse> GetAsync() { var Url = new Uri(this._urlBase); var response = await WebClientWrapper.GetAsync <PropertiesResponse>(Url); return(response); }
public void ProcessRequest(HttpContext context) { var url = context.Request.RawUrl.Substring(context.Request.RawUrl.IndexOf("test=") + 5); var response = new WebClientWrapper().DownloadString(url); var config = RequestReduce.Api.Registry.Configuration; config.BaseAddress = url; context.Response.Write(response); }
public void Class_Handles_Error_Response() { var client = new WebClientWrapper(this.baseUrl); PageResponse <string> response = client.DownloadString("/invalid-url"); response.ResponseCode.ShouldBe(HttpStatusCode.InternalServerError); response.Content.ShouldBeNull(); }
public void DownloadString_Handles_Valid_Response() { var client = new WebClientWrapper(this.baseUrl); PageResponse <string> response = client.DownloadString("/valid-url"); response.ResponseCode.ShouldBe(HttpStatusCode.OK); response.Content.ShouldContain("valid url"); }
public void Class_Handles_404_Response() { var client = new WebClientWrapper(this.baseUrl); PageResponse <string> response = client.DownloadString("/nonexistent-url"); response.ResponseCode.ShouldBe(HttpStatusCode.NotFound); response.Content.ShouldBeNull(); }
public IWebClient TokenandWebClientSetupRemoteCall(out string token) { WebClientWrapper wclient = new WebClientWrapper(new MockWebClient()); var apiCall = new TokenApi(Settings.Default.BaseUrl, 1, Settings.Default.ApiDeveloperId, Settings.Default.ApiKey, wclient); token = apiCall.GetToken("*****@*****.**", "P@ssword123"); return(wclient); }
public void DefaultUserAgents() { var client1 = new WebClientWrapper(); var client2 = new HttpClientWrapper(); string expectedUserAgent = null; Assert.AreEqual(expectedUserAgent, client1.UserAgent); Assert.AreEqual(client1.UserAgent, client2.UserAgent); Console.WriteLine($"UserAgent: \"{client1.UserAgent}\""); }
public Form1() { _trimmer = new Regex(" +", RegexOptions.Compiled); var host = ConfigurationManager.AppSettings["host"]; InitializeComponent(); var client = new WebClientWrapper(); _detektor = new Core.Detektor(client, host); notifyIcon1.Icon = new Icon("warning.ico"); }
protected virtual string ProcessJavaScript(string url) { string jsContent = string.Empty; url = url.Replace("&", "&"); using (var response = WebClientWrapper.Download <JavaScriptResource>(url)) { if (response == null) { return(null); } var expires = response.Headers["Expires"]; try { if (!string.IsNullOrEmpty(expires) && DateTime.Parse(expires) < DateTime.Now.AddDays(6)) { AddUrlToIgnores(url); } } catch (FormatException) { } var cacheControl = response.Headers["Cache-Control"]; if (!string.IsNullOrEmpty(cacheControl)) { var cacheControlVals = cacheControl.ToLower().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var val in cacheControlVals) { try { if ((val.Contains("no-") || (val.Contains("max-age") && Int32.Parse(val.Trim().Remove(0, 8)) < (60 * 60 * 24 * 7)))) { AddUrlToIgnores(url); } } catch (FormatException) {} } } using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { using (var streamDecoder = responseDecoder.GetDecodableStream(responseStream, response.Headers["Content-Encoding"])) { using (var streameader = new StreamReader(streamDecoder, Encoding.UTF8)) { jsContent = streameader.ReadToEnd(); } } } } } return(jsContent); }
public void NotFound_ThrowOnException() { var client1 = new WebClientWrapper(); var client2 = new HttpClientWrapper(); var url = "https://beatsaver.com/cdn/5317/aaa14fb7dcaeda7a688db77617045a24d7baa151d.zip"; bool expectedResponseSuccess = false; int expectedStatus = 404; string expectedContentType = null; long? expectedContentLength = null; bool? actualResponseSuccess = null; string actualReasonPhrase = null; int? actualStatus = null; string actualContentType = null; long? actualContentLength = null; Uri actualRequestUri = null; var action = new Func <IWebClient, Task <IWebResponseMessage> >(async(target) => { target.ErrorHandling = ErrorHandling.ThrowOnException; IWebResponseMessage response = null; Exception exception = null; try { response = await target.GetAsync(url).ConfigureAwait(false); actualResponseSuccess = response.IsSuccessStatusCode; actualStatus = response.StatusCode; actualContentType = response.Content.ContentType; actualContentLength = response.Content.ContentLength; actualRequestUri = response.RequestUri; //Assert.Fail("Should've thrown exception"); } catch (WebClientException ex) { exception = ex; actualResponseSuccess = ex.Response?.IsSuccessStatusCode; actualReasonPhrase = ex.Response.ReasonPhrase; actualStatus = ex.Response.StatusCode; actualRequestUri = ex.Response.RequestUri; } Assert.AreEqual(expectedResponseSuccess, actualResponseSuccess, $"Failed for {target.GetType().Name}: IsSuccessStatusCode"); Assert.AreEqual(expectedStatus, actualStatus, $"Failed for {target.GetType().Name}: StatusCode"); Assert.AreEqual(expectedContentType, response?.Content?.ContentType, $"Failed for {target.GetType().Name}: ContentType"); Assert.AreEqual(expectedContentLength, response?.Content?.ContentLength, $"Failed for {target.GetType().Name}: ContentLength"); Assert.AreEqual(url, actualRequestUri.ToString(), $"Failed for {target.GetType().Name}: RequestUri"); if (exception != null) { throw exception; } return(response); }); CompareGetAsync(client1, client2, action); }
/// <summary> /// Downloads the update configurations from the server. /// </summary> /// <param name="configFileUri">The url of the configuration file.</param> /// <param name="credentials">The HTTP authentication credentials.</param> /// <param name="proxy">The optional proxy to use.</param> /// <param name="finishedCallback">The <see cref="Action" /> to invoke when the operation has finished.</param> /// <param name="cancellationTokenSource"> /// The optional <see cref="CancellationTokenSource" /> to use for canceling the /// operation. /// </param> /// <param name="timeout">The timeout for the download request. In milliseconds. Default 10000.</param> /// <returns>Returns an <see cref="IEnumerable{UpdateConfiguration}" /> containing the package configurations.</returns> public static void DownloadAsync(Uri configFileUri, NetworkCredential credentials, WebProxy proxy, Action <IEnumerable <UpdateConfiguration>, Exception> finishedCallback, CancellationTokenSource cancellationTokenSource = null, int timeout = 10000) { using (var wc = new WebClientWrapper(timeout)) { var resetEvent = new ManualResetEvent(false); string source = null; Exception exception = null; wc.Encoding = Encoding.UTF8; if (credentials != null) { wc.Credentials = credentials; } if (proxy != null) { wc.Proxy = proxy; } wc.DownloadStringCompleted += (sender, args) => { if (!args.Cancelled) { if (args.Error != null) { exception = args.Error; } else { source = args.Result; } } resetEvent.Set(); resetEvent.Dispose(); }; // Register the cancel async method of the webclient to be called cancellationTokenSource?.Token.Register(wc.CancelAsync); // Check for SSL and ignore it ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); }; wc.DownloadStringAsync(configFileUri); resetEvent.WaitOne(); finishedCallback?.Invoke(!string.IsNullOrEmpty(source) ? Serializer.Deserialize <IEnumerable <UpdateConfiguration> >(source) : Enumerable.Empty <UpdateConfiguration>(), exception); } }
public void Timeout() { var client1 = new WebClientWrapper(); client1.Timeout = 1; var client2 = new HttpClientWrapper(); client2.Timeout = 1; var url = "http://releases.ubuntu.com/18.04.3/ubuntu-18.04.3-desktop-amd64.iso"; string directory = Path.Combine(TestOutputPath, "Canceled"); Directory.CreateDirectory(directory); string filePath = Path.Combine(directory, "canceled.iso"); bool expectedResponseSuccess = false; int expectedStatus = 408; string expectedContentType = null; long? expectedContentLength = null; bool? actualResponseSuccess = null; int? actualStatus = null; string actualContentType = null; long? actualContentLength = null; Uri actualRequestUri = null; var action = new Func <IWebClient, Task <IWebResponseMessage> >(async(target) => { IWebResponseMessage response = null; Exception exception = null; try { response = await target.GetAsync(url).ConfigureAwait(false); Assert.Fail("Should've thrown exception"); } catch (WebClientException ex) { exception = ex; actualResponseSuccess = ex.Response?.IsSuccessStatusCode; actualStatus = ex.Response.StatusCode; actualRequestUri = ex.Response.RequestUri; } Assert.AreEqual(expectedResponseSuccess, actualResponseSuccess, $"Failed for {target.GetType().Name}: IsSuccessStatusCode"); Assert.AreEqual(expectedStatus, actualStatus, $"Failed for {target.GetType().Name}: StatusCode"); Assert.AreEqual(expectedContentType, actualContentType, $"Failed for {target.GetType().Name}: ContentType"); Assert.AreEqual(expectedContentLength, actualContentLength, $"Failed for {target.GetType().Name}: ContentLength"); Assert.AreEqual(url, actualRequestUri.ToString(), $"Failed for {target.GetType().Name}: RequestUri"); if (exception != null) { throw exception; } return(response); }); CompareGetAsync(client1, client2, action); }
public void WillThrowErrorIfNotJavaScript() { if (ConfigurationManager.AppSettings["Environment"] == "Test") { return; } var wrapper = new WebClientWrapper(); var ex = Assert.Throws <InvalidOperationException>(() => wrapper.Download <JavaScriptResource>("http://localhost:8877/local.html")); Assert.NotNull(ex); }
public void SetOnce() { var client1 = new WebClientWrapper(); var client2 = new HttpClientWrapper(); string expectedUserAgent = "UserAgentTest/1.0"; client1.SetUserAgent(expectedUserAgent); client2.SetUserAgent(expectedUserAgent); Assert.AreEqual(expectedUserAgent, client1.UserAgent); Assert.AreEqual(expectedUserAgent, client2.UserAgent); Console.WriteLine($"UserAgent: \"{client1.UserAgent}\""); }
public void WillNotIncludeUtf8PreambleInstring() { if (ConfigurationManager.AppSettings["Environment"] == "Test") { return; } var wrapper = new WebClientWrapper(); var result = wrapper.DownloadString("http://localhost:8877/styles/style1.css"); Assert.NotEqual(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble())[0], result[0]); }
public void Success_NullContentLength() { var client1 = new WebClientWrapper(); var client2 = new HttpClientWrapper(); var url = "http://beatsaver.com/"; bool expectedResponseSuccess = true; int expectedStatus = 200; string expectedContentType = "text/html"; long? expectedContentLength = null; bool? actualResponseSuccess = null; int? actualStatus = null; string actualContentType = null; long? actualContentLength = null; Uri actualRequestUri = null; var action = new Func <IWebClient, Task <IWebResponseMessage> >(async(target) => { Exception exception = null; IWebResponseMessage response = null; try { response = await target.GetAsync(url).ConfigureAwait(false); actualResponseSuccess = response.IsSuccessStatusCode; actualStatus = response.StatusCode; actualContentType = response.Content.ContentType; actualContentLength = response.Content.ContentLength; actualRequestUri = response.RequestUri; //Assert.Fail("Should've thrown exception"); } catch (WebClientException ex) { Assert.Fail("Should not have thrown exception"); exception = ex; actualResponseSuccess = ex.Response?.IsSuccessStatusCode; actualStatus = ex.Response.StatusCode; actualRequestUri = ex.Response.RequestUri; } Assert.AreEqual(expectedResponseSuccess, actualResponseSuccess, $"Failed for {target.GetType().Name}: IsSuccessStatusCode"); Assert.AreEqual(expectedStatus, actualStatus, $"Failed for {target.GetType().Name}: StatusCode"); Assert.AreEqual(expectedContentType, actualContentType, $"Failed for {target.GetType().Name}: ContentType"); Assert.AreEqual(expectedContentLength, actualContentLength, $"Failed for {target.GetType().Name}: ContentLength"); Assert.AreEqual(url, actualRequestUri.ToString(), $"Failed for {target.GetType().Name}: RequestUri"); if (exception != null) { throw exception; } return(response); }); CompareGetAsync(client1, client2, action); }
public void ProcessRequest(HttpContext context) { var url = context.Request.RawUrl.Substring(context.Request.RawUrl.IndexOf("test=") + 5); var response = new WebClientWrapper().DownloadString(url); var config = Api.Registry.Configuration; config.BaseAddress = url; //config.ContentHost = "http://requestreduce.org"; if (response.IndexOf("<base ", StringComparison.OrdinalIgnoreCase) == -1) { response = response.Replace("<head>", string.Format(@"<head><base href=""{0}""></base>", url)); } context.Response.Write(response); }
public Form1() { var host = ConfigurationManager.AppSettings["host"]; InitializeComponent(); _client = new WebClientWrapper(); _klo = new Core.Klo(_client, host); _timer = new System.Timers.Timer(5000) { AutoReset = false }; _timer.Elapsed += OnTimeEvent; OnTimeEvent(null, null); }
private void sendButton_Click(object sender, EventArgs e) { if (!ValidationManager.Validate(this)) { Popup.ShowPopup(this, SystemIcons.Error, "Missing information", "All fields need to have a value.", PopupButtons.Ok); return; } if (!IsValidMailAdress(emailTextBox.Text)) { Popup.ShowPopup(SystemIcons.Error, "Invalid e-mail address", "Please enter a valid e-mail address.", PopupButtons.Ok); return; } try { string responseString; using (var client = new WebClientWrapper()) { responseString = client.DownloadString( String.Format( "http://www.nupdate.net/mail.php?name={0}&sender={1}&content={2}", nameTextBox.Text, emailTextBox.Text, contentTextBox.Text)); } if (!String.IsNullOrEmpty(responseString) && responseString != "\n") { Popup.ShowPopup(this, SystemIcons.Error, "Error while sending feedback.", String.Format("Please report this message: {0}", responseString), PopupButtons.Ok); return; } Popup.ShowPopup(this, SystemIcons.Information, "Delivering successful.", "The feedback was sent. Thank you!", PopupButtons.Ok); Close(); } catch (Exception ex) { Popup.ShowPopup(this, SystemIcons.Error, "Error while sending feedback.", ex, PopupButtons.Ok); } }
public void WebClient_GetAsync_PageNotFound() { using (var mockClient = new MockWebClient()) using (var realClient = new WebClientWrapper()) { mockClient.Timeout = 5000; realClient.Timeout = 5000; realClient.ErrorHandling = ErrorHandling.ReturnEmptyContent; var testUrl = new Uri("https://bsaber.com/wp-jsoasdfn/bsabasdfer-api/songs/"); //WebUtils.Initialize(realClient); using (var realResponse = realClient.GetAsync(testUrl).Result) using (var mockResponse = mockClient.GetAsync(testUrl).Result) { var test = realResponse?.Content?.ReadAsStringAsync().Result; Assert.AreEqual(realResponse.IsSuccessStatusCode, mockResponse.IsSuccessStatusCode); Assert.AreEqual(realResponse.StatusCode, mockResponse.StatusCode); Assert.AreEqual(realResponse.Content?.ContentType, mockResponse.Content.ContentType); } } }
/// <summary> /// Downloads the update configurations from the server. /// </summary> /// <param name="configFileUrl">The url of the configuration file.</param> /// <param name="proxy">The optional proxy to use.</param> /// <returns> /// Returns an <see cref="IEnumerable" /> of type <see cref="UpdateConfiguration" /> containing the package /// configurations. /// </returns> public static IEnumerable <UpdateConfiguration> Download(Uri configFileUrl, WebProxy proxy) { using (var wc = new WebClientWrapper(10000)) { wc.Encoding = Encoding.UTF8; if (proxy != null) { wc.Proxy = proxy; } // Check for SSL and ignore it ServicePointManager.ServerCertificateValidationCallback += delegate { return(true); }; var source = wc.DownloadString(configFileUrl); if (!String.IsNullOrEmpty(source)) { return(Serializer.Deserialize <IEnumerable <UpdateConfiguration> >(source)); } } return(null); }
public void ParsesCharacterAbilityMappings() { var client = new WebClientWrapper("http://finalfantasy.wikia.com/wiki/"); var page = new PageClient(client); var table = page.GetPageHtmlBySelector("/List_of_Final_Fantasy_IX_support_abilities", "table.FFIX"); var parser = new HtmlParser(); var dataList = parser.ParseTable <AbilityData>(table); var sut = new ParsingService(); var mappings = sut.GetCharacterAbilityMappings(dataList); mappings.Count.ShouldBe(252); mappings.First().Ability.ShouldBe("Auto-Reflect"); mappings.First().Character.ShouldBe("Zidane"); mappings.First().AP.ShouldBe(95); mappings.Last().Ability.ShouldBe("Bandit"); mappings.Last().Character.ShouldBe("Zidane"); mappings.Last().AP.ShouldBe(40); }
public override MethodResult Execute(object testClass) { MethodResult result = null; try { result = base.Execute(testClass); } finally { if (!(result is PassedResult)) { var trace = new WebClientWrapper().DownloadString("http://localhost:8877/local.html?OutputError=1&OutputTrace=1"); output.WriteLine(trace); if (output != Console.Out) { Console.Out.WriteLine(trace); } } } return(result); }
public void Parses_Ability_Data() { var client = new WebClientWrapper("http://finalfantasy.wikia.com/wiki/"); var page = new PageClient(client); var table = page.GetPageHtmlBySelector("/List_of_Final_Fantasy_IX_support_abilities", "table.FFIX"); var parser = new HtmlParser(); var dataList = parser.ParseTable <AbilityData>(table); dataList.Count.ShouldBe(63); dataList.First().Name.ShouldBe("Auto-Reflect"); dataList.First().Effect.ShouldContain("Reflect"); dataList.First().Source.ShouldContain("Reflect Ring"); dataList.First().CharacterMap.ShouldContain("Eiko"); dataList.First().Stones.ShouldBe("15"); dataList.Last().Name.ShouldBe("Bandit"); dataList.Last().Effect.ShouldContain("Steal"); dataList.Last().Effect.ShouldContain("item"); dataList.Last().Source.ShouldContain("N-Kai Armlet"); dataList.Last().CharacterMap.ShouldContain("Zidane"); dataList.Last().Stones.ShouldBe("5"); }
/// <summary> /// IWebClientを実装したオブジェクト(WebClientWrapper)を返す /// </summary> /// <remarks>生成したオブジェクトの破棄はユーザー側の責任</remarks> /// <returns>生成したオブジェクト</returns> public IWebClient CreateWebClient() { WebClientWrapper client = new WebClientWrapper(); client.Encoding = _encoding; return client; }