コード例 #1
0
        public void GetByID_returns_expected_item()
        {
            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 = proxy.GetByID(1);

            customer.ID.ShouldBe(1);
        }
コード例 #2
0
        public void GetByID_throws_UnsupportedMediaTypeException_when_bad_formatter_is_supplied()
        {
            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)) { Formatter = new XmlMediaTypeFormatter() };

            proxy.GetByID(1);
            proxy.OnFormatServerErrorWasInvoked.ShouldBe(false);
        }
コード例 #3
0
        public void GetByID_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));

            proxy.GetByID(1);
            proxy.OnFormatServerErrorWasInvoked.ShouldBe(true);
        }
コード例 #4
0
        public void GetByID_throws_DomainObjectNotFoundException_when_server_status_code_is_NOT_FOUND()
        {
            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));

            proxy.GetByID(1);
            proxy.OnFormatServerErrorWasInvoked.ShouldBe(true);
        }
コード例 #5
0
        public void GetByID_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}", Encoding.UTF8, "application/json") }));

            var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object));
            proxy.GetByID(1);

            proxy.BuildConfiguredClientWasInvoked.ShouldBe(true);
            proxy.GetMediaTypeFormatterWasInvoked.ShouldBe(true);
            proxy.OnParseResponseWasInvoked.ShouldBe(true);
        }
コード例 #6
0
        public void GetByID_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/1");
                    Assert.AreEqual(HttpMethod.Get, r.Method);
                });

            var proxy = new HttpServiceProxyStub(new HttpClient(handler.Object));
            proxy.GetByID(1);
        }