Example #1
0
        public async Task Request_WithValidRequest_ShouldCallSendAsyncOnHttpClient()
        {
            // Setup
            var          httpClient = Substitute.For <HttpClient, IDisposable>();
            IHttpService service    = new JsonHttpService(httpClient);

            var         httpRequestMessage = new HttpRequestMessage();
            HttpRequest request            = new HttpRequest(httpRequestMessage);

            var expectedResponse = new TestClass {
                Name = "Test class"
            };
            var originalResponse = new HttpResponseMessage
            {
                Content = new StringContent(JsonConvert.SerializeObject(expectedResponse))
            };
            var response = new HttpResponse <TestClass>();

            await response.SetResponseAsync(Guid.NewGuid(), originalResponse, default(CancellationToken));

            httpClient
            .SendAsync(httpRequestMessage, default(CancellationToken))
            .Returns(originalResponse);

            // Perform action 'Request'
            await service.RequestAsync <TestObject>(request, default(CancellationToken));

            // Assert that 'ShouldCallSendAsyncOnHttpClient' = 'WithValidRequest'
            httpClient
            .Received()
            .SendAsync(httpRequestMessage, default(CancellationToken));
        }
Example #2
0
        public void Request_WithInvalidRequest_ShouldThrowArgumentNullException()
        {
            // Setup
            var          httpClient = Substitute.For <HttpClient, IDisposable>();
            IHttpService service    = new JsonHttpService(httpClient);
            HttpRequest  request    = null;

            // Perform action 'Request'
            var exception = Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await service.RequestAsync <TestObject>(request, default(CancellationToken));
            });

            // Assert that 'ShouldThrowArgumentNullException' = 'WithInvalidRequest'
        }