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

@@ -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()
{

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();
// 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\"}");