mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-25 09:48:28 +02:00
Proxy (#15)
This commit is contained in:
@@ -28,6 +28,14 @@ namespace WireMock.Net.Console.NETCoreApp
|
||||
|
||||
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
|
||||
.Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet())
|
||||
.AtPriority(4)
|
||||
|
||||
@@ -78,5 +78,11 @@ namespace WireMock.Admin.Mappings
|
||||
/// The delay in milliseconds.
|
||||
/// </value>
|
||||
public int? Delay { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Proxy URL.
|
||||
/// </summary>
|
||||
/// <value>ProxyUrl</value>
|
||||
public string ProxyUrl { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ namespace WireMock
|
||||
_responseMessageFunc = responseMessageFunc;
|
||||
}
|
||||
|
||||
public Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage)
|
||||
public Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
|
||||
{
|
||||
return Task.FromResult(_responseMessageFunc(requestMessage));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock
|
||||
{
|
||||
@@ -10,12 +11,8 @@ namespace WireMock
|
||||
/// <summary>
|
||||
/// The provide response.
|
||||
/// </summary>
|
||||
/// <param name="requestMessage">
|
||||
/// The request.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="Task"/>.
|
||||
/// </returns>
|
||||
Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage);
|
||||
/// <param name="requestMessage">The request.</param>
|
||||
/// <returns>The <see cref="ResponseMessage"/>.</returns>
|
||||
Task<ResponseMessage> ProvideResponseAsync([NotNull] RequestMessage requestMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,10 +65,10 @@ namespace WireMock
|
||||
/// The response to.
|
||||
/// </summary>
|
||||
/// <param name="requestMessage">The request message.</param>
|
||||
/// <returns>The <see cref="Task"/>.</returns>
|
||||
public async Task<ResponseMessage> ResponseTo(RequestMessage requestMessage)
|
||||
/// <returns>The <see cref="ResponseMessage"/>.</returns>
|
||||
public async Task<ResponseMessage> ResponseToAsync(RequestMessage requestMessage)
|
||||
{
|
||||
return await Provider.ProvideResponse(requestMessage);
|
||||
return await Provider.ProvideResponseAsync(requestMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -103,7 +103,7 @@ namespace WireMock.Owin
|
||||
}
|
||||
}
|
||||
|
||||
response = await targetMapping.ResponseTo(request);
|
||||
response = await targetMapping.ResponseToAsync(request);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace WireMock.ResponseBuilders
|
||||
IResponseBuilder WithBody([NotNull] string body, [CanBeNull] Encoding encoding = null);
|
||||
|
||||
/// <summary>
|
||||
/// The with body.
|
||||
/// The with body as Json.
|
||||
/// </summary>
|
||||
/// <param name="body">The body.</param>
|
||||
/// <param name="encoding">The body encoding.</param>
|
||||
|
||||
17
src/WireMock.Net/ResponseBuilders/IProxyResponseBuilder.cs
Normal file
17
src/WireMock.Net/ResponseBuilders/IProxyResponseBuilder.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// The ResponseBuilder interface.
|
||||
/// </summary>
|
||||
public interface IResponseBuilder : IStatusCodeResponseBuilder
|
||||
public interface IResponseBuilder : IProxyResponseBuilder
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -10,18 +10,14 @@ namespace WireMock.ResponseBuilders
|
||||
/// <summary>
|
||||
/// The with status code.
|
||||
/// </summary>
|
||||
/// <param name="code">
|
||||
/// The code.
|
||||
/// </param>
|
||||
/// <param name="code">The code.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithStatusCode(int code);
|
||||
|
||||
/// <summary>
|
||||
/// The with status code.
|
||||
/// </summary>
|
||||
/// <param name="code">
|
||||
/// The code.
|
||||
/// </param>
|
||||
/// <param name="code">The code.</param>
|
||||
/// <returns>The <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithStatusCode(HttpStatusCode code);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
namespace WireMock.ResponseBuilders
|
||||
{
|
||||
/// <summary>
|
||||
/// The BodyResponseBuilder interface.
|
||||
/// The TransformResponseBuilder interface.
|
||||
/// </summary>
|
||||
public interface ITransformResponseBuilder : IDelayResponseBuilder
|
||||
{
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using HandlebarsDotNet;
|
||||
@@ -28,6 +31,11 @@ namespace WireMock.ResponseBuilders
|
||||
/// </value>
|
||||
public bool UseTransformer { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Proxy URL to use.
|
||||
/// </summary>
|
||||
public string ProxyUrl { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the response message.
|
||||
/// </summary>
|
||||
@@ -184,7 +192,7 @@ namespace WireMock.ResponseBuilders
|
||||
/// <param name="bodyAsbase64">The body asbase64.</param>
|
||||
/// <param name="encoding">The Encoding.</param>
|
||||
/// <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));
|
||||
|
||||
@@ -230,19 +238,72 @@ namespace WireMock.ResponseBuilders
|
||||
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>
|
||||
/// The provide response.
|
||||
/// </summary>
|
||||
/// <param name="requestMessage">
|
||||
/// The request.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="Task"/>.
|
||||
/// </returns>
|
||||
public async Task<ResponseMessage> ProvideResponse(RequestMessage requestMessage)
|
||||
/// <param name="requestMessage">The request.</param>
|
||||
/// <returns>The <see cref="ResponseMessage"/>.</returns>
|
||||
public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
|
||||
{
|
||||
Check.NotNull(requestMessage, nameof(requestMessage));
|
||||
|
||||
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 };
|
||||
|
||||
|
||||
@@ -466,6 +466,14 @@ namespace WireMock.Server
|
||||
{
|
||||
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)
|
||||
responseBuilder = responseBuilder.WithStatusCode(responseModel.StatusCode.Value);
|
||||
|
||||
@@ -492,9 +500,6 @@ namespace WireMock.Server
|
||||
if (responseModel.UseTransformer)
|
||||
responseBuilder = responseBuilder.WithTransformer();
|
||||
|
||||
if (responseModel.Delay > 0)
|
||||
responseBuilder = responseBuilder.WithDelay(responseModel.Delay.Value);
|
||||
|
||||
return responseBuilder;
|
||||
}
|
||||
|
||||
@@ -511,7 +516,7 @@ namespace WireMock.Server
|
||||
var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
|
||||
var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
|
||||
|
||||
return new MappingModel
|
||||
var mappingModel = new MappingModel
|
||||
{
|
||||
Guid = mapping.Guid,
|
||||
Title = mapping.Title,
|
||||
@@ -562,20 +567,36 @@ namespace WireMock.Server
|
||||
},
|
||||
Response = new ResponseModel
|
||||
{
|
||||
StatusCode = response.ResponseMessage.StatusCode,
|
||||
Headers = response.ResponseMessage.Headers,
|
||||
Body = response.ResponseMessage.Body,
|
||||
UseTransformer = response.UseTransformer,
|
||||
Delay = response.Delay?.Milliseconds,
|
||||
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,
|
||||
CodePage = response.ResponseMessage.BodyEncoding.CodePage,
|
||||
WebName = response.ResponseMessage.BodyEncoding.WebName
|
||||
} : null
|
||||
}
|
||||
};
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
return mappingModel;
|
||||
}
|
||||
|
||||
private MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
||||
|
||||
@@ -6,7 +6,6 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Http;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
@@ -21,9 +20,7 @@ namespace WireMock.Server
|
||||
public partial class FluentMockServer : IDisposable
|
||||
{
|
||||
private readonly IOwinSelfHost _httpServer;
|
||||
|
||||
private readonly object _syncRoot = new object();
|
||||
|
||||
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||
|
||||
/// <summary>
|
||||
@@ -56,6 +53,7 @@ namespace WireMock.Server
|
||||
}
|
||||
}
|
||||
|
||||
#region Start/Stop
|
||||
/// <summary>
|
||||
/// Starts the specified settings.
|
||||
/// </summary>
|
||||
@@ -185,6 +183,16 @@ namespace WireMock.Server
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop this server.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void Stop()
|
||||
{
|
||||
_httpServer?.StopAsync();
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Adds the catch all mapping.
|
||||
/// </summary>
|
||||
@@ -197,15 +205,6 @@ namespace WireMock.Server
|
||||
.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>
|
||||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
||||
/// </summary>
|
||||
@@ -264,9 +263,7 @@ namespace WireMock.Server
|
||||
/// <summary>
|
||||
/// The add request processing delay.
|
||||
/// </summary>
|
||||
/// <param name="delay">
|
||||
/// The delay.
|
||||
/// </param>
|
||||
/// <param name="delay">The delay.</param>
|
||||
[PublicAPI]
|
||||
public void AddGlobalProcessingDelay(TimeSpan delay)
|
||||
{
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
|
||||
<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="Handlebars.Net" Version="1.8.0" />
|
||||
<PackageReference Include="XPath2" Version="1.0.3.1" />
|
||||
@@ -54,6 +55,7 @@
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.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.XPath.XmlDocument" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -353,6 +353,22 @@ namespace WireMock.Net.Tests
|
||||
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]
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -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;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -24,7 +24,7 @@ namespace WireMock.Net.Tests
|
||||
.WithTransformer();
|
||||
|
||||
// act
|
||||
var responseMessage = await response.ProvideResponse(request);
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// then
|
||||
Check.That(responseMessage.Body).Equals("test http://localhost/foo /foo post");
|
||||
@@ -43,7 +43,7 @@ namespace WireMock.Net.Tests
|
||||
.WithTransformer();
|
||||
|
||||
// act
|
||||
var responseMessage = await response.ProvideResponse(request);
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// then
|
||||
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();
|
||||
|
||||
// act
|
||||
var responseMessage = await response.ProvideResponse(request);
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// then
|
||||
Check.That(responseMessage.Body).Equals("test");
|
||||
@@ -78,7 +78,7 @@ namespace WireMock.Net.Tests
|
||||
var response = Response.Create().WithBody("test", Encoding.ASCII);
|
||||
|
||||
// act
|
||||
var responseMessage = await response.ProvideResponse(request);
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// then
|
||||
Check.That(responseMessage.Body).Equals("test");
|
||||
@@ -96,7 +96,7 @@ namespace WireMock.Net.Tests
|
||||
var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII);
|
||||
|
||||
// act
|
||||
var responseMessage = await response.ProvideResponse(request);
|
||||
var responseMessage = await response.ProvideResponseAsync(request);
|
||||
|
||||
// then
|
||||
Check.That(responseMessage.Body).Equals("{\"value\":\"test\"}");
|
||||
|
||||
Reference in New Issue
Block a user