mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-23 17:41:01 +01:00
Allow all headers to be set as Response headers (#142)
* Allow all headers to be set as Response headers * RunTestDifferentPort * Fix Proxy_Should_change_absolute_location_header_in_proxied_response test * Fix OwinResponseMapper * Fix test * 1.0.3.18
This commit is contained in:
@@ -5,7 +5,6 @@ using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading.Tasks;
|
||||
using NFluent;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
@@ -190,29 +189,30 @@ namespace WireMock.Net.Tests
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Proxy_Should_change_absolute_location_header_in_proxied_response()
|
||||
{
|
||||
// given
|
||||
_serverForProxyForwarding = FluentMockServer.Start();
|
||||
// Assign
|
||||
var settings = new FluentMockServerSettings { AllowPartialMapping = false };
|
||||
_serverForProxyForwarding = FluentMockServer.Start(settings);
|
||||
_serverForProxyForwarding
|
||||
.Given(Request.Create().WithPath("/*"))
|
||||
.RespondWith(Response.Create()
|
||||
.WithStatusCode(HttpStatusCode.Redirect)
|
||||
.WithHeader("Location", _serverForProxyForwarding.Urls[0] + "testpath"));
|
||||
|
||||
_server = FluentMockServer.Start();
|
||||
_server = FluentMockServer.Start(settings);
|
||||
_server
|
||||
.Given(Request.Create().WithPath("/*"))
|
||||
.Given(Request.Create().WithPath("/prx"))
|
||||
.RespondWith(Response.Create().WithProxy(_serverForProxyForwarding.Urls[0]));
|
||||
|
||||
// when
|
||||
// Act
|
||||
var requestMessage = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Get,
|
||||
RequestUri = new Uri(_server.Urls[0])
|
||||
RequestUri = new Uri(_server.Urls[0] + "/prx")
|
||||
};
|
||||
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
|
||||
var response = await new HttpClient(httpClientHandler).SendAsync(requestMessage);
|
||||
|
||||
// then
|
||||
// Assert
|
||||
Check.That(response.Headers.Contains("Location")).IsTrue();
|
||||
Check.That(response.Headers.GetValues("Location")).ContainsExactly(_server.Urls[0] + "testpath");
|
||||
}
|
||||
|
||||
@@ -351,8 +351,11 @@ namespace WireMock.Net.Tests
|
||||
Check.That(responseAsBytes).ContainsExactly(new byte[] { 48, 49 });
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> ValidMatchersForHelloServerJsonMessage =>
|
||||
new List<object[]>
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_respond_to_valid_matchers_when_sent_json()
|
||||
{
|
||||
// Assign
|
||||
var validMatchersForHelloServerJsonMessage = new List<object[]>
|
||||
{
|
||||
new object[] { new WildcardMatcher("*Hello server*"), "application/json" },
|
||||
new object[] { new WildcardMatcher("*Hello server*"), "text/plain" },
|
||||
@@ -364,24 +367,25 @@ namespace WireMock.Net.Tests
|
||||
new object[] { new JsonPathMatcher("$..[?(@.message == 'Hello server')]"), "text/plain" }
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ValidMatchersForHelloServerJsonMessage))]
|
||||
public async Task FluentMockServer_Should_respond_to_valid_matchers_when_sent_json(IMatcher matcher, string contentType)
|
||||
{
|
||||
// Assign
|
||||
_server = FluentMockServer.Start();
|
||||
|
||||
_server
|
||||
.Given(Request.Create().WithPath("/foo").WithBody(matcher))
|
||||
.RespondWith(Response.Create().WithBody("Hello client"));
|
||||
foreach (var item in validMatchersForHelloServerJsonMessage)
|
||||
{
|
||||
_server
|
||||
.Given(Request.Create().WithPath("/foo").WithBody((IMatcher)item[0]))
|
||||
.RespondWith(Response.Create().WithBody("Hello client"));
|
||||
|
||||
// Act
|
||||
var content = new StringContent(jsonRequestMessage, Encoding.UTF8, contentType);
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + _server.Ports[0] + "/foo", content);
|
||||
// Act
|
||||
var content = new StringContent(jsonRequestMessage, Encoding.UTF8, (string)item[1]);
|
||||
var response = await new HttpClient().PostAsync("http://localhost:" + _server.Ports[0] + "/foo", content);
|
||||
|
||||
// Assert
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
Check.That(responseString).Equals("Hello client");
|
||||
// Assert
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
Check.That(responseString).Equals("Hello client");
|
||||
|
||||
_server.ResetMappings();
|
||||
_server.ResetLogEntries();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -581,24 +585,6 @@ namespace WireMock.Net.Tests
|
||||
Check.That(response).IsEqualTo("/fooBar");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FluentMockServer_Should_IgnoreRestrictedHeader()
|
||||
{
|
||||
// Assign
|
||||
_server = FluentMockServer.Start();
|
||||
_server
|
||||
.Given(Request.Create().WithPath("/head").UsingHead())
|
||||
.RespondWith(Response.Create().WithHeader("Content-Length", "1024"));
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Head, "http://localhost:" + _server.Ports[0] + "/head");
|
||||
|
||||
// Act
|
||||
var response = await new HttpClient().SendAsync(request);
|
||||
|
||||
// Assert
|
||||
Check.That(response.Content.Headers.GetValues("Content-Length")).ContainsExactly("0");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_server?.Stop();
|
||||
|
||||
@@ -9,18 +9,21 @@ namespace WireMock.Net.Tests
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
|
||||
[Fact]
|
||||
public async void Response_Create_WithHeader_ContentLength()
|
||||
[Theory]
|
||||
[InlineData("Content-Length", "1024")]
|
||||
[InlineData("Transfer-Encoding", "identity")]
|
||||
[InlineData("Location", "http://test")]
|
||||
public async void Response_Create_WithHeader(string headerName, string headerValue)
|
||||
{
|
||||
// Assign
|
||||
var requestMock = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp);
|
||||
IResponseBuilder builder = Response.Create().WithHeader("Content-Length", "1024");
|
||||
IResponseBuilder builder = Response.Create().WithHeader(headerName, headerValue);
|
||||
|
||||
// Act
|
||||
var response = await builder.ProvideResponseAsync(requestMock);
|
||||
|
||||
// Assert
|
||||
Check.That(response.Headers["Content-Length"].ToString()).Equals("1024");
|
||||
Check.That(response.Headers[headerName].ToString()).Equals(headerValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user