mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 17:41:01 +01:00
* net451 * tests : net462 * fixed tests * fix tests * readme * Code review * LocalFileSystemHandlerTests * refactor
90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using WireMock.Util;
|
|
#if !USE_ASPNETCORE
|
|
using Microsoft.Owin;
|
|
#else
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
|
#endif
|
|
|
|
namespace WireMock.Owin
|
|
{
|
|
/// <summary>
|
|
/// OwinRequestMapper
|
|
/// </summary>
|
|
internal class OwinRequestMapper
|
|
{
|
|
/// <summary>
|
|
/// MapAsync IOwinRequest to RequestMessage
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public async Task<RequestMessage> MapAsync(
|
|
#if !USE_ASPNETCORE
|
|
IOwinRequest request
|
|
#else
|
|
HttpRequest request
|
|
#endif
|
|
)
|
|
{
|
|
#if !USE_ASPNETCORE
|
|
var urldetails = UrlUtils.Parse(request.Uri, request.PathBase);
|
|
string clientIP = request.RemoteIpAddress;
|
|
#else
|
|
var urldetails = UrlUtils.Parse(new Uri(request.GetEncodedUrl()), request.PathBase);
|
|
var connection = request.HttpContext.Connection;
|
|
string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6
|
|
? connection.RemoteIpAddress.MapToIPv4().ToString()
|
|
: connection.RemoteIpAddress.ToString();
|
|
#endif
|
|
string method = request.Method;
|
|
|
|
Dictionary<string, string[]> headers = null;
|
|
if (request.Headers.Any())
|
|
{
|
|
headers = new Dictionary<string, string[]>();
|
|
foreach (var header in request.Headers)
|
|
{
|
|
headers.Add(header.Key, header.Value);
|
|
}
|
|
}
|
|
|
|
IDictionary<string, string> cookies = null;
|
|
if (request.Cookies.Any())
|
|
{
|
|
cookies = new Dictionary<string, string>();
|
|
foreach (var cookie in request.Cookies)
|
|
{
|
|
cookies.Add(cookie.Key, cookie.Value);
|
|
}
|
|
}
|
|
|
|
BodyData body = null;
|
|
if (request.Body != null && ShouldParseBody(method))
|
|
{
|
|
body = await BodyParser.Parse(request.Body, request.ContentType);
|
|
}
|
|
|
|
return new RequestMessage(urldetails, method, clientIP, body, headers, cookies) { DateTime = DateTime.Now };
|
|
}
|
|
|
|
private bool ShouldParseBody(string method)
|
|
{
|
|
/*
|
|
HEAD - No defined body semantics.
|
|
GET - No defined body semantics.
|
|
PUT - Body supported.
|
|
POST - Body supported.
|
|
DELETE - No defined body semantics.
|
|
TRACE - Body not supported.
|
|
OPTIONS - Body supported but no semantics on usage (maybe in the future).
|
|
CONNECT - No defined body semantics
|
|
PATCH - Body supported.
|
|
*/
|
|
return new[] { "PUT", "POST", "OPTIONS", "PATCH" }.Contains(method.ToUpper());
|
|
}
|
|
}
|
|
} |