public void When_create_Then_returns_plain_http_request()
        {
            //Arrange
            var plainRouteConfiguration = new PlainRouteConfiguration
            {
                Target = new PlainRouteTargetConfiguration
                {
                    Addresses = new[]
                    {
                        new PlainRouteTargetAddressConfiguration
                        {
                            Host = Host,
                            Port = Port
                        }
                    },
                    Scheme = Scheme
                },
                Source = new PlainRouteSourceConfiguration()
            };

            //Act
            var result = this.sut.Create(HttpMethods.Get, Path, QueryString, new HeaderDictionary(0), TimeoutInSeconds, plainRouteConfiguration);

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result.Headers, Is.Not.Null);
            Assert.That(result.Method, Is.EqualTo(HttpMethods.Get));
            Assert.That(result.Scheme, Is.EqualTo(Scheme));
            Assert.That(result.Host, Is.EqualTo(Host));
            Assert.That(result.Port, Is.EqualTo(Port));
            Assert.That(result.Path, Is.EqualTo(null));
            Assert.That(result.QueryString, Is.EqualTo(QueryString));
            Assert.That(result.TimeoutInSeconds, Is.EqualTo(TimeoutInSeconds));
        }
        private void ValidateParameters(string method, IHeaderDictionary headers, PlainRouteConfiguration routeConfiguration)
        {
            if (string.IsNullOrWhiteSpace(method))
            {
                throw new ArgumentNullException(nameof(method));
            }

            if (headers == null)
            {
                throw new ArgumentNullException(nameof(headers));
            }

            if (routeConfiguration == null)
            {
                throw new ArgumentNullException(nameof(routeConfiguration));
            }
        }
        public void When_get_matching_And_matching_route_found_Then_returns_plain_route_configuration()
        {
            //Arrange
            var configuration = new PlainRouteConfiguration
            {
                Source = new PlainRouteSourceConfiguration
                {
                    HttpMethods  = new[] { "POST" },
                    PathTemplate = "/post"
                },
                Target = new PlainRouteTargetConfiguration
                {
                    Addresses = new[]
                    {
                        new PlainRouteTargetAddressConfiguration
                        {
                            Host = "localhost",
                            Port = 36
                        }
                    }
                }
            };

            var configurations = new List <PlainRouteConfiguration> {
                configuration
            };

            var defaultHttpContext = new DefaultHttpContext();

            defaultHttpContext.Request.Method = "POST";
            defaultHttpContext.Request.Path   = "/post/1";

            //Act
            var result = this.sut.GetMatching(configurations, defaultHttpContext.Request);

            //Assert
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.EqualTo(configuration));
        }
        public PlainHttpRequest Create(
            string method,
            string path,
            string queryString,
            IHeaderDictionary headers,
            ushort?timeoutInSeconds,
            PlainRouteConfiguration routeConfiguration)
        {
            this.ValidateParameters(method, headers, routeConfiguration);

            var address = routeConfiguration.Target.Addresses.First();

            var httpPath = this.httpRequestPathProvider.Get(
                path,
                routeConfiguration.Source.PathTemplate,
                routeConfiguration.Target.PathTemplate);

            var request = new PlainHttpRequest
            {
                Method      = method,
                Scheme      = routeConfiguration.Target.Scheme,
                Host        = address.Host,
                Port        = address.Port,
                Path        = httpPath,
                QueryString = queryString ?? string.Empty,
                Headers     = headers,
                // ReSharper disable once PossibleInvalidOperationException
                TimeoutInSeconds = timeoutInSeconds.Value
            };

            this.logger.LogDebug(
                "{method} request has been created for path {path} and query string {queryString}",
                request.Method.ToUpper(),
                request.Path,
                request.QueryString);

            return(request);
        }