merge netstandard into main (#26)

* #23

* #23 "Newtonsoft.Json" Version="10.0.2"

* owin

* AspNetCore

* Fix appveyor build

* fix start/stop in untitests
This commit is contained in:
Stef Heyenrath
2017-04-26 21:28:12 +02:00
committed by GitHub
parent 0f8f9c508f
commit 453cef90e5
106 changed files with 17753 additions and 24342 deletions

View File

@@ -174,7 +174,7 @@ namespace WireMock.Net.Tests
Check.That(_server.LogEntries).HasSize(1);
var requestLogged = _server.LogEntries.First();
Check.That(requestLogged.RequestMessage.Method).IsEqualTo("get");
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
Check.That(requestLogged.RequestMessage.BodyAsBytes).IsEmpty();
}
[Fact]

View File

@@ -1,39 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using NFluent;
using Xunit;
using WireMock.Http;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
namespace WireMock.Net.Tests.Http
{
//[TestFixture]
public class TinyHttpServerTests
{
[Fact]
public void Should_call_handler_on_request()
{
// given
var port = PortUtil.FindFreeTcpPort();
bool called = false;
var urlPrefix = "http://localhost:" + port + "/";
var server = new TinyHttpServer((ctx, token) => called = true, urlPrefix);
server.Start();
// when
var httpClient = new HttpClient();
httpClient.GetAsync(urlPrefix).Wait(3000);
// then
Check.That(called).IsTrue();
}
}
}

View File

@@ -1,148 +1,104 @@
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.Http;
//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 MapperServer _server;
//namespace WireMock.Net.Tests
//{
// //[TestFixture]
// public class HttpListenerRequestMapperTests : IDisposable
// {
// private FluentMockServer _server;
//[SetUp]
public HttpListenerRequestMapperTests()
{
_server = MapperServer.Start();
}
// public HttpListenerRequestMapperTests()
// {
// _server = FluentMockServer.Start();
// }
[Fact]
public async Task Should_map_uri_from_listener_request()
{
// given
var client = new HttpClient();
// [Fact]
// public async Task Should_map_uri_from_listener_request()
// {
// // given
// var client = new HttpClient();
// when
await client.GetAsync(MapperServer.UrlPrefix + "toto");
// // when
// await client.GetAsync(MapperServer.UrlPrefix + "toto");
// then
Check.That(MapperServer.LastRequestMessage).IsNotNull();
Check.That(MapperServer.LastRequestMessage.Path).IsEqualTo("/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();
// [Fact]
// public async Task Should_map_verb_from_listener_request()
// {
// // given
// var client = new HttpClient();
// when
await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
// // when
// await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
// then
Check.That(MapperServer.LastRequestMessage).IsNotNull();
Check.That(MapperServer.LastRequestMessage.Method).IsEqualTo("put");
}
// // 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();
// [Fact]
// public async Task Should_map_body_from_listener_request()
// {
// // given
// var client = new HttpClient();
// when
await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
// // when
// await client.PutAsync(MapperServer.UrlPrefix, new StringContent("Hello!"));
// then
Check.That(MapperServer.LastRequestMessage).IsNotNull();
Check.That(MapperServer.LastRequestMessage.Body).IsEqualTo("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");
// [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);
// // 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();
}
// // 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();
// [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");
// // 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);
}
// // then
// Check.That(MapperServer.LastRequestMessage).IsNotNull();
// Check.That(MapperServer.LastRequestMessage.Path).EndsWith("/index.html");
// Check.That(MapperServer.LastRequestMessage.GetParameter("id")).HasSize(1);
// }
//[TearDown]
public void Dispose()
{
_server.Stop();
}
private class MapperServer : TinyHttpServer
{
private static volatile RequestMessage _lastRequestMessage;
private MapperServer(Action<HttpListenerContext, CancellationToken> httpHandler, string urlPrefix) : base(httpHandler, urlPrefix)
{
}
public static RequestMessage LastRequestMessage
{
get
{
return _lastRequestMessage;
}
private set
{
_lastRequestMessage = value;
}
}
public static string UrlPrefix { get; private set; }
public new static MapperServer Start()
{
int port = PortUtil.FindFreeTcpPort();
UrlPrefix = "http://localhost:" + port + "/";
var server = new MapperServer(
(context, token) =>
{
LastRequestMessage = new HttpListenerRequestMapper().Map(context.Request);
context.Response.Close();
}, UrlPrefix);
((TinyHttpServer)server).Start();
return server;
}
public new void Stop()
{
base.Stop();
LastRequestMessage = null;
}
}
}
}
// public void Dispose()
// {
// _server.Stop().Wait();
// }
// }
//}

