private async Task <HttpRequestMessage> GetRequestFor <T>(Func <T, Task> action)
        {
            HttpRequestMessage request        = null;
            string             requestContent = null;
            var mockHandler = new MockHttpMessageHandler();

            mockHandler.When("*").Respond(async r =>
            {
                request        = r;
                requestContent = await r.Content.ReadAsStringAsync();
                return(HttpResponseFactory.GetResponse(content: "''"));
            });
            var kernel = new TestingKernel <ConsoleLog>();
            var client = kernel
                         .Get <ServiceProxyProviderSpy <T> >(new ConstructorArgument("httpMessageHandler", mockHandler))
                         .Client;

            await action(client);

            var contentClone = new StringContent(requestContent, Encoding.UTF8, "application/json");

            foreach (KeyValuePair <string, IEnumerable <string> > header in request.Content.Headers.Where(h => h.Key.StartsWith("X")))
            {
                contentClone.Headers.Add(header.Key, header.Value);
            }

            kernel.Dispose();

            return(new HttpRequestMessage(request.Method, request.RequestUri)
            {
                Content = contentClone
            });
        }
Пример #2
0
        public async Task For_ServiceProxy_TakeDefaultSlot()
        {
            var kernel           = SetUpKernel(new ServiceArguments(slotNumber: 5));
            var serviceProxyFunc = kernel.Get <Func <string, ServiceProxyProvider> >();
            var serviceProxy     = serviceProxyFunc(TestingKernel <ConsoleLog> .APPNAME);
            var handlerMock      = new MockHttpMessageHandler();

            handlerMock.When("*").Respond(req =>
            {
                req.RequestUri.Port.ShouldBe(40001);
                return(HttpResponseFactory.GetResponse(content: "null"));
            });

            serviceProxy.HttpMessageHandler = handlerMock;
            await serviceProxy.Invoke(new HttpServiceRequest("myMethod", new Dictionary <string, object>()), typeof(int?));
        }
        private async Task <HttpRequestMessage> GetRequestFor <T>(Func <T, Task> action)
        {
            HttpRequestMessage request        = null;
            string             requestContent = null;
            Func <HttpClientConfiguration, HttpMessageHandler> messageHandlerFactory = _ =>
            {
                var mockHandler = new MockHttpMessageHandler();
                mockHandler.When("*").Respond(async r =>
                {
                    if (r.Method != HttpMethod.Get)
                    {
                        request        = r;
                        requestContent = await r.Content.ReadAsStringAsync();
                    }

                    return(HttpResponseFactory.GetResponse(content: "''"));
                });

                return(mockHandler);
            };
            var kernel = new TestingKernel <ConsoleLog>();

            kernel.Rebind <Func <HttpClientConfiguration, HttpMessageHandler> >().ToMethod(c => messageHandlerFactory);
            var client = kernel.Get <ServiceProxyProviderSpy <T> >();

            client.DefaultPort = _testinghost.BasePort;

            await action(client.Client);

            var contentClone = new StringContent(requestContent, Encoding.UTF8, "application/json");

            foreach (KeyValuePair <string, IEnumerable <string> > header in request.Content.Headers.Where(h =>
                                                                                                          h.Key.StartsWith("X")))
            {
                contentClone.Headers.Add(header.Key, header.Value);
            }

            kernel.Dispose();

            return(new HttpRequestMessage(request.Method, request.RequestUri)
            {
                Content = contentClone
            });
        }