public void GetAll_invokes_expected_virtual_methods() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("[{\"ID\": 1}, {\"ID\":2}]", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); proxy.GetAll(); proxy.BuildConfiguredClientWasInvoked.ShouldBe(true); proxy.GetMediaTypeFormatterWasInvoked.ShouldBe(true); proxy.OnParseResponseWasInvoked.ShouldBe(true); }
public void GetAll_throws_NotImplementedException_when_server_status_code_is_NOT_IMPLEMENTED() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotImplemented) { Content = new StringContent("Method not implemented", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); proxy.GetAll(); proxy.OnFormatServerErrorWasInvoked.ShouldBe(true); }
public void GetAll_invokes_an_HTTP_GET_against_the_correct_uri() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("[{\"ID\": 1}]", Encoding.UTF8, "application/json") })) .Callback<HttpRequestMessage, CancellationToken>((r, c) => { r.RequestUri.AbsoluteUri.ShouldBe("http://api/customers"); Assert.AreEqual(HttpMethod.Get, r.Method); }); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); proxy.GetAll(); }
public void Delete_invokes_an_HTTP_DELETE_against_the_correct_uri() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))) .Callback<HttpRequestMessage, CancellationToken>((r, c) => { r.RequestUri.AbsoluteUri.ShouldBe("http://api/customers/1"); Assert.AreEqual(HttpMethod.Delete, r.Method); }); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); proxy.Delete(1); }
public async Task Update_returns_expected_item_async() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"ID\": 1}", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); var customer = await proxy.UpdateAsync(new Customer()); customer.ID.ShouldBe(1); }
public async Task Update_throws_UnsupportedMediaTypeException_when_bad_formatter_is_supplied_async() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"ID\": 1}", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)) { Formatter = new XmlMediaTypeFormatter() }; await proxy.UpdateAsync(new Customer()); proxy.OnFormatServerErrorWasInvoked.ShouldBe(true); }
public async Task Update_throws_ServiceException_when_server_status_code_is_BAD_REQUEST_Async() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("some known error occurred and was handled", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); await proxy.UpdateAsync(new Customer()); proxy.OnFormatServerErrorWasInvoked.ShouldBe(true); }
public async Task Update_throws_DomainObjectNotFoundException_when_server_status_code_is_NOT_FOUND_Async() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("the item was not found", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); await proxy.UpdateAsync(new Customer()); proxy.OnFormatServerErrorWasInvoked.ShouldBe(true); }
public void GetAll_throws_ServiceException_when_server_status_code_is_BAD_REQUEST() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("some known error occurred and was handled", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); try { proxy.GetAll(); } catch (AggregateException ex) { proxy.OnFormatServerErrorWasInvoked.ShouldBe(true); ex.GetBaseException().ShouldBeOfType<ServiceException>(); } }
public void Update_throws_ConcurrencyException_when_server_status_code_is_CONFLICT() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("this item was changed by another user", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); proxy.Update(new Customer()); proxy.OnFormatServerErrorWasInvoked.ShouldBe(true); }
public async Task GetAll_returns_enumerable_list_async() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("[{\"ID\": 1}, {\"ID\":2}]", Encoding.UTF8, "application/json") })); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); var customers = await proxy.GetAllAsync(); customers.FirstOrDefault(f => f.ID == 1).ShouldNotBeNull(); customers.FirstOrDefault(f => f.ID == 2).ShouldNotBeNull(); }
public async Task Delete_invokes_expected_virtual_methods_async() { var handler = new Mock<HttpMessageHandler>(); handler.Protected() .Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>()) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object)); await proxy.DeleteAsync(1); proxy.BuildConfiguredClientWasInvoked.ShouldBe(true); }