Implement "/__admin/requests"

This commit is contained in:
Stef Heyenrath
2017-01-26 12:00:30 +01:00
parent 1bf543a91e
commit a334974bef
30 changed files with 319 additions and 104 deletions

View File

@@ -315,7 +315,7 @@ The mappings defined in the mock service.
### /__admin/requests ### /__admin/requests
Logged requests and responses received by the mock service. Logged requests and responses received by the mock service.
* `GET /__admin/requests` --> TODO * `GET /__admin/requests` --> Get received requests
* `GET /__admin/requests/{requestId}` --> TODO * `GET /__admin/requests/{requestId}` --> TODO
* `POST /__admin/requests/reset` --> TODO * `POST /__admin/requests/reset` --> TODO
* `POST /__admin/requests/count` --> TODO * `POST /__admin/requests/count` --> TODO

View File

@@ -82,7 +82,7 @@ namespace WireMock.Net.ConsoleApplication
Console.ReadKey(); Console.ReadKey();
Console.WriteLine("Displaying all requests"); Console.WriteLine("Displaying all requests");
var allRequests = server.RequestLogs; var allRequests = server.LogEntries;
Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented)); Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented));
Console.WriteLine("Press any key to quit"); Console.WriteLine("Press any key to quit");

View File

@@ -1,4 +1,4 @@
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// Body Model /// Body Model

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// Cookie Model /// Cookie Model

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// Header Model /// Header Model

View File

@@ -1,6 +1,6 @@
using System; using System;
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// MappingModel /// MappingModel

View File

@@ -1,4 +1,4 @@
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// MatcherModel /// MatcherModel

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// Param Model /// Param Model

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// RequestModel /// RequestModel
@@ -16,9 +16,9 @@ namespace WireMock.Admin
public UrlModel Url { get; set; } public UrlModel Url { get; set; }
/// <summary> /// <summary>
/// The verbs /// The methods
/// </summary> /// </summary>
public string[] Verbs { get; set; } public string[] Methods { get; set; }
/// <summary> /// <summary>
/// Gets or sets the Headers. /// Gets or sets the Headers.

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// ResponseModel /// ResponseModel

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Admin namespace WireMock.Admin.Mappings
{ {
/// <summary> /// <summary>
/// UrlModel /// UrlModel

View File

@@ -0,0 +1,24 @@
namespace WireMock.Admin.Requests
{
/// <summary>
/// Request Log Model
/// </summary>
public class LogEntryModel
{
/// <summary>
/// Gets or sets the request.
/// </summary>
/// <value>
/// The request.
/// </value>
public LogRequestModel Request { get; set; }
/// <summary>
/// Gets or sets the response.
/// </summary>
/// <value>
/// The response.
/// </value>
public LogResponseModel Response { get; set; }
}
}

View File

@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using WireMock.Util;
namespace WireMock.Admin.Requests
{
/// <summary>
/// RequestMessage Model
/// </summary>
public class LogRequestModel
{
/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>
/// The unique identifier.
/// </value>
public Guid Guid { get; set; }
/// <summary>
/// Gets the DateTime.
/// </summary>
public DateTime DateTime { get; set; }
/// <summary>
/// Gets or sets the URL.
/// </summary>
/// <value>
/// The URL.
/// </value>
public string Url { get; set; }
/// <summary>
/// Gets or sets the absolete URL.
/// </summary>
/// <value>
/// The absolete URL.
/// </value>
public string AbsoleteUrl { get; set; }
/// <summary>
/// Gets the query.
/// </summary>
public IDictionary<string, WireMockList<string>> Query { get; set; }
/// <summary>
/// Gets or sets the method.
/// </summary>
/// <value>
/// The method.
/// </value>
public string Method { get; set; }
/// <summary>
/// Gets or sets the Headers.
/// </summary>
/// <value>
/// The Headers.
/// </value>
public IDictionary<string, string> Headers { get; set; }
/// <summary>
/// Gets or sets the Cookies.
/// </summary>
/// <value>
/// The Cookies.
/// </value>
public IDictionary<string, string> Cookies { get; set; }
/// <summary>
/// Gets or sets the body.
/// </summary>
/// <value>
/// The body.
/// </value>
public string Body { get; set; }
/*
"id" : "45760a03-eebb-4387-ad0d-bb89b5d3d662",
"request" : {
"url" : "/received-request/9",
"absoluteUrl" : "http://localhost:56715/received-request/9",
"method" : "GET",
"clientIp" : "127.0.0.1",
"headers" : {
"Connection" : "keep-alive",
"Host" : "localhost:56715",
"User-Agent" : "Apache-HttpClient/4.5.1 (Java/1.7.0_51)"
},
"cookies" : { },
"browserProxyRequest" : false,
"loggedDate" : 1471442494809,
"bodyAsBase64" : "",
"body" : "",
"loggedDateString" : "2016-08-17T14:01:34Z"
},
"responseDefinition" : {
"status" : 404,
"transformers" : [ ],
"fromConfiguredStub" : false,
"transformerParameters" : { }
}*/
}
}

View File

@@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace WireMock.Admin.Requests
{
/// <summary>
/// Response MessageModel
/// </summary>
public class LogResponseModel
{
/// <summary>
/// Gets or sets the status code.
/// </summary>
public int StatusCode { get; set; } = 200;
/// <summary>
/// Gets the headers.
/// </summary>
public IDictionary<string, string> Headers { get; set; }
/// <summary>
/// Gets or sets the body.
/// </summary>
public string Body { get; set; }
/// <summary>
/// Gets or sets the original body.
/// </summary>
public string BodyOriginal { get; set; }
}
}

View File

@@ -28,7 +28,9 @@ namespace WireMock
foreach (Cookie cookie in listenerRequest.Cookies) foreach (Cookie cookie in listenerRequest.Cookies)
cookies.Add(cookie.Name, cookie.Value); cookies.Add(cookie.Name, cookie.Value);
return new RequestMessage(url, verb, body, bodyAsString, headers, cookies); var message = new RequestMessage(url, verb, body, bodyAsString, headers, cookies) { DateTime = DateTime.Now };
return message;
} }
/// <summary> /// <summary>

