Skip to content

Commit

Permalink
WithCallback should use also use enum HttpStatusCode (#535)
Browse files Browse the repository at this point in the history
* Fix #533

* simplyfy code
  • Loading branch information
StefH authored Nov 10, 2020
1 parent e107b5c commit a0fdc00
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 189 deletions.
20 changes: 13 additions & 7 deletions src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
Expand Down Expand Up @@ -79,17 +80,22 @@ public async Task MapAsync(ResponseMessage responseMessage, IResponse response)
break;
}

switch (responseMessage.StatusCode)
{
case int statusCodeAsInteger:
response.StatusCode = MapStatusCode(statusCodeAsInteger);
break;
var statusCodeType = responseMessage.StatusCode?.GetType();

case string statusCodeAsString:
switch (statusCodeType)
{
case Type typeAsIntOrEnum when typeAsIntOrEnum == typeof(int) || typeAsIntOrEnum == typeof(int?) || typeAsIntOrEnum.GetTypeInfo().IsEnum:
response.StatusCode = MapStatusCode((int)responseMessage.StatusCode);
break;

case Type typeAsString when typeAsString == typeof(string):
// 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);
break;

default:
break;
}

SetResponseHeaders(responseMessage, response);
Expand Down
160 changes: 80 additions & 80 deletions test/WireMock.Net.Tests/ResponseBuilders/ResponseWithCallbackTests.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
using Xunit;

namespace WireMock.Net.Tests.ResponseBuilders
{
public class ResponseWithCallbackTests
{
private readonly WireMockServerSettings _settings = new WireMockServerSettings();

[Fact]
public async Task Response_WithCallbackAsync()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
var response = Response.Create()
using System.Threading.Tasks;
using FluentAssertions;
using WireMock.Models;
using WireMock.ResponseBuilders;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
using Xunit;

namespace WireMock.Net.Tests.ResponseBuilders
{
public class ResponseWithCallbackTests
{
private readonly WireMockServerSettings _settings = new WireMockServerSettings();

[Fact]
public async Task Response_WithCallbackAsync()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
var response = Response.Create()
.WithCallback(async request =>
{
await Task.Delay(1);
Expand All @@ -32,63 +32,63 @@ public async Task Response_WithCallbackAsync()
},
StatusCode = 302
};
});

// Act
var responseMessage = await response.ProvideResponseAsync(requestMessage, _settings);

// Assert
responseMessage.BodyData.BodyAsString.Should().Be("/fooBar");
responseMessage.StatusCode.Should().Be(302);
}

[Fact]
public async Task Response_WithCallback()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
var response = Response.Create()
.WithCallback(request => new ResponseMessage
{
BodyData = new BodyData
{
DetectedBodyType = BodyType.String,
BodyAsString = request.Path + "Bar"
},
StatusCode = 302
});

// Act
var responseMessage = await response.ProvideResponseAsync(requestMessage, _settings);

// Assert
responseMessage.BodyData.BodyAsString.Should().Be("/fooBar");
responseMessage.StatusCode.Should().Be(302);
}

[Fact]
public async Task Response_WithCallback_And_UseTransformer_Is_True()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
var response = Response.Create()
.WithCallback(request => new ResponseMessage
{
BodyData = new BodyData
{
DetectedBodyType = BodyType.String,
BodyAsString = "{{request.Path}}Bar"
},
StatusCode = 302
})
.WithTransformer();

// Act
var responseMessage = await response.ProvideResponseAsync(requestMessage, _settings);

// Assert
responseMessage.BodyData.BodyAsString.Should().Be("/fooBar");
responseMessage.StatusCode.Should().Be(302);
}
}
});

// Act
var responseMessage = await response.ProvideResponseAsync(requestMessage, _settings);

// Assert
responseMessage.BodyData.BodyAsString.Should().Be("/fooBar");
responseMessage.StatusCode.Should().Be(302);
}

[Fact]
public async Task Response_WithCallback()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
var response = Response.Create()
.WithCallback(request => new ResponseMessage
{
BodyData = new BodyData
{
DetectedBodyType = BodyType.String,
BodyAsString = request.Path + "Bar"
},
StatusCode = 302
});

// Act
var responseMessage = await response.ProvideResponseAsync(requestMessage, _settings);

// Assert
responseMessage.BodyData.BodyAsString.Should().Be("/fooBar");
responseMessage.StatusCode.Should().Be(302);
}

[Fact]
public async Task Response_WithCallback_And_UseTransformer_Is_True()
{
// Assign
var requestMessage = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", "::1");
var response = Response.Create()
.WithCallback(request => new ResponseMessage
{
BodyData = new BodyData
{
DetectedBodyType = BodyType.String,
BodyAsString = "{{request.Path}}Bar"
},
StatusCode = 302
})
.WithTransformer();

// Act
var responseMessage = await response.ProvideResponseAsync(requestMessage, _settings);

// Assert
responseMessage.BodyData.BodyAsString.Should().Be("/fooBar");
responseMessage.StatusCode.Should().Be(302);
}
}
}
Loading

0 comments on commit a0fdc00

Please sign in to comment.