mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-18 16:17:10 +01:00
Fixed StatusCode = null or < 0 (#406)
* . * fix tests * responseModel.StatusCode is int statusCodeAsInt && statusCodeAsInt > 0 * < 0
This commit is contained in:
@@ -5,9 +5,8 @@ using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Util;
|
||||
using WireMock.Validation;
|
||||
using WireMock.Types;
|
||||
using WireMock.Validation;
|
||||
|
||||
namespace WireMock.Serialization
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -808,13 +809,17 @@ namespace WireMock.Server
|
||||
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)
|
||||
|
||||
@@ -4,9 +4,7 @@ using System.Linq;
|
||||
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;
|
||||
|
||||
@@ -105,6 +105,53 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
||||
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()
|
||||
{
|
||||
|
||||
@@ -12,6 +12,8 @@ using WireMock.Admin.Settings;
|
||||
using WireMock.Client;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Models;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
using Xunit;
|
||||
@@ -88,8 +90,13 @@ namespace WireMock.Net.Tests
|
||||
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();
|
||||
@@ -99,7 +106,7 @@ namespace WireMock.Net.Tests
|
||||
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"
|
||||
};
|
||||
@@ -114,6 +121,9 @@ namespace WireMock.Net.Tests
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user