This commit is contained in:
Stef Heyenrath
2017-05-05 22:25:57 +02:00
parent 84db9bbf0d
commit 7bfd9f3343
19 changed files with 185 additions and 303 deletions

View File

@@ -28,6 +28,14 @@ namespace WireMock.Net.Console.NETCoreApp
server.AllowPartialMapping(); server.AllowPartialMapping();
server
.Given(Request.Create().WithPath("/bbc").UsingGet())
.RespondWith(Response.Create().FromProxyUrl("http://www.bbc.com"));
server
.Given(Request.Create().WithPath("/google").UsingGet())
.RespondWith(Response.Create().FromProxyUrl("http://www.google.com"));
server server
.Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet())
.AtPriority(4) .AtPriority(4)

View File

@@ -78,5 +78,11 @@ namespace WireMock.Admin.Mappings
/// The delay in milliseconds. /// The delay in milliseconds.
/// </value> /// </value>
public int? Delay { get; set; } public int? Delay { get; set; }
/// <summary>
/// Gets or sets the Proxy URL.
/// </summary>
/// <value>ProxyUrl</value>
public string ProxyUrl { get; set; }
} }
} }

View File

@@ -16,7 +16,7 @@ namespace WireMock
_responseMessageFunc = responseMessageFunc; _responseMessageFunc = responseMessageFunc;
} }
public Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage) public Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
{ {
return Task.FromResult(_responseMessageFunc(requestMessage)); return Task.FromResult(_responseMessageFunc(requestMessage));
} }

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
namespace WireMock namespace WireMock
{ {
@@ -10,12 +11,8 @@ namespace WireMock
/// <summary> /// <summary>
/// The provide response. /// The provide response.
/// </summary> /// </summary>
/// <param name="requestMessage"> /// <param name="requestMessage">The request.</param>
/// The request. /// <returns>The <see cref="ResponseMessage"/>.</returns>
/// </param> Task<ResponseMessage> ProvideResponseAsync([NotNull] RequestMessage requestMessage);
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage);
} }
} }

View File

@@ -65,10 +65,10 @@ namespace WireMock
/// The response to. /// The response to.
/// </summary> /// </summary>
/// <param name="requestMessage">The request message.</param> /// <param name="requestMessage">The request message.</param>
/// <returns>The <see cref="Task"/>.</returns> /// <returns>The <see cref="ResponseMessage"/>.</returns>
public async Task<ResponseMessage> ResponseTo(RequestMessage requestMessage) public async Task<ResponseMessage> ResponseToAsync(RequestMessage requestMessage)
{ {
return await Provider.ProvideResponse(requestMessage); return await Provider.ProvideResponseAsync(requestMessage);
} }
/// <summary> /// <summary>

View File

@@ -103,7 +103,7 @@ namespace WireMock.Owin
} }
} }
response = await targetMapping.ResponseTo(request); response = await targetMapping.ResponseToAsync(request);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -17,7 +17,7 @@ namespace WireMock.ResponseBuilders
IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null); IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null);
/// <summary> /// <summary>
/// The with body. /// The with body as Json.
/// </summary> /// </summary>
/// <param name="body">The body.</param> /// <param name="body">The body.</param>
/// <param name="encoding">The body encoding.</param> /// <param name="encoding">The body encoding.</param>

View File

@@ -0,0 +1,17 @@
using JetBrains.Annotations;
namespace WireMock.ResponseBuilders
{
/// <summary>
/// The ProxyResponseBuilder interface.
/// </summary>
public interface IProxyResponseBuilder : IStatusCodeResponseBuilder
{
/// <summary>
/// From Proxy URL.
/// </summary>
/// <param name="proxyUrl">The proxy url.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder FromProxyUrl([NotNull] string proxyUrl);
}
}

View File

@@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// The ResponseBuilder interface. /// The ResponseBuilder interface.
/// </summary> /// </summary>
public interface IResponseBuilder : IStatusCodeResponseBuilder public interface IResponseBuilder : IProxyResponseBuilder
{ {
} }
} }

View File

