HttpContext

This commit is contained in:
Stef Heyenrath
2026-02-08 19:19:19 +01:00
parent 88df9af9df
commit dff55e175b
40 changed files with 236 additions and 231 deletions

View File

@@ -2,6 +2,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Stef.Validation;
using WireMock.Matchers.Request;
using WireMock.Models;
@@ -145,9 +146,9 @@ public class Mapping : IMapping
}
/// <inheritdoc />
public Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IRequestMessage requestMessage)
public Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(HttpContext context, IRequestMessage requestMessage)
{
return Provider.ProvideResponseAsync(this, requestMessage, Settings);
return Provider.ProvideResponseAsync(this, context, requestMessage, Settings);
}
/// <inheritdoc />

View File

@@ -23,7 +23,6 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
private readonly IWireMockLogger _logger;
private readonly HostUrlOptions _urlOptions;
private Exception _runningException;
private IWebHost _host;
public bool IsStarted { get; private set; }
@@ -32,7 +31,7 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
public List<int> Ports { get; } = new();
public Exception RunningException => _runningException;
public Exception? RunningException { get; private set; }
public AspNetCoreSelfHost(IWireMockMiddlewareOptions wireMockMiddlewareOptions, HostUrlOptions urlOptions)
{
@@ -136,7 +135,7 @@ internal partial class AspNetCoreSelfHost : IOwinSelfHost
}
catch (Exception e)
{
_runningException = e;
RunningException = e;
_logger.Error(e.ToString());
IsStarted = false;

View File

@@ -9,12 +9,6 @@ using Microsoft.AspNetCore.Http.Extensions;
using WireMock.Http;
using WireMock.Models;
using WireMock.Util;
//#if !USE_ASPNETCORE
//using IRequest = Microsoft.Owin.IOwinRequest;
//#else
//using Microsoft.AspNetCore.Http.Extensions;
//using IRequest = Microsoft.AspNetCore.Http.HttpRequest;
//#endif
namespace WireMock.Owin.Mappers;
@@ -76,10 +70,8 @@ internal class OwinRequestMapper : IOwinRequestMapper
headers,
cookies,
httpVersion,
//#if USE_ASPNETCORE
await request.HttpContext.Connection.GetClientCertificateAsync()
//#endif
)
)
{
DateTime = DateTime.UtcNow
};
@@ -87,10 +79,6 @@ internal class OwinRequestMapper : IOwinRequestMapper
private static (UrlDetails UrlDetails, string ClientIP) ParseRequest(HttpRequest request)
{
//#if !USE_ASPNETCORE
// var urlDetails = UrlUtils.Parse(request.Uri, request.PathBase);
// var clientIP = request.RemoteIpAddress;
//#else
var urlDetails = UrlUtils.Parse(new Uri(request.GetEncodedUrl()), request.PathBase);
var connection = request.HttpContext.Connection;
@@ -107,7 +95,7 @@ internal class OwinRequestMapper : IOwinRequestMapper
{
clientIP = connection.RemoteIpAddress.ToString();
}
//#endif
return (urlDetails, clientIP);
}
}

View File

@@ -18,13 +18,6 @@ using WireMock.ResponseBuilders;
using WireMock.Types;
using WireMock.Util;
//#if !USE_ASPNETCORE
//using IResponse = Microsoft.Owin.IOwinResponse;
//#else
//using Microsoft.AspNetCore.Http;
//using IResponse = Microsoft.AspNetCore.Http.HttpResponse;
//#endif
namespace WireMock.Owin.Mappers
{
/// <summary>
@@ -260,11 +253,7 @@ namespace WireMock.Owin.Mappers
private static void AppendResponseHeader(HttpResponse response, string headerName, string[] values)
{
//#if !USE_ASPNETCORE
// response.Headers.AppendValues(headerName, values);
//#else
response.Headers.Append(headerName, values);
//#endif
}
}
}

View File

@@ -141,7 +141,7 @@ internal class WireMockMiddleware
await Task.Delay(_options.RequestProcessingDelay.Value).ConfigureAwait(false);
}
var (theResponse, theOptionalNewMapping) = await targetMapping.ProvideResponseAsync(request).ConfigureAwait(false);
var (theResponse, theOptionalNewMapping) = await targetMapping.ProvideResponseAsync(ctx, request).ConfigureAwait(false);
response = theResponse;
var responseBuilder = targetMapping.Provider as Response;