View File

@@ -0,0 +1,24 @@
namespace WireMock.Logging
{
/// <summary>
/// LogEntry
/// </summary>
public class LogEntry
{
/// <summary>
/// Gets or sets the request message.
/// </summary>
/// <value>
/// The request message.
/// </value>
public RequestMessage RequestMessage { get; set; }
/// <summary>
/// Gets or sets the response message.
/// </summary>
/// <value>
/// The response message.
/// </value>
public ResponseMessage ResponseMessage { get; set; }
}
}

View File

@@ -7,23 +7,23 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// The request verb matcher. /// The request verb matcher.
/// </summary> /// </summary>
internal class RequestMessageVerbMatcher : IRequestMatcher internal class RequestMessageMethodMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The verbs /// The methods
/// </summary> /// </summary>
public string[] Verbs { get; } public string[] Methods { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageVerbMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageMethodMatcher"/> class.
/// </summary> /// </summary>
/// <param name="verbs"> /// <param name="methods">
/// The verb. /// The verb.
/// </param> /// </param>
public RequestMessageVerbMatcher([NotNull] params string[] verbs) public RequestMessageMethodMatcher([NotNull] params string[] methods)
{ {
Check.NotNull(verbs, nameof(verbs)); Check.NotNull(methods, nameof(methods));
Verbs = verbs.Select(v => v.ToLower()).ToArray(); Methods = methods.Select(v => v.ToLower()).ToArray();
} }
/// <summary> /// <summary>
@@ -35,7 +35,7 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
return Verbs.Contains(requestMessage.Verb); return Methods.Contains(requestMessage.Method);
} }
} }
} }

View File

@@ -3,9 +3,9 @@
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
/// <summary> /// <summary>
/// The VerbRequestBuilder interface. /// The MethodRequestBuilder interface.
/// </summary> /// </summary>
public interface IVerbRequestBuilder : IHeadersAndCookiesRequestBuilder public interface IMethodRequestBuilder : IHeadersAndCookiesRequestBuilder
{ {
/// <summary> /// <summary>
/// The using get. /// The using get.

View File

@@ -7,7 +7,7 @@ namespace WireMock.RequestBuilders
/// <summary> /// <summary>
/// IUrlAndPathRequestBuilder /// IUrlAndPathRequestBuilder
/// </summary> /// </summary>
public interface IUrlAndPathRequestBuilder : IVerbRequestBuilder public interface IUrlAndPathRequestBuilder : IMethodRequestBuilder
{ {
/// <summary> /// <summary>
/// The with url. /// The with url.

View File

@@ -127,7 +127,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersAndCookiesRequestBuilder UsingGet() public IHeadersAndCookiesRequestBuilder UsingGet()
{ {
_requestMatchers.Add(new RequestMessageVerbMatcher("get")); _requestMatchers.Add(new RequestMessageMethodMatcher("get"));
return this; return this;
} }
@@ -139,7 +139,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersAndCookiesRequestBuilder UsingPost() public IHeadersAndCookiesRequestBuilder UsingPost()
{ {
_requestMatchers.Add(new RequestMessageVerbMatcher("post")); _requestMatchers.Add(new RequestMessageMethodMatcher("post"));
return this; return this;
} }
@@ -151,7 +151,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersAndCookiesRequestBuilder UsingPut() public IHeadersAndCookiesRequestBuilder UsingPut()
{ {
_requestMatchers.Add(new RequestMessageVerbMatcher("put")); _requestMatchers.Add(new RequestMessageMethodMatcher("put"));
return this; return this;
} }
@@ -163,7 +163,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersAndCookiesRequestBuilder UsingDelete() public IHeadersAndCookiesRequestBuilder UsingDelete()
{ {
_requestMatchers.Add(new RequestMessageVerbMatcher("delete")); _requestMatchers.Add(new RequestMessageMethodMatcher("delete"));
return this; return this;
} }
@@ -175,7 +175,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersAndCookiesRequestBuilder UsingHead() public IHeadersAndCookiesRequestBuilder UsingHead()
{ {
_requestMatchers.Add(new RequestMessageVerbMatcher("head")); _requestMatchers.Add(new RequestMessageMethodMatcher("head"));
return this; return this;
} }
@@ -187,7 +187,7 @@ namespace WireMock.RequestBuilders
/// </returns> /// </returns>
public IHeadersAndCookiesRequestBuilder UsingAnyVerb() public IHeadersAndCookiesRequestBuilder UsingAnyVerb()
{ {
var matchers = _requestMatchers.Where(m => m is RequestMessageVerbMatcher).ToList(); var matchers = _requestMatchers.Where(m => m is RequestMessageMethodMatcher).ToList();
foreach (var matcher in matchers) foreach (var matcher in matchers)
{ {
_requestMatchers.Remove(matcher); _requestMatchers.Remove(matcher);
@@ -203,7 +203,7 @@ namespace WireMock.RequestBuilders
/// <returns>The <see cref="IHeadersAndCookiesRequestBuilder"/>.</returns> /// <returns>The <see cref="IHeadersAndCookiesRequestBuilder"/>.</returns>
public IHeadersAndCookiesRequestBuilder UsingVerb(params string[] verbs) public IHeadersAndCookiesRequestBuilder UsingVerb(params string[] verbs)
{ {
_requestMatchers.Add(new RequestMessageVerbMatcher(verbs)); _requestMatchers.Add(new RequestMessageMethodMatcher(verbs));
return this; return this;
} }

View File

@@ -17,15 +17,20 @@ namespace WireMock
/// </summary> /// </summary>
public string Url { get; private set; } public string Url { get; private set; }
/// <summary>
/// Gets the DateTime.
/// </summary>
public DateTime DateTime { get; set; }
/// <summary> /// <summary>
/// Gets the path. /// Gets the path.
/// </summary> /// </summary>
public string Path { get; } public string Path { get; }
/// <summary> /// <summary>
/// Gets the verb. /// Gets the method.
/// </summary> /// </summary>
public string Verb { get; } public string Method { get; }
/// <summary> /// <summary>
/// Gets the headers. /// Gets the headers.
@@ -68,7 +73,7 @@ namespace WireMock
Url = url.ToString(); Url = url.ToString();
Path = url.AbsolutePath; Path = url.AbsolutePath;
Verb = verb.ToLower(); Method = verb.ToLower();
BodyAsBytes = bodyAsBytes; BodyAsBytes = bodyAsBytes;
Body = body; Body = body;
Headers = headers; Headers = headers;

View File

@@ -185,7 +185,7 @@ namespace WireMock.ResponseBuilders
ResponseMessage responseMessage; ResponseMessage responseMessage;
if (_useTransformer) if (_useTransformer)
{ {
responseMessage = new ResponseMessage { StatusCode = ResponseMessage.StatusCode }; responseMessage = new ResponseMessage { StatusCode = ResponseMessage.StatusCode, BodyOriginal = ResponseMessage.Body };
var template = new { request = requestMessage }; var template = new { request = requestMessage };

View File

@@ -17,6 +17,11 @@ namespace WireMock
/// </summary> /// </summary>
public int StatusCode { get; set; } = 200; public int StatusCode { get; set; } = 200;
/// <summary>
/// Gets or sets the body.
/// </summary>
public string BodyOriginal { get; set; }
/// <summary> /// <summary>
/// Gets or sets the body. /// Gets or sets the body.
/// </summary> /// </summary>

View File

@@ -3,7 +3,8 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.Admin; using WireMock.Admin.Mappings;
using WireMock.Admin.Requests;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
@@ -19,6 +20,42 @@ namespace WireMock.Server
private void InitAdmin() private void InitAdmin()
{ {
Given(Request.Create().WithUrl("/__admin/mappings").UsingGet()).RespondWith(new DynamicResponseProvider(MappingsGet)); Given(Request.Create().WithUrl("/__admin/mappings").UsingGet()).RespondWith(new DynamicResponseProvider(MappingsGet));
Given(Request.Create().WithUrl("/__admin/requests").UsingGet()).RespondWith(new DynamicResponseProvider(RequestsGet));
}
private ResponseMessage RequestsGet()
{
var result = new List<LogEntryModel>();
foreach (var logEntry in LogEntries.Where(r => !r.RequestMessage.Path.StartsWith("/__admin/")))
{
var model = new LogEntryModel
{
Request = new LogRequestModel
{
Guid = Guid.NewGuid(),
DateTime = logEntry.RequestMessage.DateTime,
Url = logEntry.RequestMessage.Path,
AbsoleteUrl = logEntry.RequestMessage.Url,
Query = logEntry.RequestMessage.Query,
Method = logEntry.RequestMessage.Method,
Body = logEntry.RequestMessage.Body,
Headers = logEntry.RequestMessage.Headers,
Cookies = logEntry.RequestMessage.Cookies
},
Response = new LogResponseModel
{
StatusCode = logEntry.ResponseMessage.StatusCode,
Body = logEntry.ResponseMessage.Body,
BodyOriginal = logEntry.ResponseMessage.BodyOriginal,
Headers = logEntry.ResponseMessage.Headers
}
};
result.Add(model);
}
return ToJson(result);
} }
private ResponseMessage MappingsGet() private ResponseMessage MappingsGet()
@@ -32,7 +69,7 @@ namespace WireMock.Server
var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>(); var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>(); var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>(); var bodyMatcher = request.GetRequestMessageMatcher<RequestMessageBodyMatcher>();
var verbMatcher = request.GetRequestMessageMatcher<RequestMessageVerbMatcher>(); var methodMatcher = request.GetRequestMessageMatcher<RequestMessageMethodMatcher>();
var response = (Response) mapping.Provider; var response = (Response) mapping.Provider;
@@ -45,7 +82,7 @@ namespace WireMock.Server
{ {
Matchers = urlMatchers != null ? Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)) : null Matchers = urlMatchers != null ? Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)) : null
}, },
Verbs = verbMatcher != null ? verbMatcher.Verbs : new [] { "any" }, Methods = methodMatcher != null ? methodMatcher.Methods : new [] { "any" },
Headers = headerMatchers?.Select(hm => new HeaderModel Headers = headerMatchers?.Select(hm => new HeaderModel
{ {
Name = hm.Name, Name = hm.Name,

View File

@@ -4,10 +4,10 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Http; using WireMock.Http;
using WireMock.Logging;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.Validation; using WireMock.Validation;
@@ -18,39 +18,18 @@ namespace WireMock.Server
/// </summary> /// </summary>
public partial class FluentMockServer public partial class FluentMockServer
{ {
/// <summary>
/// The _http server.
/// </summary>
private readonly TinyHttpServer _httpServer; private readonly TinyHttpServer _httpServer;
/// <summary>
/// The _mappings.
/// </summary>
private readonly IList<Mapping> _mappings = new List<Mapping>(); private readonly IList<Mapping> _mappings = new List<Mapping>();
/// <summary> private readonly IList<LogEntry> _logEntries = new List<LogEntry>();
/// The _request logs.
/// </summary>
private readonly IList<RequestMessage> _requestLogs = new List<RequestMessage>();
/// <summary>
/// The _request mapper.
/// </summary>
private readonly HttpListenerRequestMapper _requestMapper = new HttpListenerRequestMapper(); private readonly HttpListenerRequestMapper _requestMapper = new HttpListenerRequestMapper();
/// <summary>
/// The _response mapper.
/// </summary>
private readonly HttpListenerResponseMapper _responseMapper = new HttpListenerResponseMapper(); private readonly HttpListenerResponseMapper _responseMapper = new HttpListenerResponseMapper();
/// <summary>
/// The _sync root.
/// </summary>
private readonly object _syncRoot = new object(); private readonly object _syncRoot = new object();
/// <summary>
/// The _request processing delay.
/// </summary>
private TimeSpan _requestProcessingDelay = TimeSpan.Zero; private TimeSpan _requestProcessingDelay = TimeSpan.Zero;
/// <summary> /// <summary>
@@ -61,19 +40,19 @@ namespace WireMock.Server
/// <summary> /// <summary>
/// Gets the request logs. /// Gets the request logs.
/// </summary> /// </summary>
public IEnumerable<RequestMessage> RequestLogs public IEnumerable<LogEntry> LogEntries
{ {
get get
{ {
lock (((ICollection)_requestLogs).SyncRoot) lock (((ICollection)_logEntries).SyncRoot)
{ {
return new ReadOnlyCollection<RequestMessage>(_requestLogs); return new ReadOnlyCollection<LogEntry>(_logEntries);
} }
} }
} }
/// <summary> /// <summary>
/// Gets the routes. /// Gets the mappings.
/// </summary> /// </summary>
public IEnumerable<Mapping> Mappings public IEnumerable<Mapping> Mappings
{ {
@@ -146,9 +125,9 @@ namespace WireMock.Server
/// </summary> /// </summary>
public void Reset() public void Reset()
{ {
lock (((ICollection)_requestLogs).SyncRoot) lock (((ICollection)_logEntries).SyncRoot)
{ {
_requestLogs.Clear(); _logEntries.Clear();
} }
lock (((ICollection)_mappings).SyncRoot) lock (((ICollection)_mappings).SyncRoot)
@@ -160,17 +139,13 @@ namespace WireMock.Server
/// <summary> /// <summary>
/// The search logs for. /// The search logs for.
/// </summary> /// </summary>
/// <param name="spec"> /// <param name="matcher">The matcher.</param>
/// The matcher. /// <returns>The <see cref="IEnumerable"/>.</returns>
/// </param> public IEnumerable<LogEntry> SearchLogsFor(IRequestMatcher matcher)
/// <returns>
/// The <see cref="IEnumerable"/>.
/// </returns>
public IEnumerable<RequestMessage> SearchLogsFor(IRequestMatcher spec)
{ {
lock (((ICollection)_requestLogs).SyncRoot) lock (((ICollection)_logEntries).SyncRoot)
{ {
return _requestLogs.Where(spec.IsMatch); return _logEntries.Where(log => matcher.IsMatch(log.RequestMessage));
} }
} }
@@ -215,14 +190,12 @@ namespace WireMock.Server
/// <summary> /// <summary>
/// The log request. /// The log request.
/// </summary> /// </summary>
/// <param name="requestMessage"> /// <param name="entry">The request.</param>
/// The request. private void LogRequest(LogEntry entry)
/// </param>
private void LogRequest(RequestMessage requestMessage)
{ {
lock (((ICollection)_requestLogs).SyncRoot) lock (((ICollection)_logEntries).SyncRoot)
{ {
_requestLogs.Add(requestMessage); _logEntries.Add(entry);
} }
} }
@@ -238,33 +211,44 @@ namespace WireMock.Server
} }
var request = _requestMapper.Map(ctx.Request); var request = _requestMapper.Map(ctx.Request);
LogRequest(request);
ResponseMessage response = null;
try try
{ {
var targetRoute = _mappings.FirstOrDefault(route => route.IsRequestHandled(request)); var targetRoute = _mappings.FirstOrDefault(route => route.IsRequestHandled(request));
if (targetRoute == null) if (targetRoute == null)
{ {
ctx.Response.StatusCode = 404; response = new ResponseMessage
{
byte[] content = Encoding.UTF8.GetBytes("No mapping found"); StatusCode = 404,
ctx.Response.OutputStream.Write(content, 0, content.Length); Body = "No mapping found"
};
} }
else else
{ {
var response = await targetRoute.ResponseTo(request); response = await targetRoute.ResponseTo(request);
_responseMapper.Map(response, ctx.Response);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
ctx.Response.StatusCode = 500; response = new ResponseMessage
{
byte[] content = Encoding.UTF8.GetBytes(ex.ToString()); StatusCode = 500,
ctx.Response.OutputStream.Write(content, 0, content.Length); Body = ex.ToString()
};
} }
finally finally
{ {
var log = new LogEntry
{
RequestMessage = request,
ResponseMessage = response
};
LogRequest(log);
_responseMapper.Map(response, ctx.Response);
ctx.Response.Close(); ctx.Response.Close();
} }
} }

View File

@@ -97,10 +97,10 @@ namespace WireMock.Net.Tests
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo"); await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/foo");
// then // then
Check.That(_server.RequestLogs).HasSize(1); Check.That(_server.LogEntries).HasSize(1);
var requestLogged = _server.RequestLogs.First(); var requestLogged = _server.LogEntries.First();
Check.That(requestLogged.Verb).IsEqualTo("get"); Check.That(requestLogged.RequestMessage.Method).IsEqualTo("get");
Check.That(requestLogged.BodyAsBytes).IsNull(); Check.That(requestLogged.RequestMessage.BodyAsBytes).IsNull();
} }
[Test] [Test]
@@ -118,8 +118,8 @@ namespace WireMock.Net.Tests
Check.That(result).HasSize(1); Check.That(result).HasSize(1);
var requestLogged = result.First(); var requestLogged = result.First();
Check.That(requestLogged.Path).IsEqualTo("/bar"); Check.That(requestLogged.RequestMessage.Path).IsEqualTo("/bar");
Check.That(requestLogged.Url).IsEqualTo("http://localhost:" + _server.Port + "/bar"); Check.That(requestLogged.RequestMessage.Url).IsEqualTo("http://localhost:" + _server.Port + "/bar");
} }
[Test] [Test]
@@ -133,7 +133,7 @@ namespace WireMock.Net.Tests
_server.Reset(); _server.Reset();
// then // then
Check.That(_server.RequestLogs).IsEmpty(); Check.That(_server.LogEntries).IsEmpty();
} }
[Test] [Test]

View File

@@ -46,7 +46,7 @@ namespace WireMock.Net.Tests
// then // then
Check.That(MapperServer.LastRequestMessage).IsNotNull(); Check.That(MapperServer.LastRequestMessage).IsNotNull();
Check.That(MapperServer.LastRequestMessage.Verb).IsEqualTo("put"); Check.That(MapperServer.LastRequestMessage.Method).IsEqualTo("put");
} }
[Test] [Test]

View File

@@ -20,7 +20,7 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
var response = Response.Create() var response = Response.Create()
.WithBody("test {{request.url}} {{request.path}} {{request.verb}}") .WithBody("test {{request.url}} {{request.path}} {{request.method}}")
.WithTransformer(); .WithTransformer();
// act // act