Exemplo n.º 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);
 }
Exemplo n.º 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);
 }
Exemplo n.º 3
0
        public void JSendParser_IsDefaultJSendParser_ByDefault()
        {
            // Exercise system
            var client = new JSendClient();

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

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

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

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

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

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

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

            // Verify outcome
            client.MessageInterceptor.Should().BeSameAs(settings.MessageInterceptor);
        }
Exemplo n.º 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>();
     }
 }
Exemplo n.º 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);
         }
 }
Exemplo n.º 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();
        }
Exemplo n.º 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();
     }
 }
Exemplo n.º 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();
         }
 }
Exemplo n.º 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();
     }
 }
Exemplo n.º 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);
         }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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");
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
                }
        }
Exemplo n.º 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());
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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>();
 }
Exemplo n.º 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>();
 }
Exemplo n.º 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");
                }
            }
        }
Exemplo n.º 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();
 }
Exemplo n.º 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);
     }
 }
Exemplo n.º 38
0
 public void JSendParser_IsDefaultJSendParser_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.Parser.Should().BeSameAs(DefaultJSendParser.Instance);
 }
Exemplo n.º 39
0
 public void HttpClient_IsCorrectlyInitialized(HttpClient httpClient)
 {
     // Exercise system
     var client = new JSendClient(null, httpClient);
     // Verify outcome
     client.HttpClient.Should().BeSameAs(httpClient);
 }
Exemplo n.º 40
0
 public void SerializerSettings_IsNull_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.SerializerSettings.Should().BeNull();
 }
Exemplo n.º 41
0
 public void SerializerSettings_IsCorrectlyInitialized(JSendClientSettings settings)
 {
     // Exercise system
     var client = new JSendClient(settings);
     // Verify outcome
     client.SerializerSettings.Should().BeSameAs(settings.SerializerSettings);
 }
Exemplo n.º 42
0
 public void Encoding_IsNull_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.Encoding.Should().BeNull();
 }
Exemplo n.º 43
0
 public void MessageInterceptor_IsNullMessageInterceptor_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.MessageInterceptor.Should().BeSameAs(NullMessageInterceptor.Instance);
 }
Exemplo n.º 44
0
 public void HttpClient_IsNotNull_ByDefault()
 {
     // Exercise system
     var client = new JSendClient();
     // Verify outcome
     client.HttpClient.Should().NotBeNull();
 }
Exemplo n.º 45
0
 public void MessageInterceptor_IsCorrectlyInitialized(JSendClientSettings settings)
 {
     // Exercise system
     var client = new JSendClient(settings);
     // Verify outcome
     client.MessageInterceptor.Should().BeSameAs(settings.MessageInterceptor);
 }