diff --git a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs index 553dd0b9..34a54e41 100644 --- a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs @@ -45,12 +45,12 @@ public class ProxyAndRecordSettingsModel /// /// Defines a list from headers which will be excluded from the saved mappings. /// - public string[] ExcludedHeaders { get; set; } + public string[] ExcludedHeaders { get; set; } = []; /// /// Defines a list of cookies which will be excluded from the saved mappings. /// - public string[] ExcludedCookies { get; set; } + public string[] ExcludedCookies { get; set; } = []; /// /// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to true). diff --git a/src/WireMock.Net.AspNetCore.Middleware/WireMock.Net.AspNetCore.Middleware.csproj b/src/WireMock.Net.AspNetCore.Middleware/WireMock.Net.AspNetCore.Middleware.csproj index 4b62f48a..79581b52 100644 --- a/src/WireMock.Net.AspNetCore.Middleware/WireMock.Net.AspNetCore.Middleware.csproj +++ b/src/WireMock.Net.AspNetCore.Middleware/WireMock.Net.AspNetCore.Middleware.csproj @@ -32,11 +32,6 @@ true - - - - - diff --git a/src/WireMock.Net.Minimal/Owin/GlobalExceptionMiddleware.cs b/src/WireMock.Net.Minimal/Owin/GlobalExceptionMiddleware.cs index ac6a4bd1..a33c9592 100644 --- a/src/WireMock.Net.Minimal/Owin/GlobalExceptionMiddleware.cs +++ b/src/WireMock.Net.Minimal/Owin/GlobalExceptionMiddleware.cs @@ -19,7 +19,7 @@ internal class GlobalExceptionMiddleware _responseMapper = Guard.NotNull(responseMapper); } - public RequestDelegate? Next { get; } + public RequestDelegate Next { get; } public Task Invoke(HttpContext ctx) { @@ -30,10 +30,7 @@ internal class GlobalExceptionMiddleware { try { - if (Next != null) - { - await Next.Invoke(ctx).ConfigureAwait(false); - } + await Next.Invoke(ctx).ConfigureAwait(false); } catch (Exception ex) { diff --git a/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs b/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs index e0345f50..71851167 100644 --- a/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs +++ b/src/WireMock.Net.Minimal/Owin/WireMockMiddleware.cs @@ -1,7 +1,6 @@ // Copyright © WireMock.Net using System.Diagnostics; -using System.Linq; using System.Net; using Microsoft.AspNetCore.Http; using WireMock.Constants; @@ -18,7 +17,9 @@ using WireMock.Util; namespace WireMock.Owin; internal class WireMockMiddleware( +#pragma warning disable CS9113 // Parameter is unread. RequestDelegate next, +#pragma warning restore CS9113 // Parameter is unread. IWireMockMiddlewareOptions options, IOwinRequestMapper requestMapper, IOwinResponseMapper responseMapper, diff --git a/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs b/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs index 8662a133..956cdf3a 100644 --- a/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs +++ b/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs @@ -298,7 +298,7 @@ internal class OpenApiPathsMapper(WireMockOpenApiParserSettings settings) var mappedHeaders = headers? .ToDictionary(item => item.Key, _ => GetExampleMatcherModel(null, _settings.HeaderPatternToUse).Pattern!) ?? []; - if (!string.IsNullOrEmpty(responseContentType)) + if (responseContentType != null) { mappedHeaders.TryAdd(HeaderContentType, responseContentType); } diff --git a/src/WireMock.Net.Shared/Matchers/Request/RequestMessageProtoBufMatcher.cs b/src/WireMock.Net.Shared/Matchers/Request/RequestMessageProtoBufMatcher.cs index 42b10daa..0f378fd4 100644 --- a/src/WireMock.Net.Shared/Matchers/Request/RequestMessageProtoBufMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/Request/RequestMessageProtoBufMatcher.cs @@ -1,6 +1,5 @@ // Copyright © WireMock.Net -using System; using WireMock.Models; using WireMock.Util; @@ -40,6 +39,6 @@ public class RequestMessageProtoBufMatcher : IRequestMatcher private MatchResult GetMatchResult(IRequestMessage requestMessage) { - return Matcher?.IsMatchAsync(requestMessage.BodyAsBytes).GetAwaiter().GetResult() ?? default; + return Matcher?.IsMatchAsync(requestMessage.BodyAsBytes).GetAwaiter().GetResult() ?? MatchResult.From(nameof(RequestMessageProtoBufMatcher)); } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs b/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs index c8e3def2..5f7f3c07 100644 --- a/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs +++ b/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs @@ -582,7 +582,7 @@ public class WireMockAdminApiAssertionsTests : IDisposable public async Task HaveReceivedACall_FromClientIP_whenACallWasMadeFromClientIP_Should_BeOK() { await _httpClient.GetAsync("", _ct); - var clientIP = _server.LogEntries.Last().RequestMessage.ClientIP; + var clientIP = _server.LogEntries.Last().RequestMessage!.ClientIP; _adminApi.Should() .HaveReceivedACall() diff --git a/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs b/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs index eacadc6a..868fafa0 100644 --- a/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs +++ b/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Http; using Moq; +using WireMock.Logging; using WireMock.Owin; using WireMock.Owin.Mappers; @@ -17,20 +18,32 @@ public class GlobalExceptionMiddlewareTests public GlobalExceptionMiddlewareTests() { _optionsMock = new Mock(); - _optionsMock.SetupAllProperties(); + _optionsMock.SetupGet(o => o.Logger).Returns(Mock.Of()); _responseMapperMock = new Mock(); - _responseMapperMock.SetupAllProperties(); _responseMapperMock.Setup(m => m.MapAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - _sut = new GlobalExceptionMiddleware(null, _optionsMock.Object, _responseMapperMock.Object); + _sut = new GlobalExceptionMiddleware(_ => Task.CompletedTask, _optionsMock.Object, _responseMapperMock.Object); } [Fact] - public void GlobalExceptionMiddleware_Invoke_NullAsNext_DoesNotInvokeNextAndDoesNotThrow() + public void GlobalExceptionMiddleware_Invoke_ValidNext_ShouldNotThrow() { // Act - Action act = () => _sut.Invoke(null); - act.Should().NotThrow(); + _sut.Invoke(Mock.Of()); + } + + [Fact] + public void GlobalExceptionMiddleware_Invoke_InvalidNext_ShouldCallResponseMapperWith500() + { + // Arrange + var sut = new GlobalExceptionMiddleware(_ => throw new ArgumentException(), _optionsMock.Object, _responseMapperMock.Object); + + // Act + sut.Invoke(Mock.Of()); + + // Verify + _responseMapperMock.Verify(m => m.MapAsync(It.IsAny(), It.IsAny()), Times.Once); + _responseMapperMock.VerifyNoOtherCalls(); } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyFromFileTests.cs b/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyFromFileTests.cs index eb5ad343..a0642988 100644 --- a/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyFromFileTests.cs +++ b/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithBodyFromFileTests.cs @@ -35,7 +35,7 @@ public class ResponseWithBodyFromFileTests ); // Act - var response = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/v1/content"); + var response = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/v1/content", _ct); // Assert response.Should().Contain("world"); diff --git a/test/WireMock.Net.Tests/WebSockets/WebSocketIntegrationTests.cs b/test/WireMock.Net.Tests/WebSockets/WebSocketIntegrationTests.cs index c01693ae..3aa0378e 100644 --- a/test/WireMock.Net.Tests/WebSockets/WebSocketIntegrationTests.cs +++ b/test/WireMock.Net.Tests/WebSockets/WebSocketIntegrationTests.cs @@ -796,7 +796,7 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc var proxyUri = new Uri($"{sut.Url}/ws/proxy"); await client.ConnectAsync(proxyUri, _ct); - await Task.Delay(500, _ct); + await Task.Delay(250, _ct); var testData = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }; @@ -805,6 +805,8 @@ public class WebSocketIntegrationTests(ITestOutputHelper output, ITestContextAcc var receivedData = await client.ReceiveAsBytesAsync(cancellationToken: _ct); + await Task.Delay(250, _ct); + // Assert receivedData.Should().BeEquivalentTo(testData, "binary data should be proxied and echoed back");