mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-26 19:11:04 +01:00
Update BodyParser logic (#212)
* Update BodyParser logic * update logic for byte[] * small update * MyGetKey * myget * dotnet nuget push * dotnet build * Release * . * StringContent * 1.0.4.18-preview-02 * Debug * 1.0.4.18-preview-02 * disable some proxy tests * myget * packagesToPack * fix * <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> * Release * <VersionPrefix>1.0.4.18</VersionPrefix> * fix * BodyParserTests * ResponseBodyData (#216) * ResponseBodyData * refactor tests * LogEntryMapperTests
This commit is contained in:
@@ -2,16 +2,21 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using MimeKit;
|
||||
using Newtonsoft.Json;
|
||||
using WireMock.Util;
|
||||
using WireMock.Validation;
|
||||
|
||||
namespace WireMock.Http
|
||||
{
|
||||
internal static class HttpRequestMessageHelper
|
||||
{
|
||||
public static HttpRequestMessage Create(RequestMessage requestMessage, string url)
|
||||
internal static HttpRequestMessage Create([NotNull] RequestMessage requestMessage, [NotNull] string url)
|
||||
{
|
||||
Check.NotNull(requestMessage, nameof(requestMessage));
|
||||
Check.NotNullOrEmpty(url, nameof(url));
|
||||
|
||||
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
|
||||
|
||||
ContentType contentType = null;
|
||||
@@ -21,40 +26,25 @@ namespace WireMock.Http
|
||||
ContentType.TryParse(value, out contentType);
|
||||
}
|
||||
|
||||
// Set Body if present
|
||||
if (requestMessage.BodyAsBytes != null)
|
||||
switch (requestMessage.BodyData?.DetectedBodyType)
|
||||
{
|
||||
httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes);
|
||||
}
|
||||
else if (requestMessage.BodyAsJson != null)
|
||||
{
|
||||
if (contentType != null)
|
||||
{
|
||||
var encoding = requestMessage.BodyEncoding ?? Encoding.GetEncoding(contentType.Charset ?? "UTF-8");
|
||||
httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestMessage.BodyAsJson), encoding, contentType.MimeType);
|
||||
}
|
||||
else
|
||||
{
|
||||
httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestMessage.BodyAsJson), requestMessage.BodyEncoding);
|
||||
}
|
||||
}
|
||||
else if (requestMessage.Body != null)
|
||||
{
|
||||
if (contentType != null)
|
||||
{
|
||||
var encoding = requestMessage.BodyEncoding ?? Encoding.GetEncoding(contentType.Charset ?? "UTF-8");
|
||||
httpRequestMessage.Content = new StringContent(requestMessage.Body, encoding, contentType.MimeType);
|
||||
}
|
||||
else
|
||||
{
|
||||
httpRequestMessage.Content = new StringContent(requestMessage.Body, requestMessage.BodyEncoding);
|
||||
}
|
||||
case BodyType.Bytes:
|
||||
httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyData.BodyAsBytes);
|
||||
break;
|
||||
|
||||
case BodyType.Json:
|
||||
httpRequestMessage.Content = StringContentHelper.Create(JsonConvert.SerializeObject(requestMessage.BodyData.BodyAsJson), contentType);
|
||||
break;
|
||||
|
||||
case BodyType.String:
|
||||
httpRequestMessage.Content = StringContentHelper.Create(requestMessage.BodyData.BodyAsString, contentType);
|
||||
break;
|
||||
}
|
||||
|
||||
// Overwrite the host header
|
||||
httpRequestMessage.Headers.Host = new Uri(url).Authority;
|
||||
|
||||
// Set other headers if present and if not excluded
|
||||
// Set other headers if present
|
||||
if (requestMessage.Headers == null || requestMessage.Headers.Count == 0)
|
||||
{
|
||||
return httpRequestMessage;
|
||||
@@ -63,6 +53,7 @@ namespace WireMock.Http
|
||||
var excludeHeaders = new List<string> { HttpKnownHeaderNames.Host, HttpKnownHeaderNames.ContentLength };
|
||||
if (contentType != null)
|
||||
{
|
||||
// Content-Type should be set on the content
|
||||
excludeHeaders.Add(HttpKnownHeaderNames.ContentType);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,10 +24,7 @@ namespace WireMock.Http
|
||||
contentTypeHeader = headers.First(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase)).Value;
|
||||
}
|
||||
|
||||
var body = await BodyParser.Parse(stream, contentTypeHeader?.FirstOrDefault());
|
||||
responseMessage.Body = body.BodyAsString;
|
||||
responseMessage.BodyAsJson = body.BodyAsJson;
|
||||
responseMessage.BodyAsBytes = body.BodyAsBytes;
|
||||
responseMessage.BodyData = await BodyParser.Parse(stream, contentTypeHeader?.FirstOrDefault());
|
||||
}
|
||||
|
||||
foreach (var header in headers)
|
||||
|
||||
38
src/WireMock.Net/Http/StringContentHelper.cs
Normal file
38
src/WireMock.Net/Http/StringContentHelper.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using MimeKit;
|
||||
using WireMock.Validation;
|
||||
|
||||
namespace WireMock.Http
|
||||
{
|
||||
internal static class StringContentHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a StringContent object. Note that the Encoding is only set when it's also set on the original header.
|
||||
/// </summary>
|
||||
/// <param name="content">The string content (cannot be null)</param>
|
||||
/// <param name="contentType">The ContentType (can be null)</param>
|
||||
/// <returns>StringContent</returns>
|
||||
internal static StringContent Create([NotNull] string content, [CanBeNull] ContentType contentType)
|
||||
{
|
||||
Check.NotNull(content, nameof(content));
|
||||
|
||||
if (contentType == null)
|
||||
{
|
||||
return new StringContent(content);
|
||||
}
|
||||
|
||||
if (contentType.Charset == null)
|
||||
{
|
||||
var stringContent = new StringContent(content);
|
||||
stringContent.Headers.ContentType = new MediaTypeHeaderValue(contentType.MimeType);
|
||||
return stringContent;
|
||||
}
|
||||
|
||||
var encoding = Encoding.GetEncoding(contentType.Charset);
|
||||
return new StringContent(content, encoding, contentType.MimeType);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user