public async Task GetResponseAsStringAsync_WithQueryParameter_ShouldIncludeQueryParameterInRequest() { // Arrange var httpClient = A.Fake <IHttpClient>(); var fakeResponse = new HttpResponseMessage { Content = new StringContent("All good") }; var url = "test/path?APIKey=value"; A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .Ignored)) .Returns(fakeResponse); var requester = new WebRequester(httpClient); // Act await requester.GetResponseAsStringAsync(url); // Assert A.CallTo( () => httpClient.SendAsync(A <HttpRequestMessage> .That.Matches(req => req.RequestUri.ToString().Equals("/" + url))) ).MustHaveHappened(); }
public async Task GetResponseAsStringAsync_WithTemplate_ShouldBuildCorrectUrl() { // Arrange var httpClient = A.Fake <IHttpClient>(); var template = "test/{item}/{id}"; var parameters = new NameValueCollection { { "item", "itemName" }, { "id", "123" } }; var expectedUrl = "/test/itemName/123"; A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .Ignored)) .Returns(_basicResponse); A.CallTo( () => httpClient.Client) .Returns(_basicClient); var requester = new WebRequester(httpClient); // Act await requester.GetResponseAsStringAsync(template, parameters); // Assert A.CallTo( () => httpClient.SendAsync( A <HttpRequestMessage> .That.Matches(req => req.RequestUri.ToString().Equals(expectedUrl))) ).MustHaveHappened(); }
public async Task GetResponseAsStringAsync_NoParameters_ShouldThrowArgumentNullException() { // Arrange var httpClient = A.Fake <IHttpClient>(); var requester = new WebRequester(httpClient); // Act var ex = await Record.ExceptionAsync(async() => { await requester.GetResponseAsStringAsync("/test", null, 0); }); // Assert ex.ShouldBeOfType <ArgumentNullException>(); }
public async Task GetResponseAsync_GivenConfiguratorThatAllowsCookiesOnRequestMessage_ShouldSetExpectedHeaders() { // Arrange var httpClient = WebRequestHttpClientFactory.Configure(new Uri(Url)) .WithRequestHandlerConfigurator((handler) => handler.UseCookies = false) .WithDefaultRequestHeaders(new Dictionary <string, string> { { "Cookie", "foo=bar;bar=baz" } }) .Create(); var webRequester = new WebRequester(httpClient); // Act var headerResponse = await webRequester.GetResponseAsStringAsync("/get/headers"); var cookieResponse = await webRequester.GetResponseAsStringAsync("/get/cookies"); // Assert headerResponse.Contains("Cookie").ShouldBeTrue(); headerResponse.Contains("foo=bar;bar=baz").ShouldBeTrue(); cookieResponse.ShouldBe("{\"foo\":\"bar\",\"bar\":\"baz\"}"); }
public async Task GetResponseAsStringAsync_NullResponse_ShouldReturnEmptyString() { // Arrange var httpClient = A.Fake <IHttpClient>(); A.CallTo(() => httpClient.SendAsync(A <HttpRequestMessage> .Ignored)).Returns(Task.FromResult <HttpResponseMessage>(null)); var requester = new WebRequester(httpClient); // Act var result = await requester.GetResponseAsStringAsync("/test"); // Assert result.ShouldBe(""); }
public async Task GetResponseAsync_ShouldSetDefaultHeaders() { // Arrange var httpClient = WebRequestHttpClientFactory.Configure(new Uri(Url)) .WithDefaultRequestHeaders(new Dictionary <string, string> { { "fakeHeader", "fakeValue" } }) .Create(); var webRequester = new WebRequester(httpClient); // Act var response = await webRequester.GetResponseAsStringAsync("/headers"); // Assert response.Contains("fakeHeader").ShouldBeTrue(); }