@@ -10,18 +10,14 @@ namespace WireMock.ResponseBuilders
/// <summary> /// <summary>
/// The with status code. /// The with status code.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">The code.</param>
/// The code.
/// </param>
/// <returns>The <see cref="IResponseBuilder"/>.</returns> /// <returns>The <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithStatusCode(int code); IResponseBuilder WithStatusCode(int code);
/// <summary> /// <summary>
/// The with status code. /// The with status code.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">The code.</param>
/// The code.
/// </param>
/// <returns>The <see cref="IResponseBuilder"/>.</returns> /// <returns>The <see cref="IResponseBuilder"/>.</returns>
IResponseBuilder WithStatusCode(HttpStatusCode code); IResponseBuilder WithStatusCode(HttpStatusCode code);

View File

@@ -1,7 +1,7 @@
namespace WireMock.ResponseBuilders namespace WireMock.ResponseBuilders
{ {
/// <summary> /// <summary>
/// The BodyResponseBuilder interface. /// The TransformResponseBuilder interface.
/// </summary> /// </summary>
public interface ITransformResponseBuilder : IDelayResponseBuilder public interface ITransformResponseBuilder : IDelayResponseBuilder
{ {

View File

@@ -1,6 +1,9 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using HandlebarsDotNet; using HandlebarsDotNet;
@@ -28,6 +31,11 @@ namespace WireMock.ResponseBuilders
/// </value> /// </value>
public bool UseTransformer { get; private set; } public bool UseTransformer { get; private set; }
/// <summary>
/// The Proxy URL to use.
/// </summary>
public string ProxyUrl { get; private set; }
/// <summary> /// <summary>
/// Gets the response message. /// Gets the response message.
/// </summary> /// </summary>
@@ -184,7 +192,7 @@ namespace WireMock.ResponseBuilders
/// <param name="bodyAsbase64">The body asbase64.</param> /// <param name="bodyAsbase64">The body asbase64.</param>
/// <param name="encoding">The Encoding.</param> /// <param name="encoding">The Encoding.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns> /// <returns>A <see cref="IResponseBuilder"/>.</returns>
public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null) public IResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, Encoding encoding = null)
{ {
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64)); Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
@@ -230,19 +238,72 @@ namespace WireMock.ResponseBuilders
return WithDelay(TimeSpan.FromMilliseconds(milliseconds)); return WithDelay(TimeSpan.FromMilliseconds(milliseconds));
} }
/// <summary>
/// From Proxy URL.
/// </summary>
/// <param name="proxyUrl">The proxy url.</param>
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
[PublicAPI]
public IResponseBuilder FromProxyUrl(string proxyUrl)
{
Check.NotEmpty(proxyUrl, nameof(proxyUrl));
ProxyUrl = proxyUrl;
return this;
}
/// <summary> /// <summary>
/// The provide response. /// The provide response.
/// </summary> /// </summary>
/// <param name="requestMessage"> /// <param name="requestMessage">The request.</param>
/// The request. /// <returns>The <see cref="ResponseMessage"/>.</returns>
/// </param> public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
/// <returns>
/// The <see cref="Task"/>.
/// </returns>
public async Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage)
{ {
Check.NotNull(requestMessage, nameof(requestMessage));
ResponseMessage responseMessage; ResponseMessage responseMessage;
if (UseTransformer)
if (ProxyUrl != null)
{
using (var client = new HttpClient())
{
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), ProxyUrl);
// Overwrite the host header
httpRequestMessage.Headers.Host = new Uri(ProxyUrl).Authority;
// Set headers if present
if (requestMessage.Headers != null)
{
foreach (var headerName in requestMessage.Headers.Keys.Where(k => k.ToUpper() != "HOST"))
{
httpRequestMessage.Headers.Add(headerName, new[] { requestMessage.Headers[headerName] });
}
}
// Set Body if present
if (requestMessage.BodyAsBytes != null && requestMessage.BodyAsBytes.Length > 0)
{
httpRequestMessage.Content = new ByteArrayContent(requestMessage.BodyAsBytes);
}
// Call the URL
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead);
// Transform response
responseMessage = new ResponseMessage
{
StatusCode = (int)httpResponseMessage.StatusCode,
Body = await httpResponseMessage.Content.ReadAsStringAsync()
};
foreach (var header in httpResponseMessage.Headers)
{
responseMessage.AddHeader(header.Key, header.Value.FirstOrDefault());
}
}
}
else if (UseTransformer)
{ {
responseMessage = new ResponseMessage { StatusCode = ResponseMessage.StatusCode, BodyOriginal = ResponseMessage.Body }; responseMessage = new ResponseMessage { StatusCode = ResponseMessage.StatusCode, BodyOriginal = ResponseMessage.Body };

View File

@@ -466,6 +466,14 @@ namespace WireMock.Server
{ {
IResponseBuilder responseBuilder = Response.Create(); IResponseBuilder responseBuilder = Response.Create();
if (responseModel.Delay > 0)
responseBuilder = responseBuilder.WithDelay(responseModel.Delay.Value);
if (!string.IsNullOrEmpty(responseModel.ProxyUrl))
{
return responseBuilder.FromProxyUrl(responseModel.ProxyUrl);
}
if (responseModel.StatusCode.HasValue) if (responseModel.StatusCode.HasValue)
responseBuilder = responseBuilder.WithStatusCode(responseModel.StatusCode.Value); responseBuilder = responseBuilder.WithStatusCode(responseModel.StatusCode.Value);
@@ -492,9 +500,6 @@ namespace WireMock.Server
if (responseModel.UseTransformer) if (responseModel.UseTransformer)
responseBuilder = responseBuilder.WithTransformer(); responseBuilder = responseBuilder.WithTransformer();
if (responseModel.Delay > 0)
responseBuilder = responseBuilder.WithDelay(responseModel.Delay.Value);
return responseBuilder; return responseBuilder;
} }
@@ -511,7 +516,7 @@ namespace WireMock.Server
var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>(); var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>(); var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
return new MappingModel var mappingModel = new MappingModel
{ {
Guid = mapping.Guid, Guid = mapping.Guid,
Title = mapping.Title, Title = mapping.Title,
@@ -562,20 +567,36 @@ namespace WireMock.Server
}, },
Response = new ResponseModel Response = new ResponseModel
{ {
StatusCode = response.ResponseMessage.StatusCode, Delay = response.Delay?.Milliseconds
Headers = response.ResponseMessage.Headers, }
Body = response.ResponseMessage.Body, };
UseTransformer = response.UseTransformer,
Delay = response.Delay?.Milliseconds,
BodyEncoding = response.ResponseMessage.BodyEncoding != null ? new EncodingModel if (!string.IsNullOrEmpty(response.ProxyUrl))
{
mappingModel.Response.StatusCode = null;
mappingModel.Response.Headers = null;
mappingModel.Response.Body = null;
mappingModel.Response.UseTransformer = false;
mappingModel.Response.BodyEncoding = null;
mappingModel.Response.ProxyUrl = response.ProxyUrl;
}
else
{
mappingModel.Response.StatusCode = response.ResponseMessage.StatusCode;
mappingModel.Response.Headers = response.ResponseMessage.Headers;
mappingModel.Response.Body = response.ResponseMessage.Body;
mappingModel.Response.UseTransformer = response.UseTransformer;
mappingModel.Response.BodyEncoding = response.ResponseMessage.BodyEncoding != null
? new EncodingModel
{ {
EncodingName = response.ResponseMessage.BodyEncoding.EncodingName, EncodingName = response.ResponseMessage.BodyEncoding.EncodingName,
CodePage = response.ResponseMessage.BodyEncoding.CodePage, CodePage = response.ResponseMessage.BodyEncoding.CodePage,
WebName = response.ResponseMessage.BodyEncoding.WebName WebName = response.ResponseMessage.BodyEncoding.WebName
} : null }
} : null;
}; }
return mappingModel;
} }
private MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers) private MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)

