public void SendGetRequestAsync_Error() { var response = new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound, Content = new StringContent("Error") }; var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict); handler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(response); var httpClientFactory = new Mock <IHttpClientFactory>(); httpClientFactory.Setup(h => h.CreateClient(SoundCloudClient.HttpClientName)).Returns(new HttpClient(handler.Object)); // Act var uri = new Uri("http://localhost:5000"); var exception = Assert.ThrowsAsync <SoundCloudApiException>(async() => await new SoundCloudApiGateway(httpClientFactory.Object).SendGetRequestAsync <Comment>(uri)); // Assert Assert.That(exception.HttpStatusCode, Is.EqualTo(response.StatusCode)); Assert.That(exception.HttpContent, Is.EqualTo(response.Content)); var captor = new ArgumentCaptor <HttpRequestMessage>(); handler.Protected().Verify("SendAsync", Times.Once(), captor.CaptureExpr(), ItExpr.IsAny <CancellationToken>()); Assert.That(captor.Value.RequestUri, Is.EqualTo(uri)); Assert.That(captor.Value.Method, Is.EqualTo(HttpMethod.Get)); }
public async Task SendDeleteRequestAsync() { var response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(new Comment { Body = "My Comment" })) }; var handler = new Mock <HttpMessageHandler>(MockBehavior.Strict); handler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(response); var httpClientFactory = new Mock <IHttpClientFactory>(); httpClientFactory.Setup(h => h.CreateClient(SoundCloudClient.HttpClientName)).Returns(new HttpClient(handler.Object)); // Act var uri = new Uri("http://localhost:5000"); var result = await new SoundCloudApiGateway(httpClientFactory.Object).SendDeleteRequestAsync <Comment>(uri); // Assert Assert.That(result.Body, Is.EqualTo("My Comment")); var captor = new ArgumentCaptor <HttpRequestMessage>(); handler.Protected().Verify("SendAsync", Times.Once(), captor.CaptureExpr(), ItExpr.IsAny <CancellationToken>()); Assert.That(captor.Value.RequestUri, Is.EqualTo(uri)); Assert.That(captor.Value.Method, Is.EqualTo(HttpMethod.Delete)); }