Пример #1
0
        public void SendAdminHttpRequest <T>(HttpVerb method, string path, T requestContent) where T : class
        {
            if (_host == null)
            {
                throw new InvalidOperationException("Unable to perform operation because the mock provider service is not running.");
            }

            var responseContent = String.Empty;

            var request = new HttpRequestMessage(_httpMethodMapper.Convert(method), path);

            request.Headers.Add(Constants.AdministrativeRequestHeaderKey, "true");

            if (requestContent != null)
            {
                var requestContentJson = JsonConvert.SerializeObject(requestContent, JsonConfig.ApiSerializerSettings);
                request.Content = new StringContent(requestContentJson, Encoding.UTF8, "application/json");
            }

            var response           = _httpClient.SendAsync(request, CancellationToken.None).Result;
            var responseStatusCode = response.StatusCode;

            if (response.Content != null)
            {
                responseContent = response.Content.ReadAsStringAsync().Result;
            }

            Dispose(request);
            Dispose(response);

            if (responseStatusCode != HttpStatusCode.OK)
            {
                throw new PactFailureException(responseContent);
            }
        }
Пример #2
0
        public HttpRequestMessage Convert(ProviderServiceRequest from)
        {
            if (from == null)
            {
                return(null);
            }

            var requestHttpMethod = _httpMethodMapper.Convert(from.Method);
            var requestPath       = from.PathWithQuery();

            var to = new HttpRequestMessage(requestHttpMethod, requestPath);

            var contentRelatedHeaders = new Dictionary <string, string>();

            if (from.Headers != null && from.Headers.Any())
            {
                foreach (var requestHeader in from.Headers)
                {
                    //Strip any Content- headers as they need to be attached to Request content when using a HttpRequestMessage
                    if (requestHeader.Key.IndexOf("Content-", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        contentRelatedHeaders.Add(requestHeader.Key, requestHeader.Value);
                        continue;
                    }

                    to.Headers.Add(requestHeader.Key, requestHeader.Value);
                }
            }

            if (from.Body != null)
            {
                HttpBodyContent bodyContent = _httpBodyContentMapper.Convert(body: from.Body, headers: from.Headers);
                var             httpContent = _httpContentMapper.Convert(bodyContent);

                //Set the content related headers
                if (httpContent != null && contentRelatedHeaders.Any())
                {
                    foreach (var contentHeader in contentRelatedHeaders)
                    {
                        if (contentHeader.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase) &&
                            httpContent.Headers.Any(x => x.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase)))
                        {
                            continue;
                        }

                        httpContent.Headers.Add(contentHeader.Key, contentHeader.Value);
                    }
                }

                to.Content = httpContent;
            }

            return(to);
        }
        public async Task <string> SendAdminHttpRequest <T>(HttpVerb method, string path, T requestContent, IDictionary <string, string> headers = null) where T : class
        {
            var request = new HttpRequestMessage(_httpMethodMapper.Convert(method), path);

            request.Headers.Add(Constants.AdministrativeRequestHeaderKey, "true");

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }
            }

            if (requestContent != null)
            {
                var requestContentJson = JsonConvert.SerializeObject(requestContent, _jsonSerializerSettings);
                request.Content = new StringContent(requestContentJson, Encoding.UTF8, "application/json");
            }

            var response = await _httpClient.SendAsync(request, CancellationToken.None);

            var responseStatusCode = response.StatusCode;
            var responseContent    = Empty;

            if (response.Content != null)
            {
                responseContent = await response.Content.ReadAsStringAsync();
            }

            Dispose(request);
            Dispose(response);

            if (responseStatusCode != HttpStatusCode.OK)
            {
                throw new PactFailureException(responseContent);
            }

            return(!string.IsNullOrEmpty(responseContent) ? responseContent : Empty);
        }
        public HttpRequestMessage Convert(ProviderServiceRequest from)
        {
            if (from == null)
            {
                return(null);
            }

            var requestHttpMethod = _httpMethodMapper.Convert(from.Method);
            var requestPath       = from.PathWithQuery();

            var to = new HttpRequestMessage(requestHttpMethod, requestPath);

            if (from.Headers != null && from.Headers.Any())
            {
                foreach (var requestHeader in from.Headers)
                {
                    //TODO: Check if there are any other headers which need special treatment
                    //Handle the content-type header as little differently, as they need to be attached to the content when using a HttpRequestMessage
                    //Strip the Content-Length header as is automatically attached to the request
                    if (requestHeader.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase) ||
                        requestHeader.Key.Equals("Content-Length", StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    to.Headers.Add(requestHeader.Key, requestHeader.Value);
                }
            }

            if (from.Body != null)
            {
                HttpBodyContent bodyContent = _httpBodyContentMapper.Convert(from.Body, from.Headers);
                to.Content = _httpContentMapper.Convert(bodyContent);
            }

            return(to);
        }
        public void Convert_WithRequest_CallsHttpMethodMapper()
        {
            var request = new ProviderServiceRequest
            {
                Method = HttpVerb.Post,
                Path   = "/events"
            };

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);

            mapper.Convert(request);

            _mockHttpMethodMapper.Received(1).Convert(request.Method);
        }