public void HttpRequestStringWithSettingsTest() { var settings = new HttpRequestSettings() { Url = "http://microsoft.com", }; string html = HttpUtils.HttpRequestString(settings); Assert.IsNotNull(html); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); }
public void InvalidUrlTest() { var settings = new HttpRequestSettings() { Url = "http://west-wind.com/invalidurl.html", }; string html = HttpUtils.HttpRequestString(settings); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.NotFound); }
/// <summary> /// Intializes this instance of WebClient with settings values /// </summary> /// <param name="settings"></param> public HttpUtilsWebClient(HttpRequestSettings settings = null) { Settings = settings; if (settings != null) { if (settings.Credentials != null) Credentials = settings.Credentials; if (settings.Proxy != null) Proxy = settings.Proxy; if (settings.Encoding != null) Encoding = settings.Encoding; if (settings.Headers != null) { foreach (var header in settings.Headers) { Headers[header.Key] = header.Value; } } } }
public async Task JsonRequestPostAsyncTest() { var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('Hello World, I will own you!');", Comment = "World domination imminent" }; var settings = new HttpRequestSettings() { Url = "http://codepaste.net/recent?format=json", Content = postSnippet, HttpVerb = "POST" }; var snippets = await HttpUtils.JsonRequestAsync<List<CodeSnippet>>(settings); Assert.IsNotNull(snippets); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); Assert.IsTrue(snippets.Count > 0); Console.WriteLine(snippets.Count); Console.WriteLine(settings.CapturedRequestContent); Console.WriteLine(); Console.WriteLine(settings.CapturedResponseContent); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) continue; Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } // This doesn't work for the async version - Response is never set by the base class Console.WriteLine("Status Code: " + settings.Response.StatusCode); foreach (var header in settings.Response.Headers) { Console.WriteLine(header + ": " + settings.Response.Headers[header.ToString()]); } }
public void JsonRequestPostTest() { var postSnippet = new CodeSnippet() { UserId = "Bogus", Code = "string.Format('die Bären sind süss und sauer.');", Comment = "World domination imminent" }; var settings = new HttpRequestSettings() { Url = "http://codepaste.net/recent?format=json", Content = postSnippet, HttpVerb = "POST" }; var snippets = HttpUtils.JsonRequest<List<CodeSnippet>>(settings); Assert.IsNotNull(snippets); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); Assert.IsTrue(snippets.Count > 0); Console.WriteLine(snippets.Count); Console.WriteLine(settings.CapturedRequestContent); Console.WriteLine(); Console.WriteLine(settings.CapturedResponseContent); foreach (var snippet in snippets) { if (string.IsNullOrEmpty(snippet.Code)) continue; Console.WriteLine(snippet.Code.Substring(0, Math.Min(snippet.Code.Length, 200))); Console.WriteLine("--"); } Console.WriteLine("Status Code: " + settings.Response.StatusCode); foreach (var header in settings.Response.Headers) { Console.WriteLine(header + ": " + settings.Response.Headers[header.ToString()]); } }
public async Task JsonRequestAsyncTest() { var settings = new HttpRequestSettings() { Url = "http://codepaste.net/recent?format=json", }; var snippets = await HttpUtils.JsonRequestAsync<List<CodeSnippet>>(settings); Assert.IsNotNull(snippets); Assert.IsTrue(settings.ResponseStatusCode == System.Net.HttpStatusCode.OK); Assert.IsTrue(snippets.Count > 0); Console.WriteLine(snippets.Count); Console.WriteLine(settings.CapturedResponseContent); }