예제 #1
0
 public async Task Executes_OnException_WhenParsingFails_WithCorrectContext(
     Exception exception,
     HttpRequestMessage request,
     [Frozen] IJSendParser parser,
     [Frozen] MessageInterceptor interceptor,
     [Greedy, MockHttpClient] JSendClient client)
 {
     // Fixture setup
     Mock.Get(parser)
     .Setup(c => c.ParseAsync <object>(It.IsAny <JsonSerializerSettings>(), It.IsAny <HttpResponseMessage>()))
     .ThrowsAsync(exception);
     // Exercise system
     try
     {
         await client.SendAsync <object>(request);
     }
     catch
     {
     }
     // Verify outcome
     Mock.Get(interceptor)
     .Verify(i => i.OnException(It.Is <ExceptionContext>(
                                    ctx => ctx.HttpRequest == request &&
                                    ctx.Exception == exception
                                    )), Times.Once);
 }
예제 #2
0
 public async Task Executes_OnException_WhenSendingFails_WithCorrectContext(
     Exception exception,
     HttpRequestMessage request,
     [MockHttpClient] HttpClient httpClient,
     [Frozen] MessageInterceptor interceptor,
     [Greedy] JSendClient client)
 {
     // Fixture setup
     Mock.Get(httpClient)
     .Setup(c => c.SendAsync(It.IsAny <HttpRequestMessage>(), It.IsAny <CancellationToken>()))
     .ThrowsAsync(exception);
     // Exercise system
     try
     {
         await client.SendAsync <object>(request);
     }
     catch
     {
     }
     // Verify outcome
     Mock.Get(interceptor)
     .Verify(i => i.OnException(It.Is <ExceptionContext>(
                                    ctx => ctx.HttpRequest == request &&
                                    ctx.Exception == exception
                                    )), Times.Once);
 }
예제 #3
0
        public void JSendParser_IsDefaultJSendParser_ByDefault()
        {
            // Exercise system
            var client = new JSendClient();

            // Verify outcome
            client.Parser.Should().BeSameAs(DefaultJSendParser.Instance);
        }
예제 #4
0
        public void MessageInterceptor_IsNullMessageInterceptor_ByDefault()
        {
            // Exercise system
            var client = new JSendClient();

            // Verify outcome
            client.MessageInterceptor.Should().BeSameAs(NullMessageInterceptor.Instance);
        }
예제 #5
0
        public void Encoding_IsNull_ByDefault()
        {
            // Exercise system
            var client = new JSendClient();

            // Verify outcome
            client.Encoding.Should().BeNull();
        }
예제 #6
0
        public void SerializerSettings_IsCorrectlyInitialized(JSendClientSettings settings)
        {
            // Exercise system
            var client = new JSendClient(settings);

            // Verify outcome
            client.SerializerSettings.Should().BeSameAs(settings.SerializerSettings);
        }
예제 #7
0
        public void SerializerSettings_IsNull_ByDefault()
        {
            // Exercise system
            var client = new JSendClient();

            // Verify outcome
            client.SerializerSettings.Should().BeNull();
        }
예제 #8
0
        public void HttpClient_IsCorrectlyInitialized(HttpClient httpClient)
        {
            // Exercise system
            var client = new JSendClient(null, httpClient);

            // Verify outcome
            client.HttpClient.Should().BeSameAs(httpClient);
        }
예제 #9
0
        public void HttpClient_IsNotNull_ByDefault()
        {
            // Exercise system
            var client = new JSendClient();

            // Verify outcome
            client.HttpClient.Should().NotBeNull();
        }
예제 #10
0
        public void MessageInterceptor_IsCorrectlyInitialized(JSendClientSettings settings)
        {
            // Exercise system
            var client = new JSendClient(settings);

            // Verify outcome
            client.MessageInterceptor.Should().BeSameAs(settings.MessageInterceptor);
        }
예제 #11
0
 public void DoesNotParseInvalidResponses(string route, JSendClient client)
 {
     using (client)
     {
         // Exercise system and verify outcome
         client
         .Awaiting(c => c.GetAsync <User>("http://localhost/users/" + route))
         .ShouldThrow <JSendParseException>();
     }
 }
예제 #12
0
 public async Task PutsContentInEndpoint(User inputData, JSendClient client)
 {
     // Exercise system
     using (client)
         using (var response = await client.PutAsync <User>("http://localhost/users/put-echo", inputData))
         {
             // Verify outcome
             response.Data.ShouldBeEquivalentTo(inputData);
         }
 }
예제 #13
0
        public void DisposingOfTheJSendClient_DisposesOfTheHttpClient(HttpClientSpy spy)
        {
            // Fixture setup
            var client = new JSendClient(null, spy);

            // Exercise system
            client.Dispose();
            // Verify outcome
            spy.Disposed.Should().BeTrue();
        }
예제 #14
0
 public void DoesNotParseInvalidResponses(string route, JSendClient client)
 {
     using (client)
     {
         // Exercise system and verify outcome
         client
             .Awaiting(c => c.GetAsync<User>("http://localhost/users/" + route))
             .ShouldThrow<JSendParseException>();
     }
 }
 public void InterceptsResponsesBeforeBeingParsed(
     [WithInterceptor(typeof(ReplaceEmptyBodyInterceptor))] JSendClient client)
 {
     using (client)
     {
         // Exercise system and verify outcome
         client.Awaiting(c => c.GetAsync <string>("http://localhost/users/empty-body"))
         .ShouldNotThrow();
     }
 }
예제 #16
0
 public async Task ParsesSuccessResponseWithoutData(JSendClient client)
 {
     // Exercise system
     using (client)
         using (var response = await client.GetAsync <User>("http://localhost/users/success"))
         {
             // Verify outcome
             response.Status.Should().Be(JSendStatus.Success);
             response.HasData.Should().BeFalse();
         }
 }
예제 #17
0
 public async Task ParsesSuccessResponseWithoutData(JSendClient client)
 {
     // Exercise system
     using (client)
     using (var response = await client.GetAsync<User>("http://localhost/users/success"))
     {
         // Verify outcome
         response.Status.Should().Be(JSendStatus.Success);
         response.HasData.Should().BeFalse();
     }
 }
예제 #18
0
 public async Task ParsesFailResponse(JSendClient client)
 {
     // Exercise system
     using (client)
         using (var response = await client.GetAsync <User>("http://localhost/users/fail"))
         {
             // Verify outcome
             response.Status.Should().Be(JSendStatus.Fail);
             response.HasData.Should().BeFalse();
             response.Error.Data.Value <string>().Should().Be(UsersController.ErrorData);
         }
 }
예제 #19
0
        public async Task DeleteAsync_SendsPostRequest(
            [FrozenAsHttpClient] HttpClientSpy httpClientSpy,
            Uri uri, [Greedy] JSendClient client)
        {
            // Exercise system
            await client.DeleteAsync(uri);

            // Verify outcome
            var request = httpClientSpy.Request;

            request.Method.Should().Be(HttpMethod.Delete);
        }
예제 #20
0
 public async Task ParsesSuccessResponseWithData(JSendClient client)
 {
     // Exercise system
     using (client)
     using (var response = await client.GetAsync<User>("http://localhost/users/success-with-user"))
     {
         // Verify outcome
         response.Status.Should().Be(JSendStatus.Success);
         response.HasData.Should().BeTrue();
         response.Data.ShouldBeEquivalentTo(UsersController.TestUser);
     }
 }
예제 #21
0
        public async Task PutAsync_SendsPutRequest(
            [FrozenAsHttpClient] HttpClientSpy httpClientSpy,
            Uri uri, object content, [Greedy] JSendClient client)
        {
            // Exercise system
            await client.PutAsync(uri, content);

            // Verify outcome
            var request = httpClientSpy.Request;

            request.Method.Should().Be(HttpMethod.Put);
        }
예제 #22
0
        public async Task PutAsync_SetsContentTypeHeader(
            [FrozenAsHttpClient] HttpClientSpy httpClientSpy,
            Uri uri, object content, [Greedy] JSendClient client)
        {
            // Exercise system
            await client.PutAsync(uri, content);

            // Verify outcome
            var request = httpClientSpy.Request;

            request.Content.Headers.ContentType.MediaType.Should().Be("application/json");
        }
예제 #23
0
        public async Task Executes_OnSending_WithCorrectRequest(
            HttpRequestMessage request,
            [Frozen] MessageInterceptor interceptor,
            [Greedy, MockHttpClient] JSendClient client)
        {
            // Exercise system
            await client.SendAsync <object>(request);

            // Verify outcome
            Mock.Get(interceptor)
            .Verify(i => i.OnSending(request));
        }
예제 #24
0
 public async Task ParsesFailResponse(JSendClient client)
 {
     // Exercise system
     using (client)
     using (var response = await client.GetAsync<User>("http://localhost/users/fail"))
     {
         // Verify outcome
         response.Status.Should().Be(JSendStatus.Fail);
         response.HasData.Should().BeFalse();
         response.Error.Data.Value<string>().Should().Be(UsersController.ErrorData);
     }
 }
예제 #25
0
        public async Task GetAsync_SendsGetRequest(
            [FrozenAsHttpClient] HttpClientSpy httpClientSpy,
            Uri uri, [Greedy] JSendClient client)
        {
            // Exercise system
            await client.GetAsync <Model>(uri);

            // Verify outcome
            var request = httpClientSpy.Request;

            request.Method.Should().Be(HttpMethod.Get);
        }
예제 #26
0
 public async Task ParsesSuccessResponseWithData(JSendClient client)
 {
     // Exercise system
     using (client)
         using (var response = await client.GetAsync <User>("http://localhost/users/success-with-user"))
         {
             // Verify outcome
             response.Status.Should().Be(JSendStatus.Success);
             response.HasData.Should().BeTrue();
             response.Data.ShouldBeEquivalentTo(UsersController.TestUser);
         }
 }
 public async Task InterceptsRequestsBeforeBeingSent(
     [WithInterceptor(typeof(VersionHeaderInterceptor))] JSendClient client)
 {
     // Exercise system
     using (client)
         using (var response =
                    await client.GetAsync <Dictionary <string, List <string> > >("http://localhost/users/echo-headers"))
         {
             // Verify outcome
             response.Data.Should().ContainKey("Version")
             .WhichValue.Should().ContainSingle("1");
         }
 }
        public async Task InterceptsParsedResponses(
            [FrozenWithoutAutoProperties] InterceptorSpy spy,
            [WithInterceptor(typeof(InterceptorSpy))] JSendClient client)
        {
            using (client)
                using (var response = await client.GetAsync <string>("http://localhost/users/get"))
                {
                    // Exercise system and verify outcome
                    spy.ResponseParsedContext.Should().NotBeNull();

                    spy.ResponseParsedContext.As <ResponseParsedContext <string> >()
                    .JSendResponse.Should().Be(response);
                }
        }
예제 #29
0
        public void BlockingDoesNotCauseDeadlocks(JSendClient client)
        {
            // Fixture setup
            // Use a synchronization context that schedules continuations on the initial thread to mimic an UI application.
            // This scenario is also somewhat equivalent to an ASP.NET environment, where the request context is not tied to a specific thread,
            // but is limited to one thread at a time (http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html).
            Action action = () => AsyncContext.Run(() =>
            {
                client.GetAsync <string>("http://localhost/users/get").Wait();
            });

            // Exercise system and verify outcome
            action.ExecutionTime().ShouldNotExceed(3.Seconds());
        }
예제 #30
0
        public async Task PutAsync_SetsCharSet(
            [FrozenAsHttpClient] HttpClientSpy httpClientSpy,
            Uri uri, object content, [Greedy] JSendClient client)
        {
            // Fixture setup
            var expectedCharSet = client.Encoding.WebName;
            // Exercise system
            await client.PutAsync(uri, content);

            // Verify outcome
            var request = httpClientSpy.Request;

            request.Content.Headers.ContentType.CharSet.Should().Be(expectedCharSet);
        }
예제 #31
0
        public async Task PutAsync_SerializesContent(
            [FrozenAsHttpClient] HttpClientSpy httpClientSpy,
            Uri uri, Model content, [Greedy] JSendClient client)
        {
            // Fixture setup
            var expectedContent = JsonConvert.SerializeObject(content);
            // Exercise system
            await client.PutAsync(uri, content);

            // Verify outcome
            var actualContent = httpClientSpy.Content;

            actualContent.Should().Be(expectedContent);
        }
예제 #32
0
        public async Task DeleteAsync_SetsUri(
            string uri,
            Uri expectedUri,
            [FrozenAsHttpClient] HttpClientSpy httpClientSpy,
            [Greedy] JSendClient client)
        {
            // Exercise system
            await client.DeleteAsync(uri);

            // Verify outcome
            var request = httpClientSpy.Request;

            request.RequestUri.Should().Be(expectedUri);
        }
예제 #33
0
 public void HttpClientExceptionsAreWrappedAndRethrown(
     HttpRequestMessage request,
     [MockHttpClient] HttpClient httpClient,
     [Greedy] JSendClient client)
 {
     // Fixture setup
     Mock.Get(httpClient)
     .Setup(c => c.SendAsync(request, It.IsAny <CancellationToken>()))
     .Throws <HttpRequestException>();
     // Exercise system and verify outcome
     client
     .Awaiting(c => c.SendAsync <object>(request))
     .ShouldThrow <JSendRequestException>()
     .WithInnerException <HttpRequestException>();
 }
예제 #34
0
 public void ParserExceptionsBubbleUp(
     HttpRequestMessage request,
     JSendParseException exception,
     [Frozen] IJSendParser parser,
     [Greedy, MockHttpClient] JSendClient client)
 {
     // Fixture setup
     Mock.Get(parser)
     .Setup(p => p.ParseAsync <object>(It.IsAny <JsonSerializerSettings>(), It.IsAny <HttpResponseMessage>()))
     .ThrowsAsync(exception);
     // Exercise system and verify outcome
     client
     .Awaiting(c => c.SendAsync <object>(request))
     .ShouldThrow <JSendParseException>();
 }
예제 #35
0
        public async Task SendsGetRequestsToTheCorrectEndpoint(string baseAddress, string requestUri, JSendClient client)
        {
            using (client)
            {
                // Fixture setup
                client.HttpClient.BaseAddress = baseAddress == null ? null : new Uri(baseAddress);

                // Exercise system
                using (var response = await client.GetAsync<string>(requestUri))
                {
                    // Verify outcome
                    response.Status.Should().Be(JSendStatus.Success);
                    response.Data.Should().Be("get");
                }
            }
        }
예제 #36
0
 public void DisposingOfTheJSendClient_DisposesOfTheHttpClient(HttpClientSpy spy)
 {
     // Fixture setup
     var client = new JSendClient(null, spy);
     // Exercise system
     client.Dispose();
     // Verify outcome
     spy.Disposed.Should().BeTrue();
 }
예제 #37
0
 public async Task PutsContentInEndpoint(User inputData, JSendClient client)
 {
     // Exercise system
     using (client)
     using (var response = await client.PutAsync<User>("http://localhost/users/put-echo", inputData))
     {
         // Verify outcome
         response.Data.ShouldBeEquivalentTo(inputData);
     }
 }
예제 #38
0
 public void JSendParser_IsDefaultJSendParser_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.Parser.Should().BeSameAs(DefaultJSendParser.Instance);
 }
예제 #39
0
 public void HttpClient_IsCorrectlyInitialized(HttpClient httpClient)
 {
     // Exercise system
     var client = new JSendClient(null, httpClient);
     // Verify outcome
     client.HttpClient.Should().BeSameAs(httpClient);
 }
예제 #40
0
 public void SerializerSettings_IsNull_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.SerializerSettings.Should().BeNull();
 }
예제 #41
0
 public void SerializerSettings_IsCorrectlyInitialized(JSendClientSettings settings)
 {
     // Exercise system
     var client = new JSendClient(settings);
     // Verify outcome
     client.SerializerSettings.Should().BeSameAs(settings.SerializerSettings);
 }
예제 #42
0
 public void Encoding_IsNull_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.Encoding.Should().BeNull();
 }
예제 #43
0
 public void MessageInterceptor_IsNullMessageInterceptor_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.MessageInterceptor.Should().BeSameAs(NullMessageInterceptor.Instance);
 }
예제 #44
0
 public void HttpClient_IsNotNull_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.HttpClient.Should().NotBeNull();
 }
예제 #45
0
 public void MessageInterceptor_IsCorrectlyInitialized(JSendClientSettings settings)
 {
     // Exercise system
     var client = new JSendClient(settings);
     // Verify outcome
     client.MessageInterceptor.Should().BeSameAs(settings.MessageInterceptor);
 }