mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-21 07:51:23 +02:00
Add support to use 'mapping' object in in reponse templating (#798)
* mapping * . * .
This commit is contained in:
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Moq;
|
||||
using NFluent;
|
||||
using WireMock.Models;
|
||||
using WireMock.RequestBuilders;
|
||||
@@ -12,78 +13,81 @@ using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.ResponseBuilders
|
||||
namespace WireMock.Net.Tests.ResponseBuilders;
|
||||
|
||||
public class ResponseWithProxyTests : IDisposable
|
||||
{
|
||||
public class ResponseWithProxyTests : IDisposable
|
||||
private const string ClientIp = "::1";
|
||||
private readonly WireMockServerSettings _settings = new();
|
||||
private readonly WireMockServer _server;
|
||||
private readonly Guid _guid;
|
||||
|
||||
private readonly Mock<IMapping> _mappingMock;
|
||||
|
||||
public ResponseWithProxyTests()
|
||||
{
|
||||
private const string ClientIp = "::1";
|
||||
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
|
||||
private readonly WireMockServer _server;
|
||||
private readonly Guid _guid;
|
||||
_mappingMock = new Mock<IMapping>();
|
||||
|
||||
public ResponseWithProxyTests()
|
||||
_guid = Guid.NewGuid();
|
||||
|
||||
_server = WireMockServer.Start();
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/{_guid}"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 42 }).WithHeader("Content-Type", "application/json"));
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/{_guid}/append"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 10 }).WithHeader("Content-Type", "application/json"));
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/prepend/{_guid}"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 11 }).WithHeader("Content-Type", "application/json"));
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/prepend/{_guid}/append"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 12 }).WithHeader("Content-Type", "application/json"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "", "{\"p\":42}")]
|
||||
[InlineData("", "/append", "{\"p\":10}")]
|
||||
[InlineData("/prepend", "", "{\"p\":11}")]
|
||||
[InlineData("/prepend", "/append", "{\"p\":12}")]
|
||||
public async Task Response_WithProxy(string prepend, string append, string expectedBody)
|
||||
{
|
||||
// Assign
|
||||
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml" } } };
|
||||
var request = new RequestMessage(new UrlDetails($"{_server.Urls[0]}{prepend}/{_guid}{append}"), "POST", ClientIp, new BodyData { DetectedBodyType = BodyType.Json, BodyAsJson = new { a = 1 } }, headers);
|
||||
var responseBuilder = Response.Create().WithProxy(_server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(request.ProxyUrl).IsNotNull();
|
||||
Check.That(response.Message.BodyData.BodyAsString).IsEqualTo(expectedBody);
|
||||
Check.That(response.Message.StatusCode).IsEqualTo(201);
|
||||
Check.That(response.Message.Headers["Content-Type"].ToString()).IsEqualTo("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Response_WithProxy_WebProxySettings()
|
||||
{
|
||||
// Assign
|
||||
var settings = new ProxyAndRecordSettings
|
||||
{
|
||||
_guid = Guid.NewGuid();
|
||||
|
||||
_server = WireMockServer.Start();
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/{_guid}"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 42 }).WithHeader("Content-Type", "application/json"));
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/{_guid}/append"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 10 }).WithHeader("Content-Type", "application/json"));
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/prepend/{_guid}"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 11 }).WithHeader("Content-Type", "application/json"));
|
||||
_server.Given(Request.Create().UsingPost().WithPath($"/prepend/{_guid}/append"))
|
||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 12 }).WithHeader("Content-Type", "application/json"));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "", "{\"p\":42}")]
|
||||
[InlineData("", "/append", "{\"p\":10}")]
|
||||
[InlineData("/prepend", "", "{\"p\":11}")]
|
||||
[InlineData("/prepend", "/append", "{\"p\":12}")]
|
||||
public async Task Response_WithProxy(string prepend, string append, string expectedBody)
|
||||
{
|
||||
// Assign
|
||||
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml" } } };
|
||||
var request = new RequestMessage(new UrlDetails($"{_server.Urls[0]}{prepend}/{_guid}{append}"), "POST", ClientIp, new BodyData { DetectedBodyType = BodyType.Json, BodyAsJson = new { a = 1 } }, headers);
|
||||
var responseBuilder = Response.Create().WithProxy(_server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var response = await responseBuilder.ProvideResponseAsync(request, _settings).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(request.ProxyUrl).IsNotNull();
|
||||
Check.That(response.Message.BodyData.BodyAsString).IsEqualTo(expectedBody);
|
||||
Check.That(response.Message.StatusCode).IsEqualTo(201);
|
||||
Check.That(response.Message.Headers["Content-Type"].ToString()).IsEqualTo("application/json");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Response_WithProxy_WebProxySettings()
|
||||
{
|
||||
// Assign
|
||||
var settings = new ProxyAndRecordSettings
|
||||
Url = "http://test.nl",
|
||||
WebProxySettings = new WebProxySettings
|
||||
{
|
||||
Url = "http://test.nl",
|
||||
WebProxySettings = new WebProxySettings
|
||||
{
|
||||
Address = "http://company",
|
||||
UserName = "x",
|
||||
Password = "y"
|
||||
}
|
||||
};
|
||||
var responseBuilder = Response.Create().WithProxy(settings);
|
||||
Address = "http://company",
|
||||
UserName = "x",
|
||||
Password = "y"
|
||||
}
|
||||
};
|
||||
var responseBuilder = Response.Create().WithProxy(settings);
|
||||
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails($"{_server.Urls[0]}/{_guid}"), "GET", ClientIp);
|
||||
// Act
|
||||
var request = new RequestMessage(new UrlDetails($"{_server.Urls[0]}/{_guid}"), "GET", ClientIp);
|
||||
|
||||
Check.ThatAsyncCode(() => responseBuilder.ProvideResponseAsync(request, _settings)).Throws<HttpRequestException>();
|
||||
}
|
||||
Check.ThatAsyncCode(() => responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings)).Throws<HttpRequestException>();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_server?.Stop();
|
||||
_server?.Dispose();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_server?.Stop();
|
||||
_server?.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user