Skip to content

Commit

Permalink
fix (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Apr 29, 2020
1 parent e7949a4 commit 5f4c688
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions WireMock.Net Solution.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -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/=XMS/@EntryIndexedValue">XMS</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/=Heyenrath/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jmes/@EntryIndexedValue">True</s:Boolean>
Expand Down
2 changes: 1 addition & 1 deletion examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static void Run()
.WithHeader("postmanecho", "post")
)
.RespondWith(Response.Create()
.WithProxy(new ProxyAndRecordSettings { Url = "http://postman-echo.com/post" })
.WithProxy(new ProxyAndRecordSettings { Url = "http://postman-echo.com" })
);

server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class LogRequestModel
/// </summary>
public string AbsoluteUrl { get; set; }

/// <summary>
/// The ProxyUrl (if a proxy is used).
/// </summary>
public string ProxyUrl { get; set; }

/// <summary>
/// The query.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/WireMock.Net/RequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class RequestMessage
/// </summary>
public string AbsoluteUrl { get; }

/// <summary>
/// The ProxyUrl (if a proxy is used).
/// </summary>
public string ProxyUrl { get; set; }

/// <summary>
/// Gets the DateTime.
/// </summary>
Expand Down
16 changes: 12 additions & 4 deletions src/WireMock.Net/ResponseBuilders/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,22 @@ public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMe

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 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(
_httpClientForProxy,
_httpClientForProxy,
requestMessage,
proxyUriWithRequestPathAndQuery.AbsoluteUri,
requestMessage.ProxyUrl,
!settings.DisableJsonBodyParsing.GetValueOrDefault(false),
!settings.DisableRequestBodyDecompressing.GetValueOrDefault(false)
);
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Serialization/LogEntryMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static LogEntryModel Map(LogEntry logEntry)
AbsolutePath = logEntry.RequestMessage.AbsolutePath,
Url = logEntry.RequestMessage.Url,
AbsoluteUrl = logEntry.RequestMessage.AbsoluteUrl,
ProxyUrl = logEntry.RequestMessage.ProxyUrl,
Query = logEntry.RequestMessage.Query,
Method = logEntry.RequestMessage.Method,
Headers = logEntry.RequestMessage.Headers,
Expand Down
22 changes: 17 additions & 5 deletions test/WireMock.Net.Tests/ResponseBuilders/ResponseWithProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace WireMock.Net.Tests.ResponseBuilders
{
public class ResponseWithProxyTests : IDisposable
{
private const string ClientIp = "::1";
private readonly WireMockServerSettings _settings = new WireMockServerSettings();
private readonly WireMockServer _server;
private readonly Guid _guid;
Expand All @@ -27,21 +28,32 @@ public ResponseWithProxyTests()
_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"));
}

[Fact]
public async Task Response_WithProxy()
[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]}/{_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]);

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

// 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.Headers["Content-Type"].ToString()).IsEqualTo("application/json");
}
Expand All @@ -63,7 +75,7 @@ public void Response_WithProxy_WebProxySettings()
var response = Response.Create().WithProxy(settings);

// 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>();
}
Expand Down

0 comments on commit 5f4c688

Please sign in to comment.