AllowAnyHttpStatusCodeInResponse (#407)

* .

* ,

* PUBLISH_TESTRESULTS

* fix logging

* fix compile error

* codefactor fix

* Debug - Sonar + other things in csproj
This commit is contained in:
Stef Heyenrath
2020-01-27 18:47:58 +01:00
committed by GitHub
parent 6ae7fc1d75
commit 307a89d324
27 changed files with 249 additions and 54 deletions

View File

@@ -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;
}