| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Newtonsoft.Json; |
| | | 8 | | using WireMock.Http; |
| | | 9 | | using WireMock.Util; |
| | | 10 | | #if !USE_ASPNETCORE |
| | | 11 | | using System.Net; |
| | | 12 | | using Microsoft.Owin; |
| | | 13 | | using IResponse = Microsoft.Owin.IOwinResponse; |
| | | 14 | | #else |
| | | 15 | | using Microsoft.AspNetCore.Http; |
| | | 16 | | using IResponse = Microsoft.AspNetCore.Http.HttpResponse; |
| | | 17 | | #endif |
| | | 18 | | |
| | | 19 | | namespace WireMock.Owin.Mappers |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// OwinResponseMapper |
| | | 23 | | /// </summary> |
| | | 24 | | public class OwinResponseMapper : IOwinResponseMapper |
| | | 25 | | { |
| | 54 | 26 | | private readonly Encoding _utf8NoBom = new UTF8Encoding(false); |
| | | 27 | | |
| | | 28 | | // https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx |
| | | 29 | | #if !USE_ASPNETCORE |
| | | 30 | | private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new |
| | | 31 | | #else |
| | 1 | 32 | | private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new |
| | 1 | 33 | | #endif |
| | 20 | 34 | | { HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() } |
| | 1 | 35 | | }; |
| | | 36 | | |
| | | 37 | | /// <inheritdoc cref="IOwinResponseMapper.MapAsync"/> |
| | | 38 | | public async Task MapAsync(ResponseMessage responseMessage, IResponse response) |
| | 53 | 39 | | { |
| | 53 | 40 | | if (responseMessage == null) |
| | 1 | 41 | | { |
| | 1 | 42 | | return; |
| | | 43 | | } |
| | | 44 | | |
| | 52 | 45 | | response.StatusCode = responseMessage.StatusCode; |
| | | 46 | | |
| | 52 | 47 | | byte[] bytes = null; |
| | 52 | 48 | | if (responseMessage.BodyAsBytes != null) |
| | 1 | 49 | | { |
| | 1 | 50 | | bytes = responseMessage.BodyAsBytes; |
| | 1 | 51 | | } |
| | 51 | 52 | | else if (responseMessage.BodyAsFile != null) |
| | 0 | 53 | | { |
| | 0 | 54 | | bytes = File.ReadAllBytes(responseMessage.BodyAsFile); |
| | 0 | 55 | | } |
| | 50 | 56 | | else if (responseMessage.BodyAsJson != null) |
| | 15 | 57 | | { |
| | 15 | 58 | | Formatting formatting = responseMessage.BodyAsJsonIndented == true ? Formatting.Indented : Formatting.No |
| | 15 | 59 | | string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { F |
| | 15 | 60 | | bytes = (responseMessage.BodyEncoding ?? _utf8NoBom).GetBytes(jsonBody); |
| | 15 | 61 | | } |
| | 35 | 62 | | else if (responseMessage.Body != null) |
| | 19 | 63 | | { |
| | 18 | 64 | | bytes = (responseMessage.BodyEncoding ?? _utf8NoBom).GetBytes(responseMessage.Body); |
| | 19 | 65 | | } |
| | | 66 | | |
| | 52 | 67 | | SetResponseHeaders(responseMessage, response); |
| | | 68 | | |
| | 52 | 69 | | if (bytes != null) |
| | 35 | 70 | | { |
| | 35 | 71 | | await response.Body.WriteAsync(bytes, 0, bytes.Length); |
| | 35 | 72 | | } |
| | 53 | 73 | | } |
| | | 74 | | |
| | | 75 | | private void SetResponseHeaders(ResponseMessage responseMessage, IResponse response) |
| | 51 | 76 | | { |
| | | 77 | | // Set headers |
| | 201 | 78 | | foreach (var pair in responseMessage.Headers) |
| | 23 | 79 | | { |
| | 23 | 80 | | if (ResponseHeadersToFix.ContainsKey(pair.Key)) |
| | 19 | 81 | | { |
| | 19 | 82 | | ResponseHeadersToFix[pair.Key]?.Invoke(response, pair.Value); |
| | 19 | 83 | | } |
| | | 84 | | else |
| | 4 | 85 | | { |
| | | 86 | | #if !USE_ASPNETCORE |
| | | 87 | | // For non-NETSTANDARD, check if this response header can be added (#148) |
| | | 88 | | if (!WebHeaderCollection.IsRestricted(pair.Key, true)) |
| | | 89 | | { |
| | | 90 | | response.Headers.AppendValues(pair.Key, pair.Value.ToArray()); |
| | | 91 | | } |
| | | 92 | | #else |
| | | 93 | | // NETSTANDARD can add any header (or so it seems) |
| | 4 | 94 | | response.Headers.Append(pair.Key, pair.Value.ToArray()); |
| | | 95 | | #endif |
| | 4 | 96 | | } |
| | 23 | 97 | | } |
| | 52 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |