Add the Host, Protocol, Port and Origin to the Request message so they can be used in templating (#62)

* feat: Add the Host, Protocol Port and Origin to the Request message

- Add new fields during request message creation, derived from the Uri object
- Allow the new fields to be accessed in the templating engine
- Add tests for the new fields

* source code file reformat

* appveyor
This commit is contained in:
Alastair Crabtree
2017-11-18 11:03:13 +00:00
committed by Stef Heyenrath
parent cb1117fdaa
commit 6c38400827
3 changed files with 189 additions and 146 deletions

View File

@@ -39,6 +39,6 @@ test_script:
- nuget.exe install coveralls.net -ExcludeVersion - nuget.exe install coveralls.net -ExcludeVersion
- pip install codecov - pip install codecov
- cmd: '"OpenCover\tools\OpenCover.Console.exe" -register:user -target:dotnet.exe -targetargs:"test test\WireMock.Net.Tests\WireMock.Net.Tests.csproj --no-build" -returntargetcode -filter:"+[WireMock.Net]* -[WireMock.Net.Tests*]*" -output:coverage.xml -oldstyle -searchdirs:".\test\WireMock.Net.Tests\bin\%CONFIGURATION%\net452"' - cmd: '"OpenCover\tools\OpenCover.Console.exe" -target:dotnet.exe -targetargs:"test test\WireMock.Net.Tests\WireMock.Net.Tests.csproj --no-build" -output:coverage.xml -returntargetcode -register:user -filter:"+[WireMock.Net]* -[WireMock.Net.Tests*]*" -nodefaultfilters -returntargetcode -oldstyle'
- codecov -f "coverage.xml" - codecov -f "coverage.xml"
- coveralls.net\tools\csmacnz.Coveralls.exe --opencover -i .\coverage.xml - coveralls.net\tools\csmacnz.Coveralls.exe --opencover -i .\coverage.xml

View File

@@ -63,6 +63,26 @@ namespace WireMock
/// </summary> /// </summary>
public string Body { get; } public string Body { get; }
/// <summary>
/// Gets the Host
/// </summary>
public string Host { get; }
/// <summary>
/// Gets the protocol
/// </summary>
public string Protocol { get; }
/// <summary>
/// Gets the port
/// </summary>
public int Port { get; }
/// <summary>
/// Gets the origin
/// </summary>
public string Origin { get; }
/// <summary> /// <summary>
/// Gets the body encoding. /// Gets the body encoding.
/// </summary> /// </summary>
@@ -86,6 +106,10 @@ namespace WireMock
Check.NotNull(clientIP, nameof(clientIP)); Check.NotNull(clientIP, nameof(clientIP));
Url = url.ToString(); Url = url.ToString();
Protocol = url.Scheme;
Host = url.Host;
Port = url.Port;
Origin = $"{url.Scheme}://{url.Host}:{url.Port}";
Path = url.AbsolutePath; Path = url.AbsolutePath;
Method = method.ToLower(); Method = method.ToLower();
ClientIP = clientIP; ClientIP = clientIP;

View File

@@ -86,5 +86,24 @@ namespace WireMock.Net.Tests
Check.That(responseMessage.Headers["x"]).Contains("text/plain"); Check.That(responseMessage.Headers["x"]).Contains("text/plain");
Check.That(responseMessage.Headers["x"]).Contains("http://localhost/foo"); Check.That(responseMessage.Headers["x"]).Contains("http://localhost/foo");
} }
[Fact]
public async Task Response_ProvideResponse_Handlebars_Origin_Port_Protocol_Host()
{
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost:1234"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
var response = Response.Create()
.WithBody("test {{request.origin}} {{request.port}} {{request.protocol}} {{request.host}}")
.WithTransformer();
// act
var responseMessage = await response.ProvideResponseAsync(request);
// then
Check.That(responseMessage.Body).Equals("test http://localhost:1234 1234 http localhost");
}
} }
} }