mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-27 19:27:42 +02:00
Version 2.x (#1359)
* Version 2.x * Setup .NET 9 * 12 * cleanup some #if for NETSTANDARD1_3 * cleanup + fix tests for net8 * openapi * NO ConfigureAwait(false) + cleanup * . * #endif * HashSet * WireMock.Net.NUnit * HttpContext * Add WebSockets (#1423) * Add WebSockets * Add tests * fix * more tests * Add tests * ... * remove IOwin * - * tests * fluent * ok * match * . * byte[] * x * func * func * byte * trans * ... * frameworks......... * jmes * xxx * sc * using var httpClient = new HttpClient(); * usings * maxRetries * up * xunit v3 * ct * --- * ct * ct2 * T Unit * WireMock.Net.TUnitTests / 10 * t unit first * --project * no tunit * t2 * --project * --project * ci - --project * publish ./test/wiremock-coverage.xml * windows * . * log * ... * log * goed * BodyType * . * . * --scenario * ... * pact * ct * . * WireMock.Net.RestClient.AwesomeAssertions (#1427) * WireMock.Net.RestClient.AwesomeAssertions * ok * atpath * fix test * sonar fixes * ports * proxy test * FIX? * --- * await Task.Delay(100, _ct); * ? * --project * Aspire: use IDistributedApplicationEventingSubscriber (#1428) * broadcast * ok * more tsts * . * Collection * up * . * 2 * remove nfluent * <VersionPrefix>2.0.0-preview-02</VersionPrefix> * ... * . * nuget icon * . * <PackageReference Include="JmesPath.Net" Version="1.1.0" /> * x * 500 * . * fix some warnings * ws
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using WireMock.Models;
|
||||
using WireMock.ResponseBuilders;
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
// 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Stef.Validation;
|
||||
using WireMock.Matchers.Request;
|
||||
@@ -25,6 +23,8 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
private readonly IRequestMatcher _requestMatcher;
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly IDateTimeUtils _dateTimeUtils;
|
||||
private readonly IGuidUtils _guidUtils;
|
||||
|
||||
private readonly bool _saveToFile;
|
||||
|
||||
private int _priority;
|
||||
@@ -37,7 +37,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
private int _timesInSameState = 1;
|
||||
private bool? _useWebhookFireAndForget;
|
||||
private double? _probability;
|
||||
private GraphQLSchemaDetails? _graphQLSchemaDetails;
|
||||
private GraphQLSchemaDetails? _graphQLSchemaDetails; // Future Use.
|
||||
|
||||
public Guid Guid { get; private set; }
|
||||
|
||||
@@ -49,16 +49,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
|
||||
public IdOrTexts? ProtoDefinition { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RespondWithAProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="registrationCallback">The registration callback.</param>
|
||||
/// <param name="requestMatcher">The request matcher.</param>
|
||||
/// <param name="settings">The WireMockServerSettings.</param>
|
||||
/// <param name="guidUtils">GuidUtils to make unit testing possible.</param>
|
||||
/// <param name="dateTimeUtils">DateTimeUtils to make unit testing possible.</param>
|
||||
/// <param name="saveToFile">Optional boolean to indicate if this mapping should be saved as static mapping file.</param>
|
||||
public RespondWithAProvider(
|
||||
internal RespondWithAProvider(
|
||||
RegistrationCallback registrationCallback,
|
||||
IRequestMatcher requestMatcher,
|
||||
WireMockServerSettings settings,
|
||||
@@ -71,6 +62,8 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
_requestMatcher = Guard.NotNull(requestMatcher);
|
||||
_settings = Guard.NotNull(settings);
|
||||
_dateTimeUtils = Guard.NotNull(dateTimeUtils);
|
||||
_guidUtils = Guard.NotNull(guidUtils);
|
||||
|
||||
_saveToFile = saveToFile;
|
||||
|
||||
Guid = guidUtils.NewGuid();
|
||||
@@ -79,6 +72,16 @@ internal class RespondWithAProvider : IRespondWithAProvider
|
||||
/// <inheritdoc />
|
||||
public void RespondWith(IResponseProvider provider)
|
||||
{
|
||||
if (provider is Response response && response.WebSocketBuilder != null)
|
||||
{
|
||||
// If the provider is a Response with a WebSocketBuilder, we need to use a WebSocketResponseProvider instead.
|
||||
provider = new WebSocketResponseProvider(
|
||||
response.WebSocketBuilder,
|
||||
_guidUtils,
|
||||
_dateTimeUtils
|
||||
);
|
||||
}
|
||||
|
||||
var mapping = new Mapping
|
||||
(
|
||||
Guid,
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using JsonConverter.Abstractions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
@@ -254,7 +252,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 +268,7 @@ public partial class WireMockServer
|
||||
#endregion
|
||||
|
||||
#region Settings
|
||||
private IResponseMessage SettingsGet(IRequestMessage requestMessage)
|
||||
private IResponseMessage SettingsGet(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
var model = new SettingsModel
|
||||
{
|
||||
@@ -295,11 +293,9 @@ public partial class WireMockServer
|
||||
WatchStaticMappings = _settings.WatchStaticMappings,
|
||||
WatchStaticMappingsInSubdirectories = _settings.WatchStaticMappingsInSubdirectories,
|
||||
|
||||
#if USE_ASPNETCORE
|
||||
AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate,
|
||||
ClientCertificateMode = _settings.ClientCertificateMode,
|
||||
CorsPolicyOptions = _settings.CorsPolicyOptions?.ToString()
|
||||
#endif
|
||||
};
|
||||
|
||||
model.ProxyAndRecordSettings = TinyMapperUtils.Instance.Map(_settings.ProxyAndRecordSettings);
|
||||
@@ -307,7 +303,7 @@ public partial class WireMockServer
|
||||
return ToJson(model);
|
||||
}
|
||||
|
||||
private IResponseMessage SettingsUpdate(IRequestMessage requestMessage)
|
||||
private IResponseMessage SettingsUpdate(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
var settings = DeserializeObject<SettingsModel>(requestMessage);
|
||||
|
||||
@@ -333,12 +329,10 @@ public partial class WireMockServer
|
||||
|
||||
InitSettings(_settings);
|
||||
|
||||
#if USE_ASPNETCORE
|
||||
if (Enum.TryParse<CorsPolicyOptions>(settings.CorsPolicyOptions, true, out var corsPolicyOptions))
|
||||
{
|
||||
_settings.CorsPolicyOptions = corsPolicyOptions;
|
||||
}
|
||||
#endif
|
||||
|
||||
WireMockMiddlewareOptionsHelper.InitFromSettings(_settings, _options, o =>
|
||||
{
|
||||
@@ -347,11 +341,9 @@ public partial class WireMockServer
|
||||
o.RequestProcessingDelay = TimeSpan.FromMilliseconds(settings.GlobalProcessingDelay.Value);
|
||||
}
|
||||
|
||||
#if USE_ASPNETCORE
|
||||
o.CorsPolicyOptions = corsPolicyOptions;
|
||||
o.ClientCertificateMode = _settings.ClientCertificateMode;
|
||||
o.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode) _settings.ClientCertificateMode;
|
||||
o.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
|
||||
#endif
|
||||
});
|
||||
|
||||
return ResponseMessageBuilder.Create(200, "Settings updated");
|
||||
@@ -359,9 +351,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");
|
||||
@@ -373,7 +365,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))
|
||||
{
|
||||
@@ -403,12 +395,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))
|
||||
{
|
||||
@@ -422,7 +414,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))
|
||||
{
|
||||
@@ -441,7 +433,7 @@ public partial class WireMockServer
|
||||
#endregion Mapping/{guid}
|
||||
|
||||
#region Mappings
|
||||
private IResponseMessage SwaggerGet(IRequestMessage requestMessage)
|
||||
private IResponseMessage SwaggerGet(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
return new ResponseMessage
|
||||
{
|
||||
@@ -455,7 +447,7 @@ public partial class WireMockServer
|
||||
};
|
||||
}
|
||||
|
||||
private IResponseMessage MappingsSave(IRequestMessage requestMessage)
|
||||
private IResponseMessage MappingsSave(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
SaveStaticMappings();
|
||||
|
||||
@@ -467,12 +459,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);
|
||||
|
||||
@@ -481,7 +473,7 @@ public partial class WireMockServer
|
||||
return ToResponseMessage(code);
|
||||
}
|
||||
|
||||
private IResponseMessage MappingsPost(IRequestMessage requestMessage)
|
||||
private IResponseMessage MappingsPost(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -508,7 +500,7 @@ public partial class WireMockServer
|
||||
}
|
||||
}
|
||||
|
||||
private IResponseMessage MappingsDelete(IRequestMessage requestMessage)
|
||||
private IResponseMessage MappingsDelete(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(requestMessage.Body))
|
||||
{
|
||||
@@ -562,7 +554,7 @@ public partial class WireMockServer
|
||||
return deletedGuids;
|
||||
}
|
||||
|
||||
private IResponseMessage MappingsReset(IRequestMessage requestMessage)
|
||||
private IResponseMessage MappingsReset(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
ResetMappings();
|
||||
|
||||
@@ -581,7 +573,7 @@ public partial class WireMockServer
|
||||
return ResponseMessageBuilder.Create(200, message);
|
||||
}
|
||||
|
||||
private IResponseMessage ReloadStaticMappings(IRequestMessage _)
|
||||
private IResponseMessage ReloadStaticMappings(HttpContext _, IRequestMessage __)
|
||||
{
|
||||
ReadStaticMappings();
|
||||
|
||||
@@ -590,11 +582,11 @@ 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))
|
||||
{
|
||||
var entry = LogEntries.SingleOrDefault(r => !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid);
|
||||
var entry = LogEntries.SingleOrDefault(r => r.RequestMessage != null && !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid);
|
||||
if (entry is { })
|
||||
{
|
||||
var model = new LogEntryMapper(_options).Map(entry);
|
||||
@@ -606,7 +598,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))
|
||||
{
|
||||
@@ -619,17 +611,17 @@ 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
|
||||
.Where(r => !r.RequestMessage.Path.StartsWith("/__admin/"))
|
||||
.Where(r => r.RequestMessage != null && !r.RequestMessage.Path.StartsWith("/__admin/"))
|
||||
.Select(logEntryMapper.Map);
|
||||
|
||||
return ToJson(result);
|
||||
}
|
||||
|
||||
private IResponseMessage RequestsDelete(IRequestMessage requestMessage)
|
||||
private IResponseMessage RequestsDelete(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
ResetLogEntries();
|
||||
|
||||
@@ -638,17 +630,17 @@ 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);
|
||||
|
||||
var request = (Request)InitRequestBuilder(requestModel);
|
||||
|
||||
var dict = new Dictionary<ILogEntry, RequestMatchResult>();
|
||||
foreach (var logEntry in LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/")))
|
||||
foreach (var logEntry in LogEntries.Where(le => le.RequestMessage != null && !le.RequestMessage.Path.StartsWith("/__admin/")))
|
||||
{
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
if (request.GetMatchingScore(logEntry.RequestMessage, requestMatchResult) > MatchScores.AlmostPerfect)
|
||||
if (request.GetMatchingScore(logEntry.RequestMessage!, requestMatchResult) > MatchScores.AlmostPerfect)
|
||||
{
|
||||
dict.Add(logEntry, requestMatchResult);
|
||||
}
|
||||
@@ -660,14 +652,14 @@ 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) &&
|
||||
Guid.TryParse(value.ToString(), out var mappingGuid)
|
||||
)
|
||||
{
|
||||
var logEntries = LogEntries.Where(le => !le.RequestMessage.Path.StartsWith("/__admin/") && le.MappingGuid == mappingGuid);
|
||||
var logEntries = LogEntries.Where(le => le.RequestMessage != null && !le.RequestMessage.Path.StartsWith("/__admin/") && le.MappingGuid == mappingGuid);
|
||||
var logEntryMapper = new LogEntryMapper(_options);
|
||||
var result = logEntries.Select(logEntryMapper.Map);
|
||||
return ToJson(result);
|
||||
@@ -678,7 +670,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
|
||||
{
|
||||
@@ -692,27 +684,27 @@ 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) :
|
||||
requestMessage.Path.Split('/').Reverse().Skip(1).First();
|
||||
Enumerable.Reverse(requestMessage.Path.Split('/')).Skip(1).First();
|
||||
|
||||
return ResetScenario(name) ?
|
||||
ResponseMessageBuilder.Create(200, "Scenario reset") :
|
||||
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();
|
||||
var name = Enumerable.Reverse(requestMessage.Path.Split('/')).Skip(1).First();
|
||||
if (!_options.Scenarios.ContainsKey(name))
|
||||
{
|
||||
ResponseMessageBuilder.Create(HttpStatusCode.NotFound, $"No scenario found by name '{name}'.");
|
||||
@@ -877,7 +869,7 @@ public partial class WireMockServer
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
private static T DeserializeObject<T>(IRequestMessage requestMessage) where T : new()
|
||||
private static T DeserializeObject<T>(IRequestMessage requestMessage)
|
||||
{
|
||||
switch (requestMessage.BodyData?.DetectedBodyType)
|
||||
{
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
|
||||
@@ -14,7 +14,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 +30,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 +50,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 +70,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);
|
||||
|
||||
@@ -105,8 +105,7 @@ public partial class WireMockServer
|
||||
/// Checks if file exists.
|
||||
/// 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 +118,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);
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Stef.Validation;
|
||||
using WireMock.Admin.Mappings;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using Stef.Validation;
|
||||
using WireMock.Matchers.Request;
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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 +44,7 @@ public partial class WireMockServer
|
||||
}
|
||||
}
|
||||
|
||||
private IResponseMessage MappingsPostWireMockOrg(IRequestMessage requestMessage)
|
||||
private IResponseMessage MappingsPostWireMockOrg(HttpContext _, IRequestMessage requestMessage)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -76,7 +74,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();
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
@@ -38,9 +36,13 @@ public partial class WireMockServer
|
||||
foreach (var log in allLogEntries)
|
||||
{
|
||||
var requestMatchResult = new RequestMatchResult();
|
||||
foreach (var matcher in matchers)
|
||||
|
||||
if (log.RequestMessage != null)
|
||||
{
|
||||
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
|
||||
foreach (var matcher in matchers)
|
||||
{
|
||||
matcher.GetMatchingScore(log.RequestMessage, requestMatchResult);
|
||||
}
|
||||
}
|
||||
|
||||
if (requestMatchResult.AverageTotalScore > MatchScores.AlmostPerfect)
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
// 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 +21,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 +42,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
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
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 +32,7 @@ public partial class WireMockServer
|
||||
proxyRespondProvider.AtPriority(WireMockConstants.ProxyPriority);
|
||||
}
|
||||
|
||||
if(settings.ProxyAndRecordSettings.ProxyAll)
|
||||
if (settings.ProxyAndRecordSettings.ProxyAll)
|
||||
{
|
||||
proxyRespondProvider.AtPriority(int.MinValue);
|
||||
}
|
||||
@@ -41,7 +40,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);
|
||||
|
||||
97
src/WireMock.Net.Minimal/Server/WireMockServer.WebSocket.cs
Normal file
97
src/WireMock.Net.Minimal/Server/WireMockServer.WebSocket.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.WebSockets;
|
||||
|
||||
namespace WireMock.Server;
|
||||
|
||||
public partial class WireMockServer
|
||||
{
|
||||
/// <summary>
|
||||
/// Get all active WebSocket connections
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public IReadOnlyCollection<WireMockWebSocketContext> GetWebSocketConnections()
|
||||
{
|
||||
return _options.WebSocketRegistries.Values
|
||||
.SelectMany(r => r.GetConnections())
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get WebSocket connections for a specific mapping
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public IReadOnlyCollection<WireMockWebSocketContext> GetWebSocketConnections(Guid mappingGuid)
|
||||
{
|
||||
return _options.WebSocketRegistries.TryGetValue(mappingGuid, out var registry) ? registry.GetConnections() : [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Close a specific WebSocket connection
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public async Task AbortWebSocketConnectionAsync(Guid connectionId, string statusDescription = "Closed by server", CancellationToken cancellationToken = default)
|
||||
{
|
||||
foreach (var registry in _options.WebSocketRegistries.Values)
|
||||
{
|
||||
if (registry.TryGetConnection(connectionId, out var connection))
|
||||
{
|
||||
connection.Abort(statusDescription);
|
||||
registry.RemoveConnection(connectionId);
|
||||
|
||||
await Task.Delay(100, cancellationToken); // Give the connection some time to close gracefully
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Broadcast a text message to all WebSocket connections in a specific mapping
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public async Task BroadcastToWebSocketsAsync(Guid mappingGuid, string text, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_options.WebSocketRegistries.TryGetValue(mappingGuid, out var registry))
|
||||
{
|
||||
await registry.BroadcastAsync(text, null, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Broadcast a text message to all WebSocket connections
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public async Task BroadcastToAllWebSocketsAsync(string text, CancellationToken cancellationToken = default)
|
||||
{
|
||||
foreach (var registry in _options.WebSocketRegistries.Values)
|
||||
{
|
||||
await registry.BroadcastAsync(text, null, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Broadcast a binary message to all WebSocket connections in a specific mapping
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public async Task BroadcastToWebSocketsAsync(Guid mappingGuid, byte[] bytes, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_options.WebSocketRegistries.TryGetValue(mappingGuid, out var registry))
|
||||
{
|
||||
await registry.BroadcastAsync(bytes, null, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Broadcast a binary message to all WebSocket connections
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public async Task BroadcastToAllWebSocketsAsync(byte[] bytes, CancellationToken cancellationToken = default)
|
||||
{
|
||||
foreach (var registry in _options.WebSocketRegistries.Values)
|
||||
{
|
||||
await registry.BroadcastAsync(bytes, null, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,10 @@
|
||||
|
||||
// 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;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using AnyOfTypes;
|
||||
using JetBrains.Annotations;
|
||||
using JsonConverter.Newtonsoft.Json;
|
||||
@@ -40,7 +37,7 @@ public partial class WireMockServer : IWireMockServer
|
||||
private const int ServerStartDelayInMs = 100;
|
||||
|
||||
private readonly WireMockServerSettings _settings;
|
||||
private readonly IOwinSelfHost? _httpServer;
|
||||
private readonly AspNetCoreSelfHost? _httpServer;
|
||||
private readonly IWireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||
private readonly MappingConverter _mappingConverter;
|
||||
private readonly MatcherMapper _matcherMapper;
|
||||
@@ -64,7 +61,7 @@ public partial class WireMockServer : IWireMockServer
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public int Port => Ports?.FirstOrDefault() ?? default;
|
||||
public int Port => Ports?.FirstOrDefault() ?? 0;
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
@@ -415,11 +412,12 @@ public partial class WireMockServer : IWireMockServer
|
||||
_dateTimeUtils
|
||||
);
|
||||
|
||||
#if USE_ASPNETCORE
|
||||
_options.AdditionalServiceRegistration = _settings.AdditionalServiceRegistration;
|
||||
_options.CorsPolicyOptions = _settings.CorsPolicyOptions;
|
||||
_options.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode)_settings.ClientCertificateMode;
|
||||
_options.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
|
||||
|
||||
_httpServer = new AspNetCoreSelfHost(_options, urlOptions);
|
||||
#else
|
||||
_httpServer = new OwinSelfHost(_options, urlOptions);
|
||||
#endif
|
||||
var startTask = _httpServer.StartAsync();
|
||||
|
||||
using (var ctsStartTimeout = new CancellationTokenSource(settings.StartTimeout))
|
||||
@@ -474,7 +472,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" />
|
||||
@@ -540,15 +538,11 @@ public partial class WireMockServer : IWireMockServer
|
||||
Guard.NotNull(tenant);
|
||||
Guard.NotNull(audience);
|
||||
|
||||
#if NETSTANDARD1_3
|
||||
throw new NotSupportedException("AzureADAuthentication is not supported for NETStandard 1.3");
|
||||
#else
|
||||
_options.AuthenticationMatcher = new AzureADAuthenticationMatcher(
|
||||
new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler(),
|
||||
new Microsoft.IdentityModel.Protocols.ConfigurationManager<Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfiguration>($"https://login.microsoftonline.com/{tenant}/.well-known/openid-configuration", new Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever()),
|
||||
tenant,
|
||||
audience);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IWireMockServer.SetBasicAuthentication(string, string)" />
|
||||
|
||||
Reference in New Issue
Block a user