public void Convert_WithContentLengthHeader_ContentLengthHeaderIsAddedToHttpRequestMessageContentHeaders() { var request = new ProviderServiceRequest { Method = HttpVerb.Post, Path = "/events", Headers = new Dictionary <string, string> { { "Content-Length", "12" } }, Body = "Some content" }; var httpBodyContent = new HttpBodyContent(body: request.Body, contentType: new MediaTypeHeaderValue("text/plain") { CharSet = "utf-8" }); var stringContent = new StringContent(request.Body, Encoding.UTF8, "text/plain"); var mapper = GetSubject(); _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post); _mockHttpBodyContentMapper.Convert(Arg.Any <string>(), Arg.Any <IDictionary <string, string> >()).Returns(httpBodyContent); _mockHttpContentMapper.Convert(httpBodyContent).Returns(stringContent); var result = mapper.Convert(request); Assert.Equal(request.Headers.Last().Key, result.Content.Headers.Last().Key); Assert.Equal(request.Headers.Last().Value, result.Content.Headers.Last().Value.First()); }
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 void Convert_WithContentTypeContainingParameter_ReturnsContentWithContentTypeHeader() { const string contentType = "text/plain"; const string content = "test"; NameValueHeaderValue versionParameter = new NameValueHeaderValue("version", "1"); var httpBodyContent = new HttpBodyContent( content: Encoding.UTF8.GetBytes(content), contentType: new MediaTypeHeaderValue(contentType) { CharSet = "utf-8", Parameters = { versionParameter } }); IHttpContentMapper mapper = GetSubject(); HttpContent result = mapper.Convert(httpBodyContent); Assert.Equal(contentType, result.Headers.ContentType.MediaType); Assert.Contains(versionParameter, result.Headers.ContentType.Parameters); Assert.Equal("utf-8", result.Headers.ContentType.CharSet); Assert.Equal(content, result.ReadAsStringAsync().Result); }
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); }