diff --git a/Directory.Build.props b/Directory.Build.props index 8c1cd232..c63453a7 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ - 1.1.10 + 1.2.0 diff --git a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs index a1874422..1e2763d6 100644 --- a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs +++ b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs @@ -61,8 +61,6 @@ namespace WireMock.Net.ConsoleApplication handlebarsContext.RegisterHelper(transformer.Name, transformer.Render); }, - AllowAnyHttpStatusCodeInResponse = true - // Uncomment below if you want to use the CustomFileSystemFileHandler // FileSystemHandler = new CustomFileSystemFileHandler() }); diff --git a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs index 1a89533d..65acc007 100644 --- a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs @@ -40,7 +40,7 @@ namespace WireMock.Owin bool? AllowBodyForAllHttpMethods { get; set; } - bool? AllowAnyHttpStatusCodeInResponse { get; set; } + bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; } bool? DisableJsonBodyParsing { get; set; } } diff --git a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs index e2a89d8b..365d50bc 100644 --- a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs @@ -104,12 +104,12 @@ namespace WireMock.Owin.Mappers private int MapStatusCode(int code) { - if (_options.AllowAnyHttpStatusCodeInResponse == true || Enum.IsDefined(typeof(HttpStatusCode), code)) + if (_options.AllowOnlyDefinedHttpStatusCodeInResponse == true && !Enum.IsDefined(typeof(HttpStatusCode), code)) { - return code; + return (int)HttpStatusCode.OK; } - return (int)HttpStatusCode.OK; + return code; } private bool IsFault(ResponseMessage responseMessage) diff --git a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs index 3599a249..d0be329f 100644 --- a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs @@ -43,8 +43,8 @@ namespace WireMock.Owin /// public bool? AllowBodyForAllHttpMethods { get; set; } - /// - public bool? AllowAnyHttpStatusCodeInResponse { get; set; } + /// + public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; } /// public bool? DisableJsonBodyParsing { get; set; } diff --git a/src/WireMock.Net/ResponseBuilders/IStatusCodeResponseBuilder.cs b/src/WireMock.Net/ResponseBuilders/IStatusCodeResponseBuilder.cs index 41777664..5bcda2e1 100644 --- a/src/WireMock.Net/ResponseBuilders/IStatusCodeResponseBuilder.cs +++ b/src/WireMock.Net/ResponseBuilders/IStatusCodeResponseBuilder.cs @@ -1,4 +1,5 @@ using System.Net; +using WireMock.Settings; namespace WireMock.ResponseBuilders { @@ -9,6 +10,7 @@ namespace WireMock.ResponseBuilders { /// /// The with status code. + /// By default all status codes are allowed, to change this behaviour, see . /// /// The code. /// The . @@ -16,6 +18,7 @@ namespace WireMock.ResponseBuilders /// /// The with status code. + /// By default all status codes are allowed, to change this behaviour, see . /// /// The code. /// The . @@ -23,6 +26,7 @@ namespace WireMock.ResponseBuilders /// /// The with status code. + /// By default all status codes are allowed, to change this behaviour, see . /// /// The code. /// The . diff --git a/src/WireMock.Net/Server/WireMockServer.cs b/src/WireMock.Net/Server/WireMockServer.cs index 9eaaad8f..328aa14f 100644 --- a/src/WireMock.Net/Server/WireMockServer.cs +++ b/src/WireMock.Net/Server/WireMockServer.cs @@ -269,10 +269,10 @@ namespace WireMock.Server _settings.Logger.Info("AllowBodyForAllHttpMethods is set to True"); } - if (settings.AllowAnyHttpStatusCodeInResponse == true) + if (settings.AllowOnlyDefinedHttpStatusCodeInResponse == true) { - _options.AllowAnyHttpStatusCodeInResponse = _settings.AllowAnyHttpStatusCodeInResponse; - _settings.Logger.Info("AllowAnyHttpStatusCodeInResponse is set to True"); + _options.AllowOnlyDefinedHttpStatusCodeInResponse = _settings.AllowOnlyDefinedHttpStatusCodeInResponse; + _settings.Logger.Info("AllowOnlyDefinedHttpStatusCodeInResponse is set to True"); } if (settings.AllowPartialMapping == true) diff --git a/src/WireMock.Net/Settings/IWireMockServerSettings.cs b/src/WireMock.Net/Settings/IWireMockServerSettings.cs index d11490b8..fe0e2798 100644 --- a/src/WireMock.Net/Settings/IWireMockServerSettings.cs +++ b/src/WireMock.Net/Settings/IWireMockServerSettings.cs @@ -139,10 +139,12 @@ namespace WireMock.Settings bool? AllowBodyForAllHttpMethods { get; set; } /// - /// Allow any HttpStatusCode in the response. Also null, 0, empty or invalid. (default set to false). + /// Allow only a HttpStatus Code in the response which is defined. (default set to false). + /// - false : also null, 0, empty or invalid HttpStatus codes are allowed. + /// - true : only codes defined in are allowed. /// /// [PublicAPI] - bool? AllowAnyHttpStatusCodeInResponse { get; set; } + bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; } /// /// Set to true to disable Json deserialization when processing requests. (default set to false). diff --git a/src/WireMock.Net/Settings/WireMockServerSettings.cs b/src/WireMock.Net/Settings/WireMockServerSettings.cs index f05b993d..5debcf65 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettings.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettings.cs @@ -102,8 +102,8 @@ namespace WireMock.Settings [PublicAPI] public bool? AllowBodyForAllHttpMethods { get; set; } - /// - public bool? AllowAnyHttpStatusCodeInResponse { get; set; } + /// + public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; } /// [PublicAPI] diff --git a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs index ed5c0ecf..62aa48ce 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs @@ -35,7 +35,7 @@ namespace WireMock.Settings RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"), AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"), AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods"), - AllowAnyHttpStatusCodeInResponse = parser.GetBoolValue("AllowAnyHttpStatusCodeInResponse"), + AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"), DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing") }; diff --git a/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs b/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs index a542ca77..caf662c9 100644 --- a/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs +++ b/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs @@ -70,11 +70,9 @@ namespace WireMock.Net.Tests.Owin.Mappers } [Theory] - [InlineData(0, 200)] - [InlineData(-1, 200)] - [InlineData(10000, 200)] [InlineData(300, 300)] - public async Task OwinResponseMapper_MapAsync_StatusCode(object code, int expected) + [InlineData(500, 500)] + public async Task OwinResponseMapper_MapAsync_Valid_StatusCode(object code, int expected) { // Arrange var responseMessage = new ResponseMessage @@ -89,8 +87,46 @@ namespace WireMock.Net.Tests.Owin.Mappers _responseMock.VerifySet(r => r.StatusCode = expected, Times.Once); } + [Theory] + [InlineData(0, 200)] + [InlineData(-1, 200)] + [InlineData(10000, 200)] + [InlineData(300, 300)] + public async Task OwinResponseMapper_MapAsync_Invalid_StatusCode_When_AllowOnlyDefinedHttpStatusCodeInResponseSet_Is_True(object code, int expected) + { + // Arrange + _optionsMock.SetupGet(o => o.AllowOnlyDefinedHttpStatusCodeInResponse).Returns(true); + var responseMessage = new ResponseMessage + { + StatusCode = code + }; + + // Act + await _sut.MapAsync(responseMessage, _responseMock.Object); + + // Assert + _responseMock.VerifySet(r => r.StatusCode = expected, Times.Once); + } + [Fact] - public async Task OwinResponseMapper_MapAsync_StatusCodeNull() + public async Task OwinResponseMapper_MapAsync_Null_StatusCode_When_AllowOnlyDefinedHttpStatusCodeInResponseSet_Is_True() + { + // Arrange + _optionsMock.SetupGet(o => o.AllowOnlyDefinedHttpStatusCodeInResponse).Returns(true); + var responseMessage = new ResponseMessage + { + StatusCode = null + }; + + // Act + await _sut.MapAsync(responseMessage, _responseMock.Object); + + // Assert + _responseMock.VerifyNoOtherCalls(); + } + + [Fact] + public async Task OwinResponseMapper_MapAsync_StatusCode_Is_Null() { // Arrange var responseMessage = new ResponseMessage @@ -110,10 +146,9 @@ namespace WireMock.Net.Tests.Owin.Mappers [InlineData(-1, -1)] [InlineData(10000, 10000)] [InlineData(300, 300)] - public async Task OwinResponseMapper_MapAsync_StatusCode_WithAllowAll(object code, int expected) + public async Task OwinResponseMapper_MapAsync_StatusCode_Is_NotInEnumRange(object code, int expected) { // Arrange - _optionsMock.SetupGet(o => o.AllowAnyHttpStatusCodeInResponse).Returns(true); var responseMessage = new ResponseMessage { StatusCode = code @@ -126,23 +161,6 @@ namespace WireMock.Net.Tests.Owin.Mappers _responseMock.VerifySet(r => r.StatusCode = expected, Times.Once); } - [Fact] - public async Task OwinResponseMapper_MapAsync_StatusCode_WithAllowAll_Null() - { - // Arrange - _optionsMock.SetupGet(o => o.AllowAnyHttpStatusCodeInResponse).Returns(true); - var responseMessage = new ResponseMessage - { - StatusCode = null - }; - - // Act - await _sut.MapAsync(responseMessage, _responseMock.Object); - - // Assert - _responseMock.VerifyNoOtherCalls(); - } - [Fact] public async Task OwinResponseMapper_MapAsync_NoBody() { diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index b0bade62..d788bc42 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -19,6 +19,10 @@ ../../src/WireMock.Net/WireMock.Net.snk true + + + true + true diff --git a/test/WireMock.Net.Tests/WireMockServer.Settings.cs b/test/WireMock.Net.Tests/WireMockServer.Settings.cs index ecb5c3d9..2a40d43c 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Settings.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Settings.cs @@ -141,21 +141,21 @@ namespace WireMock.Net.Tests } [Fact] - public void WireMockServer_WireMockServerSettings_AllowAnyHttpStatusCodeInResponse() + public void WireMockServer_WireMockServerSettings_AllowOnlyDefinedHttpStatusCodeInResponse() { // Assign and Act var server = WireMockServer.Start(new WireMockServerSettings { Logger = _loggerMock.Object, - AllowAnyHttpStatusCodeInResponse = true + AllowOnlyDefinedHttpStatusCodeInResponse = true }); // Assert var options = server.GetPrivateFieldValue("_options"); - Check.That(options.AllowAnyHttpStatusCodeInResponse).Equals(true); + Check.That(options.AllowOnlyDefinedHttpStatusCodeInResponse).Equals(true); // Verify - _loggerMock.Verify(l => l.Info(It.Is(s => s.Contains("AllowAnyHttpStatusCodeInResponse") && s.Contains("True")))); + _loggerMock.Verify(l => l.Info(It.Is(s => s.Contains("AllowOnlyDefinedHttpStatusCodeInResponse") && s.Contains("True")))); } [Fact]