mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 14:40:00 +02:00
fix (#461)
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WWW/@EntryIndexedValue">WWW</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WWW/@EntryIndexedValue">WWW</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=XMS/@EntryIndexedValue">XMS</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=XMS/@EntryIndexedValue">XMS</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=XUA/@EntryIndexedValue">XUA</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=XUA/@EntryIndexedValue">XUA</s:String>
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Flurl/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=funcs/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=funcs/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Heyenrath/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Heyenrath/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jmes/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jmes/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
.WithHeader("postmanecho", "post")
|
.WithHeader("postmanecho", "post")
|
||||||
)
|
)
|
||||||
.RespondWith(Response.Create()
|
.RespondWith(Response.Create()
|
||||||
.WithProxy(new ProxyAndRecordSettings { Url = "http://postman-echo.com/post" })
|
.WithProxy(new ProxyAndRecordSettings { Url = "http://postman-echo.com" })
|
||||||
);
|
);
|
||||||
|
|
||||||
server
|
server
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ namespace WireMock.Admin.Requests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string AbsoluteUrl { get; set; }
|
public string AbsoluteUrl { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The ProxyUrl (if a proxy is used).
|
||||||
|
/// </summary>
|
||||||
|
public string ProxyUrl { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The query.
|
/// The query.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ namespace WireMock
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string AbsoluteUrl { get; }
|
public string AbsoluteUrl { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The ProxyUrl (if a proxy is used).
|
||||||
|
/// </summary>
|
||||||
|
public string ProxyUrl { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the DateTime.
|
/// Gets the DateTime.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -343,14 +343,22 @@ namespace WireMock.ResponseBuilders
|
|||||||
|
|
||||||
if (ProxyUrl != null && _httpClientForProxy != null)
|
if (ProxyUrl != null && _httpClientForProxy != null)
|
||||||
{
|
{
|
||||||
|
string RemoveFirstOccurrence(string source, string find)
|
||||||
|
{
|
||||||
|
int place = source.IndexOf(find, StringComparison.OrdinalIgnoreCase);
|
||||||
|
return place >= 0 ? source.Remove(place, find.Length) : source;
|
||||||
|
}
|
||||||
|
|
||||||
var requestUri = new Uri(requestMessage.Url);
|
var requestUri = new Uri(requestMessage.Url);
|
||||||
var proxyUri = new Uri(ProxyUrl);
|
|
||||||
var proxyUriWithRequestPathAndQuery = new Uri(proxyUri, requestUri.PathAndQuery);
|
// Build the proxy url and skip duplicates
|
||||||
|
string extra = RemoveFirstOccurrence(requestUri.LocalPath.TrimEnd('/'), new Uri(ProxyUrl).LocalPath.TrimEnd('/'));
|
||||||
|
requestMessage.ProxyUrl = ProxyUrl + extra + requestUri.Query;
|
||||||
|
|
||||||
return await HttpClientHelper.SendAsync(
|
return await HttpClientHelper.SendAsync(
|
||||||
_httpClientForProxy,
|
_httpClientForProxy,
|
||||||
requestMessage,
|
requestMessage,
|
||||||
proxyUriWithRequestPathAndQuery.AbsoluteUri,
|
requestMessage.ProxyUrl,
|
||||||
!settings.DisableJsonBodyParsing.GetValueOrDefault(false),
|
!settings.DisableJsonBodyParsing.GetValueOrDefault(false),
|
||||||
!settings.DisableRequestBodyDecompressing.GetValueOrDefault(false)
|
!settings.DisableRequestBodyDecompressing.GetValueOrDefault(false)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ namespace WireMock.Serialization
|
|||||||
AbsolutePath = logEntry.RequestMessage.AbsolutePath,
|
AbsolutePath = logEntry.RequestMessage.AbsolutePath,
|
||||||
Url = logEntry.RequestMessage.Url,
|
Url = logEntry.RequestMessage.Url,
|
||||||
AbsoluteUrl = logEntry.RequestMessage.AbsoluteUrl,
|
AbsoluteUrl = logEntry.RequestMessage.AbsoluteUrl,
|
||||||
|
ProxyUrl = logEntry.RequestMessage.ProxyUrl,
|
||||||
Query = logEntry.RequestMessage.Query,
|
Query = logEntry.RequestMessage.Query,
|
||||||
Method = logEntry.RequestMessage.Method,
|
Method = logEntry.RequestMessage.Method,
|
||||||
Headers = logEntry.RequestMessage.Headers,
|
Headers = logEntry.RequestMessage.Headers,
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
|||||||
{
|
{
|
||||||
public class ResponseWithProxyTests : IDisposable
|
public class ResponseWithProxyTests : IDisposable
|
||||||
{
|
{
|
||||||
|
private const string ClientIp = "::1";
|
||||||
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
|
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
|
||||||
private readonly WireMockServer _server;
|
private readonly WireMockServer _server;
|
||||||
private readonly Guid _guid;
|
private readonly Guid _guid;
|
||||||
@@ -27,21 +28,32 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
|||||||
_server = WireMockServer.Start();
|
_server = WireMockServer.Start();
|
||||||
_server.Given(Request.Create().UsingPost().WithPath($"/{_guid}"))
|
_server.Given(Request.Create().UsingPost().WithPath($"/{_guid}"))
|
||||||
.RespondWith(Response.Create().WithStatusCode(201).WithBodyAsJson(new { p = 42 }).WithHeader("Content-Type", "application/json"));
|
.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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public async Task Response_WithProxy()
|
[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
|
// Assign
|
||||||
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml" } } };
|
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml" } } };
|
||||||
var request = new RequestMessage(new UrlDetails($"{_server.Urls[0]}/{_guid}"), "POST", "::1", new BodyData { DetectedBodyType = BodyType.Json, BodyAsJson = new { a = 1 } }, headers);
|
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 response = Response.Create().WithProxy(_server.Urls[0]);
|
var response = Response.Create().WithProxy(_server.Urls[0]);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request, _settings);
|
var responseMessage = await response.ProvideResponseAsync(request, _settings);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(responseMessage.BodyData.BodyAsString).IsEqualTo("{\"p\":42}");
|
Check.That(request.ProxyUrl).IsNotNull();
|
||||||
|
Check.That(responseMessage.BodyData.BodyAsString).IsEqualTo(expectedBody);
|
||||||
Check.That(responseMessage.StatusCode).IsEqualTo(201);
|
Check.That(responseMessage.StatusCode).IsEqualTo(201);
|
||||||
Check.That(responseMessage.Headers["Content-Type"].ToString()).IsEqualTo("application/json");
|
Check.That(responseMessage.Headers["Content-Type"].ToString()).IsEqualTo("application/json");
|
||||||
}
|
}
|
||||||
@@ -63,7 +75,7 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
|||||||
var response = Response.Create().WithProxy(settings);
|
var response = Response.Create().WithProxy(settings);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var request = new RequestMessage(new UrlDetails($"{_server.Urls[0]}/{_guid}"), "GET", "::1");
|
var request = new RequestMessage(new UrlDetails($"{_server.Urls[0]}/{_guid}"), "GET", ClientIp);
|
||||||
|
|
||||||
Check.ThatAsyncCode(() => response.ProvideResponseAsync(request, _settings)).Throws<HttpRequestException>();
|
Check.ThatAsyncCode(() => response.ProvideResponseAsync(request, _settings)).Throws<HttpRequestException>();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user