예제 #1
0
        public async Task Example_with_TestServer()
        {
            var routingHandler = new RoutingMessageHandler();

            // Create the two Test Servers
            var fooWebHostBuilder = WebHost.CreateDefaultBuilder <FooStartup>(Array.Empty <string>());
            var fooTestServer     = new TestServer(fooWebHostBuilder);

            var barWebHostBuilder = WebHost.CreateDefaultBuilder <BarStartup>(Array.Empty <string>());
            var barTestServer     = new TestServer(barWebHostBuilder);

            // Register the test servers against their respective origins
            routingHandler.AddHandler("http://foo", fooTestServer.CreateHandler());
            routingHandler.AddHandler("http://bar", barTestServer.CreateHandler());

            // Configure your HttpClient with the routing handler
            var client = new HttpClient(routingHandler);

            // Requests to specific origins should be routed to the correct TestServer
            var fooResponse = await client.GetAsync("http://foo");

            (await fooResponse.Content.ReadAsStringAsync()).ShouldBe("foo");

            var barResponse = await client.GetAsync("http://bar");

            (await barResponse.Content.ReadAsStringAsync()).ShouldBe("bar");
        }
예제 #2
0
        public void When_add_duplicate_origins_then_should_throw()
        {
            var routingHandler = new RoutingMessageHandler();

            routingHandler.AddHandler("http://foo", new Handler());

            Action act = () => routingHandler.AddHandler("http://foo", new Handler());

            act.ShouldThrow <ArgumentException>();
        }
예제 #3
0
        public async Task Should_route_to_correct_handler()
        {
            var routingHandler = new RoutingMessageHandler();
            var fooHandler     = new Handler();
            var barHandler     = new Handler();

            routingHandler.AddHandler("http://foo", fooHandler);
            routingHandler.AddHandler("http://bar", barHandler);
            var client  = new HttpClient(routingHandler);
            var request = new HttpRequestMessage(HttpMethod.Get, "http://bar");

            var response = await client.SendAsync(request);

            response.StatusCode.ShouldBe(HttpStatusCode.OK);
            fooHandler.Last.ShouldBeNull();
            barHandler.Last.ShouldBe(request);
        }
예제 #4
0
        public async Task If_host_not_found_then_should_throw()
        {
            var routingHandler = new RoutingMessageHandler();

            routingHandler.AddHandler("localhost", 80, new HttpClientHandler());
            routingHandler.AddHandler("example.com", 80, new HttpClientHandler());

            var httpClient = new HttpClient(routingHandler);

            Exception exception = null;

            try
            {
                await httpClient.GetAsync("http://test");
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            exception.ShouldBeOfType <InvalidOperationException>();
        }
예제 #5
0
        public async Task Suppressed_execution_context_should_not_throw()
        {
            var routingHandler = new RoutingMessageHandler();

            ExecutionContext.SuppressFlow();
            var asyncLocal = new AsyncLocal <Guid>
            {
                Value = Guid.NewGuid()
            };
            var outside = asyncLocal.Value;
            var inside  = outside;

            var handler = new DelegateHandler(_ =>
            {
                inside = asyncLocal.Value;
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
            });

            routingHandler.AddHandler("localhost", 80, handler);
            var httpClient = new HttpClient(routingHandler);

            await httpClient.GetAsync("http://localhost");
        }