View File

@@ -1,134 +1,135 @@
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 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;
//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();
// [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);
// // when
// new HttpListenerResponseMapper().Map(response, httpListenerResponse);
// then
Check.That(httpListenerResponse.StatusCode).IsEqualTo(404);
}
// // 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();
// [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);
// // 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");
}
// // 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 !!!"
};
// [Fact]
// public void Should_map_body_from_original_response()
// {
// // given
// var response = new ResponseMessage
// {
// Body = "Hello !!!"
// };
var httpListenerResponse = CreateHttpListenerResponse();
// var httpListenerResponse = CreateHttpListenerResponse();
// when
new HttpListenerResponseMapper().Map(response, httpListenerResponse);
// // when
// new OwinResponseMapper().Map(response, httpListenerResponse);
// then
var responseMessage = ToResponseMessage(httpListenerResponse);
Check.That(responseMessage).IsNotNull();
// // then
// var responseMessage = ToResponseMessage(httpListenerResponse);
// Check.That(responseMessage).IsNotNull();
var contentTask = responseMessage.Content.ReadAsStringAsync();
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
}
// 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
};
// [Fact]
// public void Should_map_encoded_body_from_original_response()
// {
// // given
// var response = new ResponseMessage
// {
// Body = "Hello !!!",
// BodyEncoding = Encoding.ASCII
// };
var httpListenerResponse = CreateHttpListenerResponse();
// var httpListenerResponse = CreateHttpListenerResponse();
// when
new HttpListenerResponseMapper().Map(response, httpListenerResponse);
// // when
// new HttpListenerResponseMapper().Map(response, httpListenerResponse);
// then
Check.That(httpListenerResponse.ContentEncoding).Equals(Encoding.ASCII);
// // then
// Check.That(httpListenerResponse.ContentEncoding).Equals(Encoding.ASCII);
var responseMessage = ToResponseMessage(httpListenerResponse);
Check.That(responseMessage).IsNotNull();
// var responseMessage = ToResponseMessage(httpListenerResponse);
// Check.That(responseMessage).IsNotNull();
var contentTask = responseMessage.Content.ReadAsStringAsync();
Check.That(contentTask.Result).IsEqualTo("Hello !!!");
}
// var contentTask = responseMessage.Content.ReadAsStringAsync();
// Check.That(contentTask.Result).IsEqualTo("Hello !!!");
// }
//[TearDown]
public void Dispose()
{
_server?.Stop();
}
// //[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;
}
// /// <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;
}
}
}
// public HttpResponseMessage ToResponseMessage(HttpListenerResponse listenerResponse)
// {
// listenerResponse.Close();
// _responseMsgTask.Wait();
// return _responseMsgTask.Result;
// }
// }
//}

View File

@@ -15,27 +15,19 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<!--<PackageReference Include="Microsoft.Extensions.Testing.Abstractions" Version="1.0.0-preview2-1-003177" />-->
<PackageReference Include="Microsoft.Owin.Host.HttpListener" Version="3.1.0" />
<PackageReference Include="Moq" Version="4.7.8" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<!--<PackageReference Include="NUnit" Version="3.6.1" />
<PackageReference Include="dotnet-test-nunit" Version="3.4.0-beta-3" />-->
<PackageReference Include="NFluent" Version="2.0.0-alpha" />
<PackageReference Include="OpenCover" Version="4.6.519" />
<PackageReference Include="ReportGenerator" Version="2.5.6" />
<PackageReference Include="SimMetrics.Net" Version="1.0.3" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="System.Threading" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.3.0-beta1-build3642" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta1-build1309" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
<Reference Include="System.Net.Http" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<ItemGroup>
@@ -47,4 +39,8 @@
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>