View File

@@ -4,16 +4,16 @@
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Stef.Validation;
using WireMock.Proxy;
using WireMock.RequestBuilders;
using WireMock.Settings;
using WireMock.Transformers;
using WireMock.Transformers.Handlebars;
using WireMock.Transformers.Scriban;
using WireMock.Types;
using WireMock.Util;
@@ -187,8 +187,10 @@ public partial class Response : IResponseBuilder
}
/// <inheritdoc />
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings)
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, HttpContext context, IRequestMessage requestMessage, WireMockServerSettings settings)
{
Guard.NotNull(mapping);
Guard.NotNull(context);
Guard.NotNull(requestMessage);
Guard.NotNull(settings);

View File

@@ -2,6 +2,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Stef.Validation;
using WireMock.Settings;
@@ -9,15 +10,16 @@ namespace WireMock.ResponseProviders;
internal class DynamicAsyncResponseProvider : IResponseProvider
{
private readonly Func<IRequestMessage, Task<IResponseMessage>> _responseMessageFunc;
private readonly Func<HttpContext, IRequestMessage, Task<IResponseMessage>> _responseMessageFunc;
public DynamicAsyncResponseProvider(Func<IRequestMessage, Task<IResponseMessage>> responseMessageFunc)
public DynamicAsyncResponseProvider(Func<HttpContext, IRequestMessage, Task<IResponseMessage>> responseMessageFunc)
{
_responseMessageFunc = Guard.NotNull(responseMessageFunc);
}
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings)
/// <inheritdoc />
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, HttpContext context, IRequestMessage requestMessage, WireMockServerSettings settings)
{
return (await _responseMessageFunc(requestMessage).ConfigureAwait(false), null);
return (await _responseMessageFunc(context, requestMessage).ConfigureAwait(false), null);
}
}

View File

@@ -2,6 +2,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Stef.Validation;
using WireMock.Settings;
@@ -9,16 +10,17 @@ namespace WireMock.ResponseProviders;
internal class DynamicResponseProvider : IResponseProvider
{
private readonly Func<IRequestMessage, IResponseMessage> _responseMessageFunc;
private readonly Func<HttpContext, IRequestMessage, IResponseMessage> _responseMessageFunc;
public DynamicResponseProvider(Func<IRequestMessage, IResponseMessage> responseMessageFunc)
public DynamicResponseProvider(Func<HttpContext, IRequestMessage, IResponseMessage> responseMessageFunc)
{
_responseMessageFunc = Guard.NotNull(responseMessageFunc);
}
public Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings)
/// <inheritdoc />
public Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, HttpContext context, IRequestMessage requestMessage, WireMockServerSettings settings)
{
(IResponseMessage responseMessage, IMapping? mapping) result = (_responseMessageFunc(requestMessage), null);
(IResponseMessage responseMessage, IMapping? mapping) result = (_responseMessageFunc(context, requestMessage), null);
return Task.FromResult(result);
}
}

View File

