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

@@ -0,0 +1,65 @@
//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Net;
//using System.Text;
//#if NET45
//#else
//using System.Net.Http;
//#endif
//namespace WireMock.Http
//{
// /// <summary>
// /// The http listener request mapper.
// /// </summary>
// public class HttpListenerRequestMapperOld
// {
// /// <summary>
// /// The map.
// /// </summary>
// /// <param name="listenerRequest">The listener request.</param>
// /// <returns>The <see cref="RequestMessage"/>.</returns>
// public RequestMessage MapAsync(HttpListenerRequest listenerRequest)
// {
// Uri url = listenerRequest.Url;
// string verb = listenerRequest.HttpMethod;
// byte[] body = GetRequestBody(listenerRequest);
// Encoding bodyEncoding = body != null ? listenerRequest.ContentEncoding : null;
// string bodyAsString = bodyEncoding?.GetString(body);
// var listenerHeaders = listenerRequest.Headers;
// var headers = listenerHeaders.AllKeys.ToDictionary(k => k, k => listenerHeaders[k]);
// var cookies = new Dictionary<string, string>();
// foreach (Cookie cookie in listenerRequest.Cookies)
// cookies.Add(cookie.Name, cookie.Value);
// return new RequestMessage(url, verb, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now };
// }
// /// <summary>
// /// The get request body.
// /// </summary>
// /// <param name="request">The request.</param>
// /// <returns>The <see cref="string"/>.</returns>
// private byte[] GetRequestBody(HttpListenerRequest request)
// {
// if (!request.HasEntityBody)
// {
// return null;
// }
// using (var bodyStream = request.InputStream)
// {
// using (var memoryStream = new MemoryStream())
// {
// bodyStream.CopyTo(memoryStream);
// return memoryStream.ToArray();
// }
// }
// }
// }
//}

View File

@@ -0,0 +1,44 @@
//using System.Linq;
//using System.Net;
//using System.Text;
//#if NET45
//#else
//using System.Net.Http;
//#endif
//namespace WireMock.Http
//{
// /// <summary>
// /// The http listener response mapper.
// /// </summary>
// public class HttpListenerResponseMapperOld
// {
// private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
// /// <summary>
// /// The map.
// /// </summary>
// /// <param name="responseMessage">
// /// The response.
// /// </param>
// /// <param name="listenerResponse">The listenerResponse.</param>
// public void MapAsync(ResponseMessage responseMessage, HttpListenerResponse listenerResponse)
// {
// listenerResponse.StatusCode = responseMessage.StatusCode;
// responseMessage.Headers.ToList().ForEach(pair => listenerResponse.AddHeader(pair.Key, pair.Value));
// if (responseMessage.Body == null)
// return;
// var encoding = responseMessage.BodyEncoding ?? _utf8NoBom;
// byte[] buffer = encoding.GetBytes(responseMessage.Body);
// listenerResponse.ContentEncoding = encoding;
// listenerResponse.ContentLength64 = buffer.Length;
// listenerResponse.OutputStream.Write(buffer, 0, buffer.Length);
// listenerResponse.OutputStream.Flush();
// }
// }
//}

View File

@@ -1,113 +1,117 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Validation;
//using System;
//using System.Collections.Generic;
//#if NET45
//using System.Net;
//#else
//using System.Net.Http;
//#endif
//using System.Threading;
//using System.Threading.Tasks;
//using JetBrains.Annotations;
//using WireMock.Validation;
namespace WireMock.Http
{
/// <summary>
/// The tiny http server.
/// </summary>
public class TinyHttpServer
{
private readonly Action<HttpListenerContext, CancellationToken> _httpHandler;
//namespace WireMock.Http
//{
// /// <summary>
// /// The tiny http server.
// /// </summary>
// public class TinyHttpServerOld
// {
// private readonly Action<HttpListenerContext, CancellationToken> _httpHandler;
private readonly HttpListener _listener;
// private readonly HttpListener _listener;
private readonly CancellationTokenSource _cts;
// private readonly CancellationTokenSource _cts;
/// <summary>
/// Gets a value indicating whether this server is started.
/// </summary>
/// <value>
/// <c>true</c> if this server is started; otherwise, <c>false</c>.
/// </value>
public bool IsStarted { get; private set; }
// /// <summary>
// /// Gets a value indicating whether this server is started.
// /// </summary>
// /// <value>
// /// <c>true</c> if this server is started; otherwise, <c>false</c>.
// /// </value>
// public bool IsStarted { get; private set; }
/// <summary>
/// Gets the url.
/// </summary>
/// <value>
/// The urls.
/// </value>
[PublicAPI]
public List<Uri> Urls { get; } = new List<Uri>();
// /// <summary>
// /// Gets the url.
// /// </summary>
// /// <value>
// /// The urls.
// /// </value>
// [PublicAPI]
// public List<Uri> Urls { get; } = new List<Uri>();
/// <summary>
/// Gets the ports.
/// </summary>
/// <value>
/// The ports.
/// </value>
[PublicAPI]
public List<int> Ports { get; } = new List<int>();
// /// <summary>
// /// Gets the ports.
// /// </summary>
// /// <value>
// /// The ports.
// /// </value>
// [PublicAPI]
// public List<int> Ports { get; } = new List<int>();
/// <summary>
/// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
/// </summary>
/// <param name="uriPrefixes">The uriPrefixes.</param>
/// <param name="httpHandler">The http handler.</param>
public TinyHttpServer([NotNull] Action<HttpListenerContext, CancellationToken> httpHandler, [NotNull] params string[] uriPrefixes)
{
Check.NotNull(httpHandler, nameof(httpHandler));
Check.NotEmpty(uriPrefixes, nameof(uriPrefixes));
// /// <summary>
// /// Initializes a new instance of the <see cref="TinyHttpServer"/> class.
// /// </summary>
// /// <param name="uriPrefixes">The uriPrefixes.</param>
// /// <param name="httpHandler">The http handler.</param>
// public TinyHttpServerOld([NotNull] Action<HttpListenerContext, CancellationToken> httpHandler, [NotNull] params string[] uriPrefixes)
// {
// Check.NotNull(httpHandler, nameof(httpHandler));
// Check.NotEmpty(uriPrefixes, nameof(uriPrefixes));
_cts = new CancellationTokenSource();
// _cts = new CancellationTokenSource();
_httpHandler = httpHandler;
// _httpHandler = httpHandler;
// Create a listener.
_listener = new HttpListener();
foreach (string uriPrefix in uriPrefixes)
{
var uri = new Uri(uriPrefix);
Urls.Add(uri);
Ports.Add(uri.Port);
// // Create a listener.
// _listener = new HttpListener();
// foreach (string uriPrefix in uriPrefixes)
// {
// var uri = new Uri(uriPrefix);
// Urls.Add(uri);
// Ports.Add(uri.Port);
_listener.Prefixes.Add(uriPrefix);
}
}
// _listener.Prefixes.Add(uriPrefix);
// }
// }
/// <summary>
/// Start the server.
/// </summary>
[PublicAPI]
public void Start()
{
_listener.Start();
// /// <summary>
// /// Start the server.
// /// </summary>
// [PublicAPI]
// public void Start()
// {
// _listener.Start();
IsStarted = true;
// IsStarted = true;
Task.Run(
async () =>
{
//using (_listener)
{
while (!_cts.Token.IsCancellationRequested)
{
HttpListenerContext context = await _listener.GetContextAsync();
_httpHandler(context, _cts.Token);
}
// Task.Run(
// async () =>
// {
// //using (_listener)
// {
// while (!_cts.Token.IsCancellationRequested)
// {
// HttpListenerContext context = await _listener.GetContextAsync();
// _httpHandler(context, _cts.Token);
// }
_listener.Stop();
IsStarted = false;
}
},
_cts.Token);
}
// _listener.Stop();
// IsStarted = false;
// }
// },
// _cts.Token);
// }
/// <summary>
/// Stop the server.
/// </summary>
[PublicAPI]
public void Stop()
{
_listener?.Stop();
// /// <summary>
// /// Stop the server.
// /// </summary>
// [PublicAPI]
// public void Stop()
// {
// _listener?.Stop();
_cts.Cancel();
}
}
}
// _cts.Cancel();
// }
// }
//}