mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-24 10:22:13 +01:00
AllowAnyHttpStatusCodeInResponse (#407)
* . * , * PUBLISH_TESTRESULTS * fix logging * fix compile error * codefactor fix * Debug - Sonar + other things in csproj
This commit is contained in:
@@ -49,10 +49,11 @@ namespace WireMock.Logging
|
||||
/// <summary>
|
||||
/// Writes the message at the Error level using the specified exception.
|
||||
/// </summary>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="formatString">The format string.</param>
|
||||
/// <param name="exception">The exception.</param>
|
||||
[PublicAPI]
|
||||
void Error([NotNull] string message, [NotNull] Exception exception);
|
||||
[StringFormatMethod("formatString")]
|
||||
void Error([NotNull] string formatString, [NotNull] Exception exception);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the LogEntryModel (LogRequestModel, LogResponseModel and more).
|
||||
|
||||
@@ -8,7 +8,6 @@ using JetBrains.Annotations;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.HttpsCertificate;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Owin.Mappers;
|
||||
@@ -70,7 +69,6 @@ namespace WireMock.Owin
|
||||
_host = builder
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddSingleton(_options.FileSystemHandler);
|
||||
services.AddSingleton(_options);
|
||||
services.AddSingleton<IMappingMatcher, MappingMatcher>();
|
||||
services.AddSingleton<IOwinRequestMapper, OwinRequestMapper>();
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace WireMock.Owin
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_options.Logger.Error("HttpStatusCode set to 500", ex);
|
||||
_options.Logger.Error("HttpStatusCode set to 500 {0}", ex);
|
||||
await _responseMapper.MapAsync(ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500), ctx.Response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,5 +39,7 @@ namespace WireMock.Owin
|
||||
IFileSystemHandler FileSystemHandler { get; set; }
|
||||
|
||||
bool? AllowBodyForAllHttpMethods { get; set; }
|
||||
|
||||
bool? AllowAnyHttpStatusCodeInResponse { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using RandomDataGenerator.FieldOptions;
|
||||
using RandomDataGenerator.Randomizers;
|
||||
using WireMock.Exceptions;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Http;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using WireMock.Validation;
|
||||
#if !USE_ASPNETCORE
|
||||
using IResponse = Microsoft.Owin.IOwinResponse;
|
||||
@@ -24,11 +25,11 @@ namespace WireMock.Owin.Mappers
|
||||
/// <summary>
|
||||
/// OwinResponseMapper
|
||||
/// </summary>
|
||||
public class OwinResponseMapper : IOwinResponseMapper
|
||||
internal class OwinResponseMapper : IOwinResponseMapper
|
||||
{
|
||||
private readonly IRandomizerNumber<double> _randomizerDouble = RandomizerFactory.GetRandomizer(new FieldOptionsDouble { Min = 0, Max = 1 });
|
||||
private readonly IRandomizerBytes _randomizerBytes = RandomizerFactory.GetRandomizer(new FieldOptionsBytes { Min = 100, Max = 200 });
|
||||
private readonly IFileSystemHandler _fileSystemHandler;
|
||||
private readonly IWireMockMiddlewareOptions _options;
|
||||
private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
|
||||
|
||||
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
|
||||
@@ -43,12 +44,12 @@ namespace WireMock.Owin.Mappers
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="fileSystemHandler">The IFileSystemHandler.</param>
|
||||
public OwinResponseMapper(IFileSystemHandler fileSystemHandler)
|
||||
/// <param name="options">The IWireMockMiddlewareOptions.</param>
|
||||
public OwinResponseMapper(IWireMockMiddlewareOptions options)
|
||||
{
|
||||
Check.NotNull(fileSystemHandler, nameof(fileSystemHandler));
|
||||
Check.NotNull(options, nameof(options));
|
||||
|
||||
_fileSystemHandler = fileSystemHandler;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IOwinResponseMapper.MapAsync"/>
|
||||
@@ -83,11 +84,13 @@ namespace WireMock.Owin.Mappers
|
||||
switch (responseMessage.StatusCode)
|
||||
{
|
||||
case int statusCodeAsInteger:
|
||||
response.StatusCode = statusCodeAsInteger;
|
||||
response.StatusCode = MapStatusCode(statusCodeAsInteger);
|
||||
break;
|
||||
|
||||
case string statusCodeAsString:
|
||||
response.StatusCode = int.Parse(statusCodeAsString);
|
||||
// Note: this case will also match on null
|
||||
int.TryParse(statusCodeAsString, out int result);
|
||||
response.StatusCode = MapStatusCode(result);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -99,6 +102,16 @@ namespace WireMock.Owin.Mappers
|
||||
}
|
||||
}
|
||||
|
||||
private int MapStatusCode(int code)
|
||||
{
|
||||
if (_options.AllowAnyHttpStatusCodeInResponse == true || Enum.IsDefined(typeof(HttpStatusCode), code))
|
||||
{
|
||||
return code;
|
||||
}
|
||||
|
||||
return (int)HttpStatusCode.OK;
|
||||
}
|
||||
|
||||
private bool IsFault(ResponseMessage responseMessage)
|
||||
{
|
||||
return responseMessage.FaultPercentage == null || _randomizerDouble.Generate() <= responseMessage.FaultPercentage;
|
||||
@@ -126,7 +139,7 @@ namespace WireMock.Owin.Mappers
|
||||
break;
|
||||
|
||||
case BodyType.File:
|
||||
bytes = _fileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile);
|
||||
bytes = _options.FileSystemHandler.ReadResponseBodyAsFile(responseMessage.BodyData.BodyAsFile);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Owin.Mappers;
|
||||
using WireMock.Util;
|
||||
@@ -76,7 +75,7 @@ namespace WireMock.Owin
|
||||
try
|
||||
{
|
||||
var requestMapper = new OwinRequestMapper();
|
||||
var responseMapper = new OwinResponseMapper(_options.FileSystemHandler);
|
||||
var responseMapper = new OwinResponseMapper(_options);
|
||||
var matcher = new MappingMatcher(_options);
|
||||
|
||||
Action<IAppBuilder> startup = app =>
|
||||
|
||||
@@ -42,5 +42,8 @@ namespace WireMock.Owin
|
||||
|
||||
/// <inheritdoc cref="IWireMockMiddlewareOptions.AllowBodyForAllHttpMethods"/>
|
||||
public bool? AllowBodyForAllHttpMethods { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IWireMockMiddlewareOptions.AllowAnyHttpStatusCodeInResponse"/>
|
||||
public bool? AllowAnyHttpStatusCodeInResponse { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -60,7 +60,7 @@ namespace WireMock.ResponseBuilders
|
||||
[PublicAPI]
|
||||
public static IResponseBuilder Create([CanBeNull] ResponseMessage responseMessage = null)
|
||||
{
|
||||
var message = responseMessage ?? new ResponseMessage { StatusCode = (int)HttpStatusCode.OK };
|
||||
var message = responseMessage ?? new ResponseMessage(); // { StatusCode = (int)HttpStatusCode.OK };
|
||||
return new Response(message);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace WireMock
|
||||
/// <summary>
|
||||
/// Gets or sets the status code.
|
||||
/// </summary>
|
||||
public object StatusCode { get; set; } = 200;
|
||||
public object StatusCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the body.
|
||||
|
||||
@@ -491,12 +491,12 @@ namespace WireMock.Server
|
||||
}
|
||||
catch (ArgumentException a)
|
||||
{
|
||||
_settings.Logger.Error("HttpStatusCode set to 400", a);
|
||||
_settings.Logger.Error("HttpStatusCode set to 400 {0}", a);
|
||||
return ResponseMessageBuilder.Create(a.Message, 400);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_settings.Logger.Error("HttpStatusCode set to 500", e);
|
||||
_settings.Logger.Error("HttpStatusCode set to 500 {0}", e);
|
||||
return ResponseMessageBuilder.Create(e.ToString(), 500);
|
||||
}
|
||||
}
|
||||
@@ -813,13 +813,10 @@ namespace WireMock.Server
|
||||
{
|
||||
responseBuilder = responseBuilder.WithStatusCode(statusCodeAsString);
|
||||
}
|
||||
else if (responseModel.StatusCode is int statusCodeAsInt && statusCodeAsInt > 0)
|
||||
else if (responseModel.StatusCode != null)
|
||||
{
|
||||
responseBuilder = responseBuilder.WithStatusCode(statusCodeAsInt);
|
||||
}
|
||||
else
|
||||
{
|
||||
responseBuilder = responseBuilder.WithStatusCode(HttpStatusCode.OK);
|
||||
// Convert to Int32 because Newtonsoft deserializes an 'object' with a number value to a long.
|
||||
responseBuilder = responseBuilder.WithStatusCode(Convert.ToInt32(responseModel.StatusCode));
|
||||
}
|
||||
|
||||
if (responseModel.Headers != null)
|
||||
@@ -876,7 +873,7 @@ namespace WireMock.Server
|
||||
DetectedBodyType = BodyType.String,
|
||||
BodyAsString = JsonConvert.SerializeObject(result, keepNullValues ? _settingsIncludeNullValues : _jsonSerializerSettings)
|
||||
},
|
||||
StatusCode = 200,
|
||||
StatusCode = (int) HttpStatusCode.OK,
|
||||
Headers = new Dictionary<string, WireMockList<string>> { { HttpKnownHeaderNames.ContentType, new WireMockList<string>(ContentTypeJson) } }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -258,7 +258,13 @@ namespace WireMock.Server
|
||||
if (settings.AllowBodyForAllHttpMethods == true)
|
||||
{
|
||||
_options.AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods;
|
||||
_settings.Logger.Info("AllowBodyForAllHttpMethods is set to {0}", _settings.AllowBodyForAllHttpMethods == true);
|
||||
_settings.Logger.Info("AllowBodyForAllHttpMethods is set to True");
|
||||
}
|
||||
|
||||
if (settings.AllowAnyHttpStatusCodeInResponse == true)
|
||||
{
|
||||
_options.AllowAnyHttpStatusCodeInResponse = _settings.AllowAnyHttpStatusCodeInResponse;
|
||||
_settings.Logger.Info("AllowAnyHttpStatusCodeInResponse is set to True");
|
||||
}
|
||||
|
||||
if (settings.AllowPartialMapping == true)
|
||||
|
||||
@@ -137,5 +137,11 @@ namespace WireMock.Settings
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
bool? AllowBodyForAllHttpMethods { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Allow any HttpStatusCode in the response. Also null, 0, empty or invalid. (default set to false).
|
||||
/// </summary>
|
||||
/// [PublicAPI]
|
||||
bool? AllowAnyHttpStatusCodeInResponse { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -101,5 +101,8 @@ namespace WireMock.Settings
|
||||
/// <inheritdoc cref="IWireMockServerSettings.AllowBodyForAllHttpMethods"/>
|
||||
[PublicAPI]
|
||||
public bool? AllowBodyForAllHttpMethods { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IWireMockServerSettings.AllowAnyHttpStatusCodeInResponse"/>
|
||||
public bool? AllowAnyHttpStatusCodeInResponse { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,8 @@ namespace WireMock.Settings
|
||||
MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"),
|
||||
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
||||
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
|
||||
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods")
|
||||
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods"),
|
||||
AllowAnyHttpStatusCodeInResponse = parser.GetBoolValue("AllowAnyHttpStatusCodeInResponse")
|
||||
};
|
||||
|
||||
if (logger != null)
|
||||
|
||||
@@ -23,10 +23,9 @@
|
||||
<IncludeSymbols>True</IncludeSymbols>-->
|
||||
<ProjectGuid>{D3804228-91F4-4502-9595-39584E5A01AD}</ProjectGuid>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
|
||||
<!--<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
|
||||
<CodeAnalysisRuleSet>WireMock.Net.ruleset</CodeAnalysisRuleSet>
|
||||
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>-->
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>WireMock.Net.snk</AssemblyOriginatorKeyFile>
|
||||
<!--<DelaySign>true</DelaySign>-->
|
||||
@@ -41,10 +40,14 @@
|
||||
<Target Name="CheckIfShouldKillVBCSCompiler" />
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
|
||||
<PathMap>$(MSBuildProjectDirectory)=/</PathMap>
|
||||
<!--<PathMap>$(MSBuildProjectDirectory)=/</PathMap>-->
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Debug - Sonar'">
|
||||
<CodeAnalysisRuleSet>WireMock.Net.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard1.3' or '$(TargetFramework)' == 'netstandard2.0'">
|
||||
<DefineConstants>NETSTANDARD;USE_ASPNETCORE</DefineConstants>
|
||||
</PropertyGroup>
|
||||
@@ -58,7 +61,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="RequestBuilders\Request.WithFault.cs" />
|
||||
<Compile Remove="Util\CloneUtils.cs" />
|
||||
<Compile Remove="Util\IndexableDictionary.cs" />
|
||||
<Compile Remove="Util\NamedReaderWriterLocker.cs" />
|
||||
@@ -78,8 +80,6 @@
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.12" />
|
||||
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.10" />
|
||||
<PackageReference Include="JmesPath.Net" Version="1.0.125" />
|
||||
<!--<PackageReference Include="CloneExtensionsEx" Version="1.0.2" />-->
|
||||
<!--<PackageReference Include="FastDeepCloner" Version="1.2.5" />-->
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
|
||||
|
||||
Reference in New Issue
Block a user