@@ -2,6 +2,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Stef.Validation;
using WireMock.Settings;
@@ -9,17 +10,18 @@ namespace WireMock.ResponseProviders;
internal class ProxyAsyncResponseProvider : IResponseProvider
{
private readonly Func<IRequestMessage, WireMockServerSettings, Task<IResponseMessage>> _responseMessageFunc;
private readonly Func<HttpContext, IRequestMessage, WireMockServerSettings, Task<IResponseMessage>> _responseMessageFunc;
private readonly WireMockServerSettings _settings;
public ProxyAsyncResponseProvider(Func<IRequestMessage, WireMockServerSettings, Task<IResponseMessage>> responseMessageFunc, WireMockServerSettings settings)
public ProxyAsyncResponseProvider(Func<HttpContext, IRequestMessage, WireMockServerSettings, Task<IResponseMessage>> responseMessageFunc, WireMockServerSettings settings)
{
_responseMessageFunc = Guard.NotNull(responseMessageFunc);
_settings = Guard.NotNull(settings);
}
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings)
/// <inheritdoc />
public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, HttpContext context, IRequestMessage requestMessage, WireMockServerSettings settings)
{
return (await _responseMessageFunc(requestMessage, _settings).ConfigureAwait(false), null);
return (await _responseMessageFunc(context, requestMessage, _settings).ConfigureAwait(false), null);
}
}

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Net;
using System.Text;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stef.Validation;
@@ -254,7 +255,7 @@ public partial class WireMockServer
#endregion
#region Health
private static IResponseMessage HealthGet(IRequestMessage requestMessage)
private static IResponseMessage HealthGet(HttpContext _, IRequestMessage requestMessage)
{
return new ResponseMessage
{
@@ -270,7 +271,7 @@ public partial class WireMockServer
#endregion
#region Settings
private IResponseMessage SettingsGet(IRequestMessage requestMessage)
private IResponseMessage SettingsGet(HttpContext _, IRequestMessage requestMessage)
{
var model = new SettingsModel
{
@@ -305,7 +306,7 @@ public partial class WireMockServer
return ToJson(model);
}
private IResponseMessage SettingsUpdate(IRequestMessage requestMessage)
private IResponseMessage SettingsUpdate(HttpContext _, IRequestMessage requestMessage)
{
var settings = DeserializeObject<SettingsModel>(requestMessage);
@@ -357,9 +358,9 @@ public partial class WireMockServer
#endregion Settings
#region Mapping/{guid}
private IResponseMessage MappingGet(IRequestMessage requestMessage)
private IResponseMessage MappingGet(HttpContext context, IRequestMessage requestMessage)
{
var mapping = FindMappingByGuid(requestMessage);
var mapping = FindMappingByGuid(context, requestMessage);
if (mapping == null)
{
_settings.Logger.Warn("HttpStatusCode set to 404 : Mapping not found");
@@ -371,7 +372,7 @@ public partial class WireMockServer
return ToJson(model);
}
private IResponseMessage MappingCodeGet(IRequestMessage requestMessage)
private IResponseMessage MappingCodeGet(HttpContext context, IRequestMessage requestMessage)
{
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
{
@@ -401,12 +402,12 @@ public partial class WireMockServer
return defaultValue;
}
private IMapping? FindMappingByGuid(IRequestMessage requestMessage)
private IMapping? FindMappingByGuid(HttpContext _, IRequestMessage requestMessage)
{
return TryParseGuidFromRequestMessage(requestMessage, out var guid) ? Mappings.FirstOrDefault(m => !m.IsAdminInterface && m.Guid == guid) : null;
}
private IResponseMessage MappingPut(IRequestMessage requestMessage)
private IResponseMessage MappingPut(HttpContext _, IRequestMessage requestMessage)
{
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
{
@@ -420,7 +421,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create(HttpStatusCode.NotFound, "Mapping not found");
}
private IResponseMessage MappingDelete(IRequestMessage requestMessage)
private IResponseMessage MappingDelete(HttpContext _, IRequestMessage requestMessage)
{
if (TryParseGuidFromRequestMessage(requestMessage, out var guid) && DeleteMapping(guid))
{
@@ -439,7 +440,7 @@ public partial class WireMockServer
#endregion Mapping/{guid}
#region Mappings
private IResponseMessage SwaggerGet(IRequestMessage requestMessage)
private IResponseMessage SwaggerGet(HttpContext _, IRequestMessage requestMessage)
{
return new ResponseMessage
{
@@ -453,7 +454,7 @@ public partial class WireMockServer
};
}
private IResponseMessage MappingsSave(IRequestMessage requestMessage)
private IResponseMessage MappingsSave(HttpContext _, IRequestMessage requestMessage)
{
SaveStaticMappings();
@@ -465,12 +466,12 @@ public partial class WireMockServer
return _mappingBuilder.GetMappings();
}
private IResponseMessage MappingsGet(IRequestMessage requestMessage)
private IResponseMessage MappingsGet(HttpContext _, IRequestMessage requestMessage)
{
return ToJson(ToMappingModels());
}
private IResponseMessage MappingsCodeGet(IRequestMessage requestMessage)
private IResponseMessage MappingsCodeGet(HttpContext _, IRequestMessage requestMessage)
{
var converterType = GetEnumFromQuery(requestMessage, MappingConverterType.Server);
@@ -479,7 +480,7 @@ public partial class WireMockServer
return ToResponseMessage(code);
}
private IResponseMessage MappingsPost(IRequestMessage requestMessage)
private IResponseMessage MappingsPost(HttpContext _, IRequestMessage requestMessage)
{
try
{
@@ -506,7 +507,7 @@ public partial class WireMockServer
}
}
private IResponseMessage MappingsDelete(IRequestMessage requestMessage)
private IResponseMessage MappingsDelete(HttpContext _, IRequestMessage requestMessage)
{
if (!string.IsNullOrEmpty(requestMessage.Body))
{
@@ -560,7 +561,7 @@ public partial class WireMockServer
return deletedGuids;
}
private IResponseMessage MappingsReset(IRequestMessage requestMessage)
private IResponseMessage MappingsReset(HttpContext _, IRequestMessage requestMessage)
{
ResetMappings();
@@ -579,7 +580,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create(200, message);
}
private IResponseMessage ReloadStaticMappings(IRequestMessage _)
private IResponseMessage ReloadStaticMappings(HttpContext _, IRequestMessage __)
{
ReadStaticMappings();
@@ -588,7 +589,7 @@ public partial class WireMockServer
#endregion Mappings
#region Request/{guid}
private IResponseMessage RequestGet(IRequestMessage requestMessage)
private IResponseMessage RequestGet(HttpContext _, IRequestMessage requestMessage)
{
if (TryParseGuidFromRequestMessage(requestMessage, out var guid))
{
@@ -604,7 +605,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create(HttpStatusCode.NotFound, "Request not found");
}
private IResponseMessage RequestDelete(IRequestMessage requestMessage)
private IResponseMessage RequestDelete(HttpContext _, IRequestMessage requestMessage)
{
if (TryParseGuidFromRequestMessage(requestMessage, out var guid) && DeleteLogEntry(guid))
{
@@ -617,7 +618,7 @@ public partial class WireMockServer
#endregion Request/{guid}
#region Requests
private IResponseMessage RequestsGet(IRequestMessage requestMessage)
private IResponseMessage RequestsGet(HttpContext _, IRequestMessage requestMessage)
{
var logEntryMapper = new LogEntryMapper(_options);
var result = LogEntries
@@ -627,7 +628,7 @@ public partial class WireMockServer
return ToJson(result);
}
private IResponseMessage RequestsDelete(IRequestMessage requestMessage)
private IResponseMessage RequestsDelete(HttpContext _, IRequestMessage requestMessage)
{
ResetLogEntries();
@@ -636,7 +637,7 @@ public partial class WireMockServer
#endregion Requests
#region Requests/find
private IResponseMessage RequestsFind(IRequestMessage requestMessage)
private IResponseMessage RequestsFind(HttpContext _, IRequestMessage requestMessage)
{
var requestModel = DeserializeObject<RequestModel>(requestMessage);
@@ -658,7 +659,7 @@ public partial class WireMockServer
return ToJson(result);
}
private IResponseMessage RequestsFindByMappingGuid(IRequestMessage requestMessage)
private IResponseMessage RequestsFindByMappingGuid(HttpContext _, IRequestMessage requestMessage)
{
if (requestMessage.Query != null &&
requestMessage.Query.TryGetValue("mappingGuid", out var value) &&
@@ -676,7 +677,7 @@ public partial class WireMockServer
#endregion Requests/find
#region Scenarios
private IResponseMessage ScenariosGet(IRequestMessage requestMessage)
private IResponseMessage ScenariosGet(HttpContext _, IRequestMessage requestMessage)
{
var scenariosStates = Scenarios.Values.Select(s => new ScenarioStateModel
{
@@ -690,14 +691,14 @@ public partial class WireMockServer
return ToJson(scenariosStates, true);
}
private IResponseMessage ScenariosReset(IRequestMessage requestMessage)
private IResponseMessage ScenariosReset(HttpContext _, IRequestMessage requestMessage)
{
ResetScenarios();
return ResponseMessageBuilder.Create(200, "Scenarios reset");
}
private IResponseMessage ScenarioReset(IRequestMessage requestMessage)
private IResponseMessage ScenarioReset(HttpContext _, IRequestMessage requestMessage)
{
var name = string.Equals(HttpRequestMethod.DELETE, requestMessage.Method, StringComparison.OrdinalIgnoreCase) ?
requestMessage.Path.Substring(_adminPaths!.Scenarios.Length + 1) :
@@ -708,7 +709,7 @@ public partial class WireMockServer
ResponseMessageBuilder.Create(HttpStatusCode.NotFound, $"No scenario found by name '{name}'.");
}
private IResponseMessage ScenariosSetState(IRequestMessage requestMessage)
private IResponseMessage ScenariosSetState(HttpContext _, IRequestMessage requestMessage)
{
var name = requestMessage.Path.Split('/').Reverse().Skip(1).First();
if (!_options.Scenarios.ContainsKey(name))

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.AspNetCore.Http;
using WireMock.Types;
using WireMock.Util;
@@ -14,7 +15,7 @@ public partial class WireMockServer
private static readonly Encoding[] FileBodyIsString = [Encoding.UTF8, Encoding.ASCII];
#region ProtoDefinitions/{id}
private IResponseMessage ProtoDefinitionAdd(IRequestMessage requestMessage)
private IResponseMessage ProtoDefinitionAdd(HttpContext _, IRequestMessage requestMessage)
{
if (requestMessage.Body is null)
{
@@ -30,7 +31,7 @@ public partial class WireMockServer
#endregion
#region Files/{filename}
private IResponseMessage FilePost(IRequestMessage requestMessage)
private IResponseMessage FilePost(HttpContext _, IRequestMessage requestMessage)
{
if (requestMessage.BodyAsBytes is null)
{
@@ -50,7 +51,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create(HttpStatusCode.OK, "File created");
}
private IResponseMessage FilePut(IRequestMessage requestMessage)
private IResponseMessage FilePut(HttpContext _, IRequestMessage requestMessage)
{
if (requestMessage.BodyAsBytes is null)
{
@@ -70,7 +71,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create(HttpStatusCode.OK, "File updated");
}
private IResponseMessage FileGet(IRequestMessage requestMessage)
private IResponseMessage FileGet(HttpContext _, IRequestMessage requestMessage)
{
var filename = GetFileNameFromRequestMessage(requestMessage);
@@ -106,7 +107,7 @@ public partial class WireMockServer
/// Note: Response is returned with no body as a head request doesn't accept a body, only the status code.
/// </summary>
/// <param name="requestMessage">The request message.</param>
private IResponseMessage FileHead(IRequestMessage requestMessage)
private IResponseMessage FileHead(HttpContext _, IRequestMessage requestMessage)
{
var filename = GetFileNameFromRequestMessage(requestMessage);
@@ -119,7 +120,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create(HttpStatusCode.NoContent);
}
private IResponseMessage FileDelete(IRequestMessage requestMessage)
private IResponseMessage FileDelete(HttpContext _, IRequestMessage requestMessage)
{
var filename = GetFileNameFromRequestMessage(requestMessage);

View File

@@ -6,12 +6,13 @@ using System.IO;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using Stef.Validation;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Util;
using Stef.Validation;
using OrgMapping = WireMock.Org.Abstractions.Mapping;
namespace WireMock.Server;
@@ -46,7 +47,7 @@ public partial class WireMockServer
}
}
private IResponseMessage MappingsPostWireMockOrg(IRequestMessage requestMessage)
private IResponseMessage MappingsPostWireMockOrg(HttpContext _, IRequestMessage requestMessage)
{
try
{
@@ -76,7 +77,7 @@ public partial class WireMockServer
}
}
private Guid? ConvertWireMockOrgMappingAndRegisterAsRespondProvider(Org.Abstractions.Mapping mapping, Guid? guid = null, string? path = null)
private Guid? ConvertWireMockOrgMappingAndRegisterAsRespondProvider(OrgMapping mapping, Guid? guid = null, string? path = null)
{
var requestBuilder = Request.Create();

View File

@@ -1,19 +1,17 @@
// Copyright © WireMock.Net
using System.Net;
//#if OPENAPIPARSER
using System;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Http;
using WireMock.Net.OpenApiParser;
//#endif
namespace WireMock.Server;
public partial class WireMockServer
{
private IResponseMessage OpenApiConvertToMappings(IRequestMessage requestMessage)
private IResponseMessage OpenApiConvertToMappings(HttpContext _, IRequestMessage requestMessage)
{
//#if OPENAPIPARSER
try
{
var mappingModels = new WireMockOpenApiParser().FromText(requestMessage.Body!, out var diagnostic);
@@ -24,14 +22,10 @@ public partial class WireMockServer
_settings.Logger.Error("HttpStatusCode set to {0} {1}", HttpStatusCode.BadRequest, e);
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest, e.Message);
}
//#else
// return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest, "Not supported for .NETStandard 1.3 and .NET 4.6.x or lower.");
//#endif
}
private IResponseMessage OpenApiSaveToMappings(IRequestMessage requestMessage)
private IResponseMessage OpenApiSaveToMappings(HttpContext _, IRequestMessage requestMessage)
{
//#if OPENAPIPARSER
try
{
var mappingModels = new WireMockOpenApiParser().FromText(requestMessage.Body!, out var diagnostic);
@@ -49,8 +43,5 @@ public partial class WireMockServer
_settings.Logger.Error("HttpStatusCode set to {0} {1}", HttpStatusCode.BadRequest, e);
return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest, e.Message);
}
//#else
// return ResponseMessageBuilder.Create(HttpStatusCode.BadRequest, "Not supported for .NETStandard 1.3 and .NET 4.6.x or lower.");
//#endif
}
}

View File

@@ -3,6 +3,7 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using WireMock.Constants;
using WireMock.Http;
using WireMock.Proxy;
@@ -33,7 +34,7 @@ public partial class WireMockServer
proxyRespondProvider.AtPriority(WireMockConstants.ProxyPriority);
}
if(settings.ProxyAndRecordSettings.ProxyAll)
if (settings.ProxyAndRecordSettings.ProxyAll)
{
proxyRespondProvider.AtPriority(int.MinValue);
}
@@ -41,7 +42,7 @@ public partial class WireMockServer
proxyRespondProvider.RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings));
}
private async Task<IResponseMessage> ProxyAndRecordAsync(IRequestMessage requestMessage, WireMockServerSettings settings)
private async Task<IResponseMessage> ProxyAndRecordAsync(HttpContext _, IRequestMessage requestMessage, WireMockServerSettings settings)
{
var requestUri = new Uri(requestMessage.Url);
var proxyUri = new Uri(settings.ProxyAndRecordSettings!.Url);

View File

@@ -471,7 +471,7 @@ public partial class WireMockServer : IWireMockServer
Given(Request.Create().WithPath("/*").UsingAnyMethod())
.WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05"))
.AtPriority(1000)
.RespondWith(new DynamicResponseProvider(_ => ResponseMessageBuilder.Create(HttpStatusCode.NotFound, WireMockConstants.NoMatchingFound)));
.RespondWith(new DynamicResponseProvider((_, _) => ResponseMessageBuilder.Create(HttpStatusCode.NotFound, WireMockConstants.NoMatchingFound)));
}
/// <inheritdoc cref="IWireMockServer.Reset" />

View File

@@ -2,6 +2,8 @@
using System;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Http;
using WireMock.Matchers.Request;
using WireMock.Models;
using WireMock.ResponseProviders;
@@ -146,9 +148,10 @@ public interface IMapping
/// <summary>
/// ProvideResponseAsync
/// </summary>
/// <param name="context">The HttpContext.</param>
/// <param name="requestMessage">The request message.</param>
/// <returns>The <see cref="IResponseMessage"/> including a new (optional) <see cref="IMapping"/>.</returns>
Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IRequestMessage requestMessage);
Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(HttpContext context, IRequestMessage requestMessage);
/// <summary>
/// Gets the RequestMatchResult based on the RequestMessage.

View File

@@ -3,6 +3,7 @@
// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using WireMock.Settings;
namespace WireMock.ResponseProviders;
@@ -16,8 +17,9 @@ public interface IResponseProvider
/// The provide response.
/// </summary>
/// <param name="mapping">The used mapping.</param>
/// <param name="context">The HttpContext.</param>
/// <param name="requestMessage">The request.</param>
/// <param name="settings">The WireMockServerSettings.</param>
/// <returns>The <see cref="IResponseMessage"/> including a new (optional) <see cref="IMapping"/>.</returns>
Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings);
Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, HttpContext context, IRequestMessage requestMessage, WireMockServerSettings settings);
}

View File

@@ -28,6 +28,7 @@
<ItemGroup>
<!-- Keep at 6.14.0 -->
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.9" />
<PackageReference Include="Polyfill" Version="6.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>