| | | 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 | | #if NET45 |
| | | 8 | | using Microsoft.Owin; |
| | | 9 | | #else |
| | | 10 | | using Microsoft.AspNetCore.Http; |
| | | 11 | | using Microsoft.AspNetCore.Http.Extensions; |
| | | 12 | | using Microsoft.AspNetCore.Http.Features; |
| | | 13 | | #endif |
| | | 14 | | |
| | | 15 | | namespace WireMock.Owin |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// OwinRequestMapper |
| | | 19 | | /// </summary> |
| | | 20 | | public class OwinRequestMapper |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// MapAsync IOwinRequest to RequestMessage |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="request"></param> |
| | | 26 | | /// <returns></returns> |
| | | 27 | | public async Task<RequestMessage> MapAsync( |
| | | 28 | | #if NET45 |
| | | 29 | | IOwinRequest request |
| | | 30 | | #else |
| | | 31 | | HttpRequest request |
| | | 32 | | #endif |
| | | 33 | | ) |
| | 13 | 34 | | { |
| | | 35 | | #if NET45 |
| | 13 | 36 | | Uri url = request.Uri; |
| | | 37 | | #else |
| | | 38 | | Uri url = new Uri(request.GetEncodedUrl()); |
| | | 39 | | #endif |
| | 13 | 40 | | string verb = request.Method; |
| | | 41 | | |
| | 13 | 42 | | string bodyAsString = null; |
| | 13 | 43 | | byte[] body = null; |
| | 13 | 44 | | Encoding bodyEncoding = null; |
| | 13 | 45 | | if (request.Body != null) |
| | 13 | 46 | | { |
| | 13 | 47 | | using (var streamReader = new StreamReader(request.Body)) |
| | 13 | 48 | | { |
| | 13 | 49 | | bodyAsString = await streamReader.ReadToEndAsync(); |
| | 13 | 50 | | bodyEncoding = streamReader.CurrentEncoding; |
| | 13 | 51 | | } |
| | | 52 | | |
| | 13 | 53 | | body = bodyEncoding.GetBytes(bodyAsString); |
| | 13 | 54 | | } |
| | | 55 | | |
| | 13 | 56 | | var listenerHeaders = request.Headers; |
| | | 57 | | |
| | 13 | 58 | | var headers = new Dictionary<string, string>(); |
| | 87 | 59 | | foreach (var header in listenerHeaders) |
| | 24 | 60 | | headers.Add(header.Key, header.Value.FirstOrDefault()); |
| | | 61 | | |
| | 13 | 62 | | var cookies = new Dictionary<string, string>(); |
| | | 63 | | |
| | 39 | 64 | | foreach (var cookie in request.Cookies) |
| | 0 | 65 | | cookies.Add(cookie.Key, cookie.Value); |
| | | 66 | | |
| | 13 | 67 | | return new RequestMessage(url, verb, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTi |
| | 13 | 68 | | } |
| | | 69 | | } |
| | | 70 | | } |