예제 #1
0
        public async Task Request_body_should_stay_as_is(string httpMethod)
        {
            const string text          = "you shall stay same";
            var          contentStream = GenerateStreamFromString(text);

            _testMessageHandler = new TestMessageHandler
            {
                Sender = message => new HttpResponseMessage(HttpStatusCode.OK)
            };

            _builder.Configure(app =>
            {
                app.Use((context, next) =>
                {
                    context.Request.ContentLength = contentStream.Length;
                    return(next());
                });
                app.RunProxy(
                    context => context
                    .ForwardTo("http://localhost:5000/bar/")
                    .Send());
            })
            .ConfigureServices(services => services.AddProxy(httpClientBuilder =>
            {
                httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() => _testMessageHandler);
            }));

            var server = new TestServer(_builder);
            var client = server.CreateClient();

            var requestMessage = new HttpRequestMessage(new HttpMethod(httpMethod), "http://mydomain.example")
            {
                Content = new StringContent(text)
            };

            await client.SendAsync(requestMessage);

            var sentRequest = _testMessageHandler.SentRequestMessages.First();
            var sentContent = sentRequest.Content;

            sentContent.ShouldNotBeNull();
            var sentString = await sentContent.ReadAsStringAsync();

            sentString.Length.ShouldBe(text.Length);

            server.Dispose();
            contentStream.Dispose();
        }
예제 #2
0
        public ProxyTests()
        {
            _testMessageHandler = new TestMessageHandler(req =>
            {
                var response = new HttpResponseMessage(HttpStatusCode.Created);
                response.Headers.Add("testHeader", "testHeaderValue");
                response.Content = new StringContent("Response Body");
                return(response);
            });

            _builder = new WebHostBuilder()
                       .ConfigureServices(services => services.AddProxy(httpClientBuilder =>
            {
                httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() => _testMessageHandler);
            }));
        }
예제 #3
0
        public ProxyTests()
        {
            _testMessageHandler = new TestMessageHandler
            {
                Sender = req =>
                {
                    var response = new HttpResponseMessage(HttpStatusCode.Created);
                    response.Headers.Add("testHeader", "testHeaderValue");
                    response.Content = new StringContent("Response Body");
                    return(response);
                }
            };

            _builder = new WebHostBuilder()
                       .ConfigureServices(services => services.AddProxy(options =>
            {
                options.GetMessageHandler = () => _testMessageHandler;
            }));
        }
예제 #4
0
        public async Task Request_body_should_be_empty(string httpMethod)
        {
            const string text = "you shall not be present in a response";

            _testMessageHandler = new TestMessageHandler
            {
                Sender = message => new HttpResponseMessage(HttpStatusCode.OK)
            };

            _builder.Configure(app =>
                               app.RunProxy(
                                   context => context
                                   .ForwardTo("http://localhost:5000/bar/")
                                   .Send())
                               )
            .ConfigureServices(services => services.AddProxy(httpClientBuilder =>
            {
                httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() => _testMessageHandler);
            }));

            var server = new TestServer(_builder);
            var client = server.CreateClient();

            var requestMessage = new HttpRequestMessage(new HttpMethod(httpMethod), "http://mydomain.example")
            {
                Content = new StringContent(text)
            };

            await client.SendAsync(requestMessage);

            var sentRequest = _testMessageHandler.SentRequestMessages.First();
            var sentContent = sentRequest.Content;

            sentContent.ShouldBeNull();

            server.Dispose();
        }
예제 #5
0
        public async Task Response_stream_should_not_be_Flushed_if_the_response_is_ReadyOnly()
        {
            _testMessageHandler = new TestMessageHandler
            {
                Sender = req =>
                {
                    var response = new HttpResponseMessage(HttpStatusCode.Found)
                    {
                        // Usually the response of FOUND verb comes with null stream in TestHost. At least that's been observed sometimes.
                        Content = new StreamContent(Stream.Null)
                    };
                    return(response);
                }
            };

            _builder.Configure(app => app.RunProxy(
                                   context => context
                                   .ForwardTo("http://localhost:5000/bar/")
                                   .Send()))
            .ConfigureServices(services => services.AddProxy(httpClientBuilder =>
            {
                //overwrite the registration that made in constructor with the null stream handler
                httpClientBuilder.ConfigurePrimaryHttpMessageHandler(() => _testMessageHandler);
            }));
            var        server = new TestServer(_builder);
            HttpClient client = server.CreateClient();

            var requestMessage = new HttpRequestMessage(HttpMethod.Get, "http://mydomain.example")
            {
                Content = new StringContent("Request Body")
            };

            Func <Task> send = () => client.SendAsync(requestMessage);

            send.ShouldNotThrow();
        }