mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-27 19:27:42 +02:00
WithCallback should use also use enum HttpStatusCode (#535)
* Fix #533 * simplyfy code
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@@ -79,17 +80,22 @@ namespace WireMock.Owin.Mappers
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (responseMessage.StatusCode)
|
var statusCodeType = responseMessage.StatusCode?.GetType();
|
||||||
|
|
||||||
|
switch (statusCodeType)
|
||||||
{
|
{
|
||||||
case int statusCodeAsInteger:
|
case Type typeAsIntOrEnum when typeAsIntOrEnum == typeof(int) || typeAsIntOrEnum == typeof(int?) || typeAsIntOrEnum.GetTypeInfo().IsEnum:
|
||||||
response.StatusCode = MapStatusCode(statusCodeAsInteger);
|
response.StatusCode = MapStatusCode((int)responseMessage.StatusCode);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case string statusCodeAsString:
|
case Type typeAsString when typeAsString == typeof(string):
|
||||||
// Note: this case will also match on null
|
// Note: this case will also match on null
|
||||||
int.TryParse(statusCodeAsString, out int result);
|
int.TryParse(responseMessage.StatusCode as string, out int result);
|
||||||
response.StatusCode = MapStatusCode(result);
|
response.StatusCode = MapStatusCode(result);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
SetResponseHeaders(responseMessage, response);
|
SetResponseHeaders(responseMessage, response);
|
||||||
|
|||||||
37
test/WireMock.Net.Tests/WireMockServerTests.WithCallback.cs
Normal file
37
test/WireMock.Net.Tests/WireMockServerTests.WithCallback.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using WireMock.RequestBuilders;
|
||||||
|
using WireMock.ResponseBuilders;
|
||||||
|
using WireMock.Server;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests
|
||||||
|
{
|
||||||
|
public partial class WireMockServerTests
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(HttpStatusCode.Conflict)]
|
||||||
|
[InlineData(409)]
|
||||||
|
[InlineData("409")]
|
||||||
|
public async Task WireMockServer_WithCallback_Should_Use_StatusCodeFromResponse(object statusCode)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = WireMockServer.Start();
|
||||||
|
server.Given(Request.Create().UsingPost().WithPath("/foo"))
|
||||||
|
.RespondWith(Response.Create()
|
||||||
|
.WithCallback(request => new ResponseMessage
|
||||||
|
{
|
||||||
|
StatusCode = statusCode
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
using var httpClient = new HttpClient();
|
||||||
|
var response = await httpClient.PostAsync("http://localhost:" + server.Ports[0] + "/foo", new StringContent("dummy"));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
response.StatusCode.Should().Be(409);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ using Xunit;
|
|||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
public class WireMockServerTests
|
public partial class WireMockServerTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WireMockServer_Should_reset_requestlogs()
|
public async Task WireMockServer_Should_reset_requestlogs()
|
||||||
|
|||||||
Reference in New Issue
Block a user