Skip to content

Commit

Permalink
SetBody (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Feb 2, 2018
1 parent 66b2ff1 commit e5b2ad0
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/WireMock.Net/Http/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,10 @@ public static async Task<ResponseMessage> SendAsync(HttpClient client, RequestMe

// Set both content and response headers, replacing URLs in values
var headers = (httpResponseMessage.Content?.Headers.Union(httpResponseMessage.Headers) ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>()).ToArray();

// In case the Content-Type header is application/json, try to set BodyAsJson, else set Body and BodyAsBytes.
bool bodyAsJson = false;
var contentTypeHeader = headers.FirstOrDefault(header => string.Equals(header.Key, HttpKnownHeaderNames.ContentType, StringComparison.OrdinalIgnoreCase));
if (!contentTypeHeader.Equals(default(KeyValuePair<string, IEnumerable<string>>)) &&
contentTypeHeader.Value.Any(value => value != null && value.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)))
{
if (httpResponseMessage.Content != null)
{
string content = await httpResponseMessage.Content.ReadAsStringAsync();
try
{
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(content, new JsonSerializerSettings { Formatting = Formatting.Indented });
bodyAsJson = true;
}
catch
{
}
}
}

if (!bodyAsJson)
if (httpResponseMessage.Content != null)
{
if (httpResponseMessage.Content != null)
{
responseMessage.BodyAsBytes = await httpResponseMessage.Content.ReadAsByteArrayAsync();
responseMessage.Body = await httpResponseMessage.Content.ReadAsStringAsync();
}
SetBody(httpResponseMessage.Content, contentTypeHeader, responseMessage);
}

foreach (var header in headers)
Expand All @@ -141,5 +117,41 @@ public static async Task<ResponseMessage> SendAsync(HttpClient client, RequestMe

return responseMessage;
}

private static async void SetBody(HttpContent content, KeyValuePair<string, IEnumerable<string>> contentTypeHeader, ResponseMessage responseMessage)
{
bool contentTypeIsDefault = contentTypeHeader.Equals(default(KeyValuePair<string, IEnumerable<string>>));
string[] textContentTypes = { "text/", "application/xml", "application/javascript", "application/typescript", "application/xhtml+xml" };

if (!contentTypeIsDefault && contentTypeHeader.Value.Any(value => textContentTypes.Any(t => value != null && value.StartsWith(t, StringComparison.OrdinalIgnoreCase))))
{
try
{
responseMessage.Body = await content.ReadAsStringAsync();
}
catch
{
// Reading as string failed, just get the ByteArray.
responseMessage.BodyAsBytes = await content.ReadAsByteArrayAsync();
}
}
else if (!contentTypeIsDefault && contentTypeHeader.Value.Any(value => value != null && value.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)))
{
string stringContent = await content.ReadAsStringAsync();
try
{
responseMessage.BodyAsJson = JsonConvert.DeserializeObject(stringContent, new JsonSerializerSettings { Formatting = Formatting.Indented });
}
catch
{
// JsonConvert failed, just set the Body as string.
responseMessage.Body = stringContent;
}
}
else
{
responseMessage.BodyAsBytes = await content.ReadAsByteArrayAsync();
}
}
}
}

0 comments on commit e5b2ad0

Please sign in to comment.