public async Task GetAsync_should_return_expected_todo() { var expected = new Todo(1, 456, "go shopping"); mock.SetupGet("/todos/1").ReturnsAsync(201, expected); var actual = await service.GetAsync(1); Assert.Equal(expected.Id, actual.Id); Assert.Equal(expected.UserId, actual.UserId); Assert.Equal(expected.Title, actual.Title); Assert.Equal(expected.Completed, actual.Completed); }
public async Task MockHttpClientExceptionIsThrownIfMatchedMoreThanNRequests() { var expected = "\n\nExpected request on the mock once, but found 2:\nGET /api\n"; var mock = new MockHttpClient(); mock.SetupGet("/api").ReturnsAsync(200); mock.SetupGet("/api").ReturnsAsync(404); var exception = await Assert.ThrowsAsync <MockHttpClientException>(() => mock.Object.GetAsync("/api")); Assert.Equal(ExceptionReasonTypes.MatchedMoreThanNRequests, exception.Reason); Assert.Equal(expected, exception.Message); }
public async Task MockHttpClientExceptionIsThrownIfMultipleUnmatchesOccur() { var expected = "\n\nSetup:\nSetupGet(\"/api\")\n\nDid not match the HTTP method for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n" + "-------\n\nSetup:\nSetupPost(\"api\")\n\nDid not match the Uri for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n" + "-------\n\nSetup:\nSetupPost(\"/api\", headers: x => (Convert(x.Accept, String) == \"wrong accept header\"))\n\nDid not match the headers for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n" + "-------\n\nSetup:\nSetupPost<System.String>(\"/api\", content: x => (x == \"wrong content body\"))\n\nDid not match the content for request:\nPOST /api\nAccept: application/json\r\nContent-Type: text/plain; charset=utf-8\r\n\nactual body\n"; var mock = new MockHttpClient(); mock.SetupGet("/api").ReturnsAsync(200); mock.SetupPost("api").ReturnsAsync(201); mock.SetupPost("/api", headers: x => x.Accept == "wrong accept header").ReturnsAsync(201); mock.SetupPost <string>("/api", content: x => x == "wrong content body").ReturnsAsync(201); var request = new SystemHttpRequestMessage(HttpMethod.Post, "/api"); request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); request.Content = new System.Net.Http.StringContent("actual body"); var exception = await Assert.ThrowsAsync <MockHttpClientException>(() => mock.Object.SendAsync(request)); Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedHttpMethod)); Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedRequestUri)); Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedHeaders)); Assert.True(exception.Reason.HasFlag(ExceptionReasonTypes.UnmatchedContent)); Assert.Equal(expected, exception.Message); }
public async Task TestReturnsObjectType(object expected) { var mock = new MockHttpClient(); mock.SetupGet("/").ReturnsAsync(content: expected); var result = await mock.Object.GetAsync("/"); var actual = JsonConvert.DeserializeObject(await result.Content.ReadAsStringAsync(), expected.GetType()); Assert.Equal(expected, actual); }
public async Task MockHttpClientExceptionIsThrownIfUnmatchedRequestUri() { var expected = "\n\nSetup:\nSetupGet(\"api\")\n\nDid not match the Uri for request:\nGET /api\n"; var mock = new MockHttpClient(); mock.SetupGet("api").ReturnsAsync(200); var exception = await Assert.ThrowsAsync <MockHttpClientException>(() => mock.Object.GetAsync("api")); Assert.Equal(ExceptionReasonTypes.UnmatchedRequestUri, exception.Reason); Assert.Equal(expected, exception.Message); }
public async Task MockHttpClientExceptionIsThrownIfNoResponse() { var expected = "\n\nMissing response for:\nGET /api\n"; var mock = new MockHttpClient(); mock.SetupGet("/api"); var exception = await Assert.ThrowsAsync <MockHttpClientException>(() => mock.Object.GetAsync("/api")); Assert.Equal(ExceptionReasonTypes.NoResponse, exception.Reason); Assert.Equal(expected, exception.Message); }
public async Task ReturnsCustomHeaderInformation() { var mock = new MockHttpClient(); var headers = new HttpResponseHeaders(); headers["x-session-id"] = Guid.NewGuid().ToString(); mock.SetupGet("/").ReturnsAsync(headers); var result = await mock.Object.GetAsync("/"); Assert.Equal(headers.GetValues("x-session-id"), result.Headers.GetValues("x-session-id")); }
public async Task ReturnsHeaderInformation() { var mock = new MockHttpClient(); var headers = new HttpResponseHeaders(); headers.AcceptRanges.Add("none"); mock.SetupGet("/").ReturnsAsync(headers); var result = await mock.Object.GetAsync("/"); Assert.Equal(headers.AcceptRanges, result.Headers.AcceptRanges); }
public async Task MockHttpClientExceptionIsThrownIfUnmatchedHeaders() { var expected = "\n\nSetup:\nSetupGet(\"/api\", headers: x => (Convert(x.Accept, String) == \"wrong accept header\"))\n\nDid not match the headers for request:\nGET /api\nAccept: application/json\n"; var mock = new MockHttpClient(); mock.SetupGet("/api", headers: x => x.Accept == "wrong accept header").ReturnsAsync(200); var request = new SystemHttpRequestMessage(HttpMethod.Get, "/api"); request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var exception = await Assert.ThrowsAsync <MockHttpClientException>(() => mock.Object.SendAsync(request)); Assert.Equal(ExceptionReasonTypes.UnmatchedHeaders, exception.Reason); Assert.Equal(expected, exception.Message); }
public async Task TestByteArrayContent() { var mock = new MockHttpClient(); var expectedByteArray = new byte[] { 65 }; var expected = new ByteArrayContent(expectedByteArray); expected.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("attachment"); mock.SetupGet("/").ReturnsAsync(content: expected); var result = await mock.Object.GetAsync("/"); var resultByteArray = await result.Content.ReadAsByteArrayAsync(); Assert.Equal(expectedByteArray, resultByteArray); Assert.Equal("attachment", result.Content.Headers.ContentDisposition.ToString()); }
public async Task TestResponseContentHeaders() { var mock = new MockHttpClient(); var currentDate = DateTime.UtcNow; var expectedExpires = new DateTimeOffset(currentDate.AddSeconds(60)); var expectedByteArray = new byte[] { 65 }; var expected = new ByteArrayContent(expectedByteArray); expected.Headers.Allow.Add("GET"); expected.Headers.Allow.Add("POST"); expected.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("attachment"); expected.Headers.ContentLength = expectedByteArray.Length; expected.Headers.ContentLocation = new Uri("https://example.com"); expected.Headers.ContentRange = ContentRangeHeaderValue.Parse("bytes 200-1000/67589"); expected.Headers.ContentType = MediaTypeHeaderValue.Parse("text/plain"); expected.Headers.ContentMD5 = new byte[] { 1 }; expected.Headers.ContentLanguage.Add("en-US"); expected.Headers.ContentEncoding.Add("gzip"); expected.Headers.Expires = expectedExpires; expected.Headers.LastModified = currentDate; mock.SetupGet("/").ReturnsAsync(content: expected); var result = await mock.Object.GetAsync("/"); var resultByteArray = await result.Content.ReadAsByteArrayAsync(); Assert.Equal(expectedByteArray, resultByteArray); Assert.Equal("GET, POST", result.Content.Headers.Allow.ToString()); Assert.Equal("attachment", result.Content.Headers.ContentDisposition.ToString()); Assert.Equal(expectedByteArray.Length, result.Content.Headers.ContentLength); Assert.Equal("https://example.com/", result.Content.Headers.ContentLocation.AbsoluteUri); Assert.Equal("bytes 200-1000/67589", result.Content.Headers.ContentRange.ToString()); Assert.Equal("text/plain", result.Content.Headers.ContentType.ToString()); Assert.Equal(new byte[] { 1 }, result.Content.Headers.ContentMD5); Assert.Equal("en-US", result.Content.Headers.ContentLanguage.ToString()); Assert.Equal("gzip", result.Content.Headers.ContentEncoding.ToString()); Assert.NotNull(result.Content.Headers.Expires); Assert.NotNull(result.Content.Headers.LastModified); Assert.Equal(expectedExpires.ToString("s"), result.Content.Headers.Expires.Value.ToString("s")); Assert.Equal(currentDate.ToString("s"), result.Content.Headers.LastModified.Value.ToString("s")); }
public async Task ReturnsMultipleHeaderInformation() { var mock = new MockHttpClient(); var headers = new HttpResponseHeaders(); headers["x-session-id"] = Guid.NewGuid().ToString(); headers.AcceptRanges.Add("none"); headers.AcceptRanges.Add("bytes"); headers.Location = new Uri("https://localhost/v1/customers/1"); mock.SetupGet("/").ReturnsAsync(headers); var result = await mock.Object.GetAsync("/"); Assert.Equal(headers.GetValues("x-session-id"), result.Headers.GetValues("x-session-id")); Assert.Equal(headers.AcceptRanges, result.Headers.AcceptRanges); Assert.Equal(headers.Location, result.Headers.Location); }