Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed StatusCode = null or < 0 #406

Merged
merged 4 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Settings;
using WireMock.Util;
using WireMock.Validation;
using WireMock.Types;
using WireMock.Validation;

namespace WireMock.Serialization
{
Expand Down
11 changes: 8 additions & 3 deletions src/WireMock.Net/Server/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -808,13 +809,17 @@ private IResponseBuilder InitResponseBuilder(ResponseModel responseModel)
return responseBuilder.WithProxy(proxyAndRecordSettings);
}

if (responseModel.StatusCode is string)
if (responseModel.StatusCode is string statusCodeAsString)
{
responseBuilder = responseBuilder.WithStatusCode((string) responseModel.StatusCode);
responseBuilder = responseBuilder.WithStatusCode(statusCodeAsString);
}
else if (responseModel.StatusCode is int statusCodeAsInt && statusCodeAsInt > 0)
{
responseBuilder = responseBuilder.WithStatusCode(statusCodeAsInt);
}
else
{
responseBuilder = responseBuilder.WithStatusCode(Convert.ToInt32(responseModel.StatusCode));
responseBuilder = responseBuilder.WithStatusCode(HttpStatusCode.OK);
}

if (responseModel.Headers != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using Moq;
using Newtonsoft.Json;
using NFluent;
using System.Linq;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,53 @@ public async Task Response_ProvideResponse_Handlebars_Query()
Check.That(responseMessage.BodyData.BodyAsString).Equals("test keya=1 idx=1 idx=2 keyb=5");
}

[Fact]
public async Task Response_ProvideResponse_Handlebars_StatusCode()
{
// Assign
var body = new BodyData
{
BodyAsString = "abc",
DetectedBodyType = BodyType.String
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo?a=400"), "POST", ClientIp, body);

var response = Response.Create()
.WithStatusCode("{{request.query.a}}")
.WithBody("test")
.WithTransformer();

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

// Assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("test");
Check.That(responseMessage.StatusCode).Equals("400");
}

[Fact]
public async Task Response_ProvideResponse_Handlebars_StatusCodeIsNull()
{
// Assign
var body = new BodyData
{
BodyAsString = "abc",
DetectedBodyType = BodyType.String
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo?a=400"), "POST", ClientIp, body);

var response = Response.Create()
.WithBody("test")
.WithTransformer();

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

// Assert
Check.That(responseMessage.BodyData.BodyAsString).Equals("test");
Check.That(responseMessage.StatusCode).Equals(200);
}

[Fact]
public async Task Response_ProvideResponse_Handlebars_Header()
{
Expand Down
16 changes: 13 additions & 3 deletions test/WireMock.Net.Tests/WireMockAdminApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using WireMock.Client;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.Server;
using WireMock.Settings;
using Xunit;
Expand Down Expand Up @@ -88,8 +90,13 @@ public async Task IWireMockAdminApi_PutMappingAsync()
server.Stop();
}

[Fact]
public async Task IWireMockAdminApi_PostMappingAsync()
[Theory]
[InlineData(null, 200)]
[InlineData(-1, 200)]
[InlineData(0, 200)]
[InlineData(200, 200)]
[InlineData("200", "200")]
public async Task IWireMockAdminApi_PostMappingAsync(object statusCode, object expectedStatusCode)
{
// Arrange
var server = WireMockServer.StartWithAdminInterface();
Expand All @@ -99,7 +106,7 @@ public async Task IWireMockAdminApi_PostMappingAsync()
var model = new MappingModel
{
Request = new RequestModel { Path = "/1" },
Response = new ResponseModel { Body = "txt", StatusCode = 200 },
Response = new ResponseModel { Body = "txt", StatusCode = statusCode },
Priority = 500,
Title = "test"
};
Expand All @@ -114,6 +121,9 @@ public async Task IWireMockAdminApi_PostMappingAsync()
Check.That(mapping).IsNotNull();
Check.That(mapping.Title).Equals("test");

var response = await mapping.ProvideResponseAsync(new RequestMessage(new UrlDetails("http://localhost/1"), "GET", ""));
Check.That(response.StatusCode).Equals(expectedStatusCode);

server.Stop();
}

Expand Down