Example #1
0
            public async Task WhenDeserializationThrowsThenCapturedInResponseException(string contentType, string content)
            {
                var expectedResponse = new HttpConnectResponse <TestType>(HttpStatusCode.OK);

                expectedResponse.Content = new HttpConnectResponseContent();
                expectedResponse.Content.Headers.ContentType = new ContentTypeHeader(contentType);
                expectedResponse.Content.Content             = content;
                expectedResponse.Status = HttpConnectResponseStatus.Completed;

                var client = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        ctx.Response = expectedResponse;
                        return(Task.CompletedTask);
                    });
                });

                var actualResponse = await client.SendAsync <TestType>(
                    new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")),
                    CancellationToken.None);

                actualResponse.IsSuccess.Should().BeFalse();
                actualResponse.Exception.Should().NotBeNull();
                actualResponse.Data.Should().BeNull();
            }
Example #2
0
            public async Task WhenResponseContentTypeContainsCharsetThenCanDeserialize(string contentType, string content)
            {
                var expectedResponse = new HttpConnectResponse <TestType>(HttpStatusCode.OK);

                expectedResponse.Content = new HttpConnectResponseContent();
                expectedResponse.Content.Headers.ContentType = new ContentTypeHeader(contentType);
                expectedResponse.Content.Content             = content;
                expectedResponse.Data = new TestType {
                    Name = "openwrks"
                };
                expectedResponse.Status = HttpConnectResponseStatus.Completed;

                var client = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        ctx.Response = expectedResponse;
                        return(Task.CompletedTask);
                    });
                });

                var actualResponse = await client.SendAsync <TestType>(new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")), CancellationToken.None);

                actualResponse.Should().BeEquivalentTo(expectedResponse);
            }
Example #3
0
            public async Task WhenClientIsSetupWithBaseUrlAndRequestUsesRelativeUriTheRequestUriUpdatedWithBaseUrl()
            {
                var client  = new HttpConnectClient(new Uri("https://www.example.com"));
                var request = new HttpConnectRequest(HttpMethod.Get, new Uri("/relative/url", UriKind.Relative));

                await client.SendAsync <int>(request, CancellationToken.None);

                request.RequestUri.Should().Be(new Uri("https://www.example.com/relative/url"));
            }
Example #4
0
            public async Task WhenRequestUsesRelativeUriAndNoBaseUrlThenThrows()
            {
                var client = new HttpConnectClient();

                Exception ex = await Assert.ThrowsAsync <InvalidOperationException>(
                    () => client.SendAsync <int>(
                        new HttpConnectRequest(HttpMethod.Get, new Uri("/relative/url", UriKind.Relative)),
                        CancellationToken.None));
            }
Example #5
0
            public async Task WhenBuiltWithMiddlewareThatThrowsExceptionThenRequestStatusIsError()
            {
                Exception expectedException = new Exception("test exception");
                var       client            = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        throw expectedException;
                    });
                });

                var actualResponse = await client.SendAsync <int>(new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")), CancellationToken.None);

                actualResponse.Status.Should().Be(HttpConnectResponseStatus.Error);
            }
Example #6
0
            public async Task WhenBuiltWithSimpleMiddlewareThenReturnsExpectedResponse()
            {
                var expectedResponse = new HttpConnectResponse(HttpStatusCode.OK);
                var client           = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        ctx.Response = expectedResponse;
                        return(Task.CompletedTask);
                    });
                });

                var actualResponse = await client.SendAsync(new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")), CancellationToken.None);

                actualResponse.Should().Be(expectedResponse);
            }
Example #7
0
            public async Task WhenMiddlewareReturnsNonSuccessStatusCodeThenResponseIsNotSuccessful(HttpStatusCode statusCode)
            {
                var expectedResponse = new HttpConnectResponse <int>(statusCode);

                var client = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        ctx.Response = expectedResponse;
                        return(Task.CompletedTask);
                    });
                });

                var actualResponse = await client.SendAsync <int>(new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")), CancellationToken.None);

                actualResponse.IsSuccess.Should().BeFalse();
            }
Example #8
0
            public async Task WhenBuiltWithMultipleMiddlewaresThenAllMiddlewareExecuted()
            {
                int executionCounter = 0;
                Func <HttpConnectRequestDelegate, HttpConnectRequestDelegate> middleware =
                    (next) => (ctx) => { executionCounter++; return(next(ctx)); };
                var client = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(middleware);
                    pipline.Use(middleware);
                    pipline.Use(middleware);
                    pipline.Use(middleware);
                    pipline.Use(middleware);
                    pipline.Use(middleware);
                    pipline.Use(middleware);
                    pipline.Use(middleware);
                });

                await client.SendAsync(new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")), CancellationToken.None);

                executionCounter.Should().Be(8);
            }
Example #9
0
            public async Task WhenMiddlewareReturnsNonSuccessResponseThenDoesNotDeserializeResponse(HttpStatusCode statusCode)
            {
                var expectedResponse = new HttpConnectResponse <int>(statusCode);

                expectedResponse.Content = new HttpConnectResponseContent();
                expectedResponse.Content.Headers.ContentType = new ContentTypeHeader("application/json");
                expectedResponse.Content.Content             = "{\"error\":\"test_error\"";
                expectedResponse.Data   = default;
                expectedResponse.Status = HttpConnectResponseStatus.Completed;

                var client = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        ctx.Response = expectedResponse;
                        return(Task.CompletedTask);
                    });
                });

                var actualResponse = await client.SendAsync <int>(new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")), CancellationToken.None);

                actualResponse.Should().BeEquivalentTo(expectedResponse);
            }
Example #10
0
            public async Task WhenBuiltWithSimpleMiddlewareThenReturnsExpectedResponse()
            {
                var expectedResponse = new HttpConnectResponse <int>(HttpStatusCode.OK);

                expectedResponse.Content = new HttpConnectResponseContent();
                expectedResponse.Content.Headers.ContentType = new ContentTypeHeader("application/json");
                expectedResponse.Content.Content             = "1";
                expectedResponse.Data   = 1;
                expectedResponse.Status = HttpConnectResponseStatus.Completed;

                var client = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        ctx.Response = expectedResponse;
                        return(Task.CompletedTask);
                    });
                });

                var actualResponse = await client.SendAsync <int>(new HttpConnectRequest(HttpMethod.Get, new Uri("https://www.example.com")), CancellationToken.None);

                actualResponse.Should().BeEquivalentTo(expectedResponse);
            }
Example #11
0
            public async Task WhenBuiltWithMiddlewareThatThrowsExceptionAfterResponseHasBeenSetThenOriginalResponseReturnedWithError()
            {
                HttpConnectResponse <int> expectedResponse = new HttpConnectResponse <int>(HttpStatusCode.Unauthorized);
                Exception expectedException = new Exception("test exception");
                var       client            = new HttpConnectClient((pipline) =>
                {
                    pipline.Use(next => ctx =>
                    {
                        ctx.Response = expectedResponse;
                        return(next(ctx));
                    });
                    pipline.Use(next => ctx =>
                    {
                        throw expectedException;
                    });
                });

                var actualResponse = await client.SendAsync <int>(
                    new HttpConnectRequest(HttpMethod.Head, new Uri("https://www.example.com")),
                    CancellationToken.None);

                actualResponse.Should().BeEquivalentTo(expectedResponse);
                actualResponse.Status.Should().Be(HttpConnectResponseStatus.Error);
            }
Example #12
0
            public async Task WhenRequestIsNullTheThrows()
            {
                var client = new HttpConnectClient();

                await Assert.ThrowsAsync <ArgumentNullException>(() => client.SendAsync(null, CancellationToken.None));
            }