示例#1
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);
 }
示例#2
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);
 }
示例#3
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));
        }
示例#4
0
        public async Task Executes_OnReceived_WithCorrectContext(
            HttpRequestMessage request,
            [Frozen] HttpResponseMessage response,
            [Frozen] MessageInterceptor interceptor,
            [Greedy, MockHttpClient] JSendClient client)
        {
            // Exercise system
            await client.SendAsync <object>(request);

            // Verify outcome
            Mock.Get(interceptor)
            .Verify(i => i.OnReceived(
                        It.Is <ResponseReceivedContext>(
                            ctx => ctx.HttpRequest == request && ctx.HttpResponse == response)));
        }
示例#5
0
        public async Task SendAsync_ReturnsParsedResponse(
            HttpResponseMessage httpResponse, JSendResponse <Model> parsedResponse,
            [FrozenAsHttpClient] HttpClientStub httpClientStub,
            HttpRequestMessage request, [Greedy] JSendClient client)
        {
            // Fixture setup
            httpClientStub.ReturnOnSend = httpResponse;

            Mock.Get(client.Parser)
            .Setup(p => p.ParseAsync <Model>(It.IsAny <JsonSerializerSettings>(), httpResponse))
            .ReturnsAsync(parsedResponse);
            // Exercise system
            var response = await client.SendAsync <Model>(request);

            // Verify outcome
            response.Should().BeSameAs(parsedResponse);
        }
示例#6
0
        public async Task Executes_OnSending_BeforeSendingRequest(
            HttpRequestMessage request,
            [Frozen] MessageInterceptor interceptor,
            [FrozenAsHttpClient] HttpClientSpy httpClient,
            [Greedy] JSendClient client)
        {
            // Fixture setup
            Mock.Get(interceptor)
            .Setup(i => i.OnSending(It.IsAny <HttpRequestMessage>()))
            .Callback(
                () => httpClient.HasRequestBeenSent.Should().BeFalse());
            // Exercise system
            await client.SendAsync <object>(request);

            // Verify outcome
            Mock.Get(interceptor)
            .Verify(i => i.OnSending(It.IsAny <HttpRequestMessage>()), Times.Once);
        }
示例#7
0
        public async Task Executes_OnReceived_BeforeParsingResponse(
            HttpRequestMessage request,
            [Frozen] MessageInterceptor interceptor,
            [Frozen] IJSendParser parser,
            [Greedy, MockHttpClient] JSendClient client)
        {
            // Fixture setup
            Action verifyResponseHasntBeenParsed = () =>
                                                   Mock.Get(parser)
                                                   .Verify(
                p => p.ParseAsync <object>(It.IsAny <JsonSerializerSettings>(), It.IsAny <HttpResponseMessage>()),
                Times.Never);

            Mock.Get(interceptor)
            .Setup(i => i.OnReceived(It.IsAny <ResponseReceivedContext>()))
            .Callback(verifyResponseHasntBeenParsed);

            // Exercise system
            await client.SendAsync <object>(request);

            // Verify outcome
            Mock.Get(interceptor)
            .Verify(i => i.OnReceived(It.IsAny <ResponseReceivedContext>()), Times.Once);
        }