| | | 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.Util; |
| | | 9 | | #if !NETSTANDARD |
| | | 10 | | using Microsoft.Owin; |
| | | 11 | | #else |
| | | 12 | | using Microsoft.AspNetCore.Http; |
| | | 13 | | #endif |
| | | 14 | | |
| | | 15 | | namespace WireMock.Owin |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// OwinResponseMapper |
| | | 19 | | /// </summary> |
| | | 20 | | public class OwinResponseMapper |
| | | 21 | | { |
| | 94 | 22 | | private readonly Encoding _utf8NoBom = new UTF8Encoding(false); |
| | | 23 | | |
| | | 24 | | // https://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest |
| | | 25 | | #if !NETSTANDARD |
| | 1 | 26 | | private static readonly IDictionary<string, Action<IOwinResponse, WireMockList<string>>> RestrictedResponseHeade |
| | 1 | 27 | | #else |
| | 1 | 28 | | private static readonly IDictionary<string, Action<HttpResponse, WireMockList<string>>> RestrictedResponseHeader |
| | 1 | 29 | | #endif |
| | 1 | 30 | | { "Content-Length", null }, |
| | 8 | 31 | | { "Content-Type", (r, v) => r.ContentType = v.FirstOrDefault() }, |
| | 1 | 32 | | { "Keep-Alive", null }, |
| | 1 | 33 | | { "Transfer-Encoding", null }, |
| | 1 | 34 | | { "WWW-Authenticate", null } |
| | 1 | 35 | | }; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// MapAsync ResponseMessage to OwinResponse |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="responseMessage"></param> |
| | | 41 | | /// <param name="response"></param> |
| | | 42 | | public async Task MapAsync(ResponseMessage responseMessage |
| | | 43 | | #if !NETSTANDARD |
| | | 44 | | , IOwinResponse response |
| | | 45 | | #else |
| | | 46 | | , HttpResponse response |
| | | 47 | | #endif |
| | | 48 | | ) |
| | 58 | 49 | | { |
| | 58 | 50 | | response.StatusCode = responseMessage.StatusCode; |
| | | 51 | | |
| | | 52 | | // Set headers |
| | 254 | 53 | | foreach (var pair in responseMessage.Headers) |
| | 40 | 54 | | { |
| | 40 | 55 | | if (RestrictedResponseHeaders.ContainsKey(pair.Key)) |
| | 15 | 56 | | { |
| | 15 | 57 | | RestrictedResponseHeaders[pair.Key]?.Invoke(response, pair.Value); |
| | 15 | 58 | | } |
| | | 59 | | else |
| | 25 | 60 | | { |
| | | 61 | | #if !NETSTANDARD |
| | 25 | 62 | | response.Headers.AppendValues(pair.Key, pair.Value.ToArray()); |
| | | 63 | | #else |
| | | 64 | | response.Headers.Append(pair.Key, pair.Value.ToArray()); |
| | | 65 | | #endif |
| | 25 | 66 | | } |
| | 40 | 67 | | } |
| | | 68 | | |
| | 58 | 69 | | if (responseMessage.Body == null && responseMessage.BodyAsBytes == null && responseMessage.BodyAsFile == nul |
| | 18 | 70 | | { |
| | 18 | 71 | | return; |
| | | 72 | | } |
| | | 73 | | |
| | 40 | 74 | | if (responseMessage.BodyAsBytes != null) |
| | 7 | 75 | | { |
| | 7 | 76 | | await response.Body.WriteAsync(responseMessage.BodyAsBytes, 0, responseMessage.BodyAsBytes.Length); |
| | 7 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | |
| | 33 | 80 | | if (responseMessage.BodyAsFile != null) |
| | 0 | 81 | | { |
| | 0 | 82 | | byte[] bytes = File.ReadAllBytes(responseMessage.BodyAsFile); |
| | | 83 | | |
| | 0 | 84 | | await response.Body.WriteAsync(bytes, 0, bytes.Length); |
| | 0 | 85 | | return; |
| | | 86 | | } |
| | | 87 | | |
| | 33 | 88 | | if (responseMessage.BodyAsJson != null) |
| | 2 | 89 | | { |
| | 2 | 90 | | string jsonBody = JsonConvert.SerializeObject(responseMessage.BodyAsJson, new JsonSerializerSettings { F |
| | 2 | 91 | | using (var writer = new StreamWriter(response.Body, responseMessage.BodyEncoding ?? _utf8NoBom)) |
| | 2 | 92 | | { |
| | 2 | 93 | | await writer.WriteAsync(jsonBody); |
| | 2 | 94 | | } |
| | | 95 | | |
| | 2 | 96 | | return; |
| | | 97 | | } |
| | | 98 | | |
| | 31 | 99 | | using (var writer = new StreamWriter(response.Body, responseMessage.BodyEncoding ?? _utf8NoBom)) |
| | 31 | 100 | | { |
| | 31 | 101 | | await writer.WriteAsync(responseMessage.Body); |
| | 31 | 102 | | } |
| | 58 | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |