From e5b2ad0543c432ccab0b5d55a83062a53e8bb9a7 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 2 Feb 2018 15:42:25 +0100 Subject: [PATCH] SetBody (#81) --- src/WireMock.Net/Http/HttpClientHelper.cs | 64 ++++++++++++++--------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/WireMock.Net/Http/HttpClientHelper.cs b/src/WireMock.Net/Http/HttpClientHelper.cs index a2aa69c1..83e8ba2a 100644 --- a/src/WireMock.Net/Http/HttpClientHelper.cs +++ b/src/WireMock.Net/Http/HttpClientHelper.cs @@ -92,34 +92,10 @@ namespace WireMock.Http // Set both content and response headers, replacing URLs in values var headers = (httpResponseMessage.Content?.Headers.Union(httpResponseMessage.Headers) ?? Enumerable.Empty>>()).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>)) && - contentTypeHeader.Value.Any(value => value != null && value.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))) + if (httpResponseMessage.Content != null) { - 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) - { - responseMessage.BodyAsBytes = await httpResponseMessage.Content.ReadAsByteArrayAsync(); - responseMessage.Body = await httpResponseMessage.Content.ReadAsStringAsync(); - } + SetBody(httpResponseMessage.Content, contentTypeHeader, responseMessage); } foreach (var header in headers) @@ -141,5 +117,41 @@ namespace WireMock.Http return responseMessage; } + + private static async void SetBody(HttpContent content, KeyValuePair> contentTypeHeader, ResponseMessage responseMessage) + { + bool contentTypeIsDefault = contentTypeHeader.Equals(default(KeyValuePair>)); + 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(); + } + } } } \ No newline at end of file