View File

@@ -6,7 +6,6 @@ using System.Linq;
using System.Text; using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Http; using WireMock.Http;
using WireMock.Logging;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
@@ -21,9 +20,7 @@ namespace WireMock.Server
public partial class FluentMockServer : IDisposable public partial class FluentMockServer : IDisposable
{ {
private readonly IOwinSelfHost _httpServer; private readonly IOwinSelfHost _httpServer;
private readonly object _syncRoot = new object(); private readonly object _syncRoot = new object();
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions(); private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
/// <summary> /// <summary>
@@ -56,6 +53,7 @@ namespace WireMock.Server
} }
} }
#region Start/Stop
/// <summary> /// <summary>
/// Starts the specified settings. /// Starts the specified settings.
/// </summary> /// </summary>
@@ -185,6 +183,16 @@ namespace WireMock.Server
} }
} }
/// <summary>
/// Stop this server.
/// </summary>
[PublicAPI]
public void Stop()
{
_httpServer?.StopAsync();
}
#endregion
/// <summary> /// <summary>
/// Adds the catch all mapping. /// Adds the catch all mapping.
/// </summary> /// </summary>
@@ -197,15 +205,6 @@ namespace WireMock.Server
.RespondWith(new DynamicResponseProvider(request => new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" })); .RespondWith(new DynamicResponseProvider(request => new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" }));
} }
/// <summary>
/// Stop this server.
/// </summary>
[PublicAPI]
public void Stop()
{
_httpServer?.StopAsync();
}
/// <summary> /// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary> /// </summary>
@@ -264,9 +263,7 @@ namespace WireMock.Server
/// <summary> /// <summary>
/// The add request processing delay. /// The add request processing delay.
/// </summary> /// </summary>
/// <param name="delay"> /// <param name="delay">The delay.</param>
/// The delay.
/// </param>
[PublicAPI] [PublicAPI]
public void AddGlobalProcessingDelay(TimeSpan delay) public void AddGlobalProcessingDelay(TimeSpan delay)
{ {

View File

@@ -46,6 +46,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="System.Net.Http" Version="4.3.0" />
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" /> <PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
<PackageReference Include="Handlebars.Net" Version="1.8.0" /> <PackageReference Include="Handlebars.Net" Version="1.8.0" />
<PackageReference Include="XPath2" Version="1.0.3.1" /> <PackageReference Include="XPath2" Version="1.0.3.1" />
@@ -54,6 +55,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' "> <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Handlebars.NetStandard" Version="1.8.1" /> <PackageReference Include="Handlebars.NetStandard" Version="1.8.1" />
<PackageReference Include="System.Net.Http" Version="4.3.0" />
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" /> <PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" /> <PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
</ItemGroup> </ItemGroup>

View File

@@ -353,6 +353,22 @@ namespace WireMock.Net.Tests
Check.That(watch.ElapsedMilliseconds).IsStrictlyGreaterThan(200); Check.That(watch.ElapsedMilliseconds).IsStrictlyGreaterThan(200);
} }
[Fact]
public async Task Should_proxy_responses()
{
// given
_server = FluentMockServer.Start();
_server
.Given(Request.Create().WithPath("/*"))
.RespondWith(Response.Create().FromProxyUrl("http://www.google.com"));
// when
var result = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
// then
Check.That(result).Contains("google");
}
//[TearDown] //[TearDown]
public void Dispose() public void Dispose()
{ {

View File

@@ -1,104 +0,0 @@
//using System;
//using System.Collections.Generic;
//using System.Net;
//using System.Net.Http;
//using System.Threading;
//using System.Threading.Tasks;
//using NFluent;
//using Xunit;
//using WireMock.Matchers;
//using WireMock.RequestBuilders;
//using WireMock.ResponseBuilders;
//using WireMock.Server;
//namespace WireMock.Net.Tests
//{
// //[TestFixture]
// public class HttpListenerRequestMapperTests : IDisposable
// {
// private FluentMockServer _server;
// public HttpListenerRequestMapperTests()
// {
// _server = FluentMockServer.Start();
// }
// [Fact]
// public async Task Should_map_uri_from_listener_request()
// {
// // given
// var client = new HttpClient();
// // when
// await client.GetAsync(MapperServer.UrlPrefix + "toto");
// // then
// Check.That(MapperServer.LastRequestMessage).IsNotNull();
// Check.That(MapperServer.LastRequestMessage.Path).IsEqualTo("/toto");
// }
// [Fact]
// public async Task Should_map_verb_from_listener_request()
// {
// // given
// var client = new HttpClient();
// // when
// await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
// // then
// Check.That(MapperServer.LastRequestMessage).IsNotNull();
// Check.That(MapperServer.LastRequestMessage.Method).IsEqualTo("put");
// }
// [Fact]
// public async Task Should_map_body_from_listener_request()
// {
// // given
// var client = new HttpClient();
// // when
// await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
// // then
// Check.That(MapperServer.LastRequestMessage).IsNotNull();
// Check.That(MapperServer.LastRequestMessage.Body).IsEqualTo("Hello!");
// }
// [Fact]
// public async Task Should_map_headers_from_listener_request()
// {
// // given
// var client = new HttpClient();
// client.DefaultRequestHeaders.Add("X-Alex", "1706");
// // when
// await client.GetAsync(MapperServer.UrlPrefix);
// // then
// Check.That(MapperServer.LastRequestMessage).IsNotNull();
// Check.That(MapperServer.LastRequestMessage.Headers).Not.IsNullOrEmpty();
// Check.That(MapperServer.LastRequestMessage.Headers.Contains(new KeyValuePair<string, string>("X-Alex", "1706"))).IsTrue();
// }
// [Fact]
// public async Task Should_map_params_from_listener_request()
// {
// // given
// var client = new HttpClient();
// // when
// await client.GetAsync(MapperServer.UrlPrefix + "index.html?id=toto");
// // then
// Check.That(MapperServer.LastRequestMessage).IsNotNull();
// Check.That(MapperServer.LastRequestMessage.Path).EndsWith("/index.html");
// Check.That(MapperServer.LastRequestMessage.GetParameter("id")).HasSize(1);
// }
// public void Dispose()
// {
// _server.Stop().Wait();
// }
// }
//}

View File

@@ -1,135 +0,0 @@
//using System;
//using System.Net;
//using System.Net.Http;
//using System.Text;
//using System.Threading;
//using System.Threading.Tasks;
//using NFluent;
//using Xunit;
//using WireMock.Http;
//using WireMock.Owin;
//namespace WireMock.Net.Tests
//{
// //[TestFixture]
// public class HttpListenerResponseMapperTests : IDisposable
// {
// private TinyHttpServer _server;
// private Task<HttpResponseMessage> _responseMsgTask;
// [Fact]
// public void Should_map_status_code_from_original_response()
// {
// // given
// var response = new ResponseMessage { StatusCode = 404 };
// var httpListenerResponse = CreateHttpListenerResponse();
// // when
// new HttpListenerResponseMapper().Map(response, httpListenerResponse);
// // then
// Check.That(httpListenerResponse.StatusCode).IsEqualTo(404);
// }
// [Fact]
// public void Should_map_headers_from_original_response()
// {
// // given
// var response = new ResponseMessage();
// response.AddHeader("cache-control", "no-cache");
// var httpListenerResponse = CreateHttpListenerResponse();
// // when
// new HttpListenerResponseMapper().Map(response, httpListenerResponse);
// // then
// Check.That(httpListenerResponse.Headers).HasSize(1);
// Check.That(httpListenerResponse.Headers.Keys).Contains("cache-control");
// Check.That(httpListenerResponse.Headers.Get("cache-control")).Contains("no-cache");
// }
// [Fact]
// public void Should_map_body_from_original_response()
// {
// // given
// var response = new ResponseMessage
// {
// Body = "Hello !!!"
// };
// var httpListenerResponse = CreateHttpListenerResponse();
// // when
// new OwinResponseMapper().Map(response, httpListenerResponse);
// // then
// var responseMessage = ToResponseMessage(httpListenerResponse);
// Check.That(responseMessage).IsNotNull();
// var contentTask = responseMessage.Content.ReadAsStringAsync();
// Check.That(contentTask.Result).IsEqualTo("Hello !!!");
// }
// [Fact]
// public void Should_map_encoded_body_from_original_response()
// {
// // given
// var response = new ResponseMessage
// {
// Body = "Hello !!!",
// BodyEncoding = Encoding.ASCII
// };
// var httpListenerResponse = CreateHttpListenerResponse();
// // when
// new HttpListenerResponseMapper().Map(response, httpListenerResponse);
// // then
// Check.That(httpListenerResponse.ContentEncoding).Equals(Encoding.ASCII);
// var responseMessage = ToResponseMessage(httpListenerResponse);
// Check.That(responseMessage).IsNotNull();
// var contentTask = responseMessage.Content.ReadAsStringAsync();
// Check.That(contentTask.Result).IsEqualTo("Hello !!!");
// }
// //[TearDown]
// public void Dispose()
// {
// _server?.Stop();
// }
// /// <summary>
// /// Dirty HACK to get HttpListenerResponse instances
// /// </summary>
// /// <returns>
// /// The <see cref="HttpListenerResponse"/>.
// /// </returns>
// public HttpListenerResponse CreateHttpListenerResponse()
// {
// var port = PortUtil.FindFreeTcpPort();
// var urlPrefix = "http://localhost:" + port + "/";
// var responseReady = new AutoResetEvent(false);
// HttpListenerResponse response = null;
// _server = new TinyHttpServer(
// (context, token) =>
// {
// response = context.Response;
// responseReady.Set();
// }, urlPrefix);
// _server.Start();
// _responseMsgTask = new HttpClient().GetAsync(urlPrefix);
// responseReady.WaitOne();
// return response;
// }
// public HttpResponseMessage ToResponseMessage(HttpListenerResponse listenerResponse)
// {
// listenerResponse.Close();
// _responseMsgTask.Wait();
// return _responseMsgTask.Result;
// }
// }
//}

View File

@@ -24,7 +24,7 @@ namespace WireMock.Net.Tests
.WithTransformer(); .WithTransformer();
// act // act
var responseMessage = await response.ProvideResponse(request); var responseMessage = await response.ProvideResponseAsync(request);
// then // then
Check.That(responseMessage.Body).Equals("test http://localhost/foo /foo post"); Check.That(responseMessage.Body).Equals("test http://localhost/foo /foo post");
@@ -43,7 +43,7 @@ namespace WireMock.Net.Tests
.WithTransformer(); .WithTransformer();
// act // act
var responseMessage = await response.ProvideResponse(request); var responseMessage = await response.ProvideResponseAsync(request);
// then // then
Check.That(responseMessage.Body).Equals("test keya=1 idx=1 idx=2 keyb=5"); Check.That(responseMessage.Body).Equals("test keya=1 idx=1 idx=2 keyb=5");
@@ -60,7 +60,7 @@ namespace WireMock.Net.Tests
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer(); var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer();
// act // act
var responseMessage = await response.ProvideResponse(request); var responseMessage = await response.ProvideResponseAsync(request);
// then // then
Check.That(responseMessage.Body).Equals("test"); Check.That(responseMessage.Body).Equals("test");
@@ -78,7 +78,7 @@ namespace WireMock.Net.Tests
var response = Response.Create().WithBody("test", Encoding.ASCII); var response = Response.Create().WithBody("test", Encoding.ASCII);
// act // act
var responseMessage = await response.ProvideResponse(request); var responseMessage = await response.ProvideResponseAsync(request);
// then // then
Check.That(responseMessage.Body).Equals("test"); Check.That(responseMessage.Body).Equals("test");
@@ -96,7 +96,7 @@ namespace WireMock.Net.Tests
var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII); var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII);
// act // act
var responseMessage = await response.ProvideResponse(request); var responseMessage = await response.ProvideResponseAsync(request);
// then // then
Check.That(responseMessage.Body).Equals("{\"value\":\"test\"}"); Check.That(responseMessage.Body).Equals("{\"value\":\"test\"}");