コード例 #1
0
            public async Task GivenAnHttpResponseMessageAndTheHttpClientFactory_GetAsync_ReturnsAFakeResponse()
            {
                // Arrange.
                const string requestUrl      = "http://www.something.com/some/website";
                const string responseData    = "I am not some Html.";
                var          messageResponse = FakeHttpMessageHandler.GetStringHttpResponseMessage(responseData);
                var          messageHandler  = new FakeHttpMessageHandler(requestUrl, messageResponse);

                var key = Guid.NewGuid().ToString();

                HttpClientFactory.AddMessageHandler(messageHandler, key: key);
                HttpResponseMessage message;
                string content;

                using (var httpClient = HttpClientFactory.GetHttpClient(key))
                {
                    // Act.
                    message = await httpClient.GetAsync(requestUrl);

                    content = await message.Content.ReadAsStringAsync();
                }

                // Assert.
                message.StatusCode.ShouldBe(HttpStatusCode.OK);
                content.ShouldBe(responseData);
            }
コード例 #2
0
            public async Task GivenTwoGetRequestsButFailedToReuseHandler_GetStringAsync_ShouldThrowAnException()
            {
                // Arrange.
                string html1;

                var messageHandler = new HttpClientHandler
                {
                    Credentials = new NetworkCredential("api", "hi there!")
                };

                var key = Guid.NewGuid().ToString();

                HttpClientFactory.AddMessageHandler(messageHandler, key); // Handler is disposed after first used.

                using (var httpClient = HttpClientFactory.GetHttpClient(key))
                {
                    html1 = await httpClient.GetStringAsync("http://www.google.com.au");
                }

                Exception exception;

                using (var httpClient = HttpClientFactory.GetHttpClient(key))
                {
                    var client = httpClient;
                    exception = Should.Throw <Exception>(
                        async() => await client.GetStringAsync("http://www.google.com.au"));
                }

                // Assert.
                html1.ShouldNotBeNullOrEmpty();
                exception.Message.ShouldStartWith("Cannot access a disposed object.");
            }
コード例 #3
0
            public async Task GivenTwoGetRequests_GetStringAsync_ReturnsSomeData()
            {
                // Arrange.
                string html1, html2;

                var messageHandler = new HttpClientHandler
                {
                    Credentials = new NetworkCredential("api", "hi there!")
                };

                var key = Guid.NewGuid().ToString();

                HttpClientFactory.AddMessageHandler(messageHandler, key, false);

                using (var httpClient = HttpClientFactory.GetHttpClient(key))
                {
                    html1 = await httpClient.GetStringAsync("http://www.google.com.au");
                }

                using (var httpClient = HttpClientFactory.GetHttpClient(key))
                {
                    html2 = await httpClient.GetStringAsync("http://www.google.com.au");
                }

                // Assert.
                html1.ShouldNotBeNullOrEmpty();
                html2.ShouldNotBeNullOrEmpty();
            }
コード例 #4
0
            public void GivenAMessageHandlerExistsByADifferentKeyIsProvided_RemoveMessageHandler_Succeeds()
            {
                // Arrange.
                var messageHandler = new HttpClientHandler
                {
                    Credentials = new NetworkCredential("api", "hi there!")
                };

                HttpClientFactory.AddMessageHandler(messageHandler, Guid.NewGuid().ToString());

                // Act & Assert.
                HttpClientFactory.RemoveMessageHandler(Guid.NewGuid().ToString());
            }
コード例 #5
0
            public void GivenAMessageHandlerExists_RemoveMessageHandler_Succeeds()
            {
                // Arrange.
                var messageHandler = new HttpClientHandler
                {
                    Credentials = new NetworkCredential("api", "hi there!")
                };

                var key = Guid.NewGuid().ToString();

                HttpClientFactory.AddMessageHandler(messageHandler, key);

                // Act & Assert.
                HttpClientFactory.RemoveMessageHandler(key);
            }
コード例 #6
0
            public async Task GivenAFewHttpResponseMessages_GetAsync_ReturnsAFakeResponse()
            {
                // Arrange.
                const string requestUrl1      = "http://www.something.com/some/website";
                const string responseData1    = "I am not some Html.";
                var          messageResponse1 = FakeHttpMessageHandler.GetStringHttpResponseMessage(responseData1);

                const string requestUrl2      = "http://www.something.com/another/site";
                const string responseData2    = "Html, I am not.";
                var          messageResponse2 = FakeHttpMessageHandler.GetStringHttpResponseMessage(responseData2);

                const string requestUrl3      = "http://www.whatever.com/";
                const string responseData3    = "<html><head><body>pew pew</body></head>";
                var          messageResponse3 = FakeHttpMessageHandler.GetStringHttpResponseMessage(responseData3);

                var messageResponses = new Dictionary <string, HttpResponseMessage>
                {
                    { requestUrl1, messageResponse1 },
                    { requestUrl2, messageResponse2 },
                    { requestUrl3, messageResponse3 }
                };

                var messageHandler = new FakeHttpMessageHandler(messageResponses);

                var key = Guid.NewGuid().ToString();

                HttpClientFactory.AddMessageHandler(messageHandler, key: key);

                HttpResponseMessage message;
                string content;

                using (var httpClient = HttpClientFactory.GetHttpClient(key))
                {
                    // Act.
                    message = await httpClient.GetAsync(requestUrl2);

                    content = await message.Content.ReadAsStringAsync();
                }

                // Assert.
                message.StatusCode.ShouldBe(HttpStatusCode.OK);
                content.ShouldBe(responseData2);
            }