Skip to content

Commit

Permalink
Handle Exception (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Jan 19, 2017
1 parent 95b93e8 commit 68f3a2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
8 changes: 8 additions & 0 deletions examples/WireMock.Net.ConsoleApplication/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Newtonsoft.Json;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;

Expand Down Expand Up @@ -50,6 +51,13 @@ static void Main(string[] args)
.WithHeader("Content-Type", "application/json")
.WithBody(@"{ ""result"": ""data posted with 201""}"));

server
.Given(Request.WithUrl("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")))
.RespondWith(Response
.WithStatusCode(201)
.WithHeader("Content-Type", "application/json")
.WithBody(@"{ ""result"": ""json posted with 201""}"));

server
.Given(Request.WithUrl("/data").UsingDelete())
.RespondWith(Response
Expand Down
32 changes: 22 additions & 10 deletions src/WireMock/FluentMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,33 @@ private async void HandleRequest(HttpListenerContext ctx)

var request = _requestMapper.Map(ctx.Request);
LogRequest(request);
var targetRoute = _routes.FirstOrDefault(route => route.IsRequestHandled(request));
if (targetRoute == null)
try
{
ctx.Response.StatusCode = 404;
var content = Encoding.UTF8.GetBytes("<html><body>Mock Server: page not found</body></html>");
ctx.Response.OutputStream.Write(content, 0, content.Length);
var targetRoute = _routes.FirstOrDefault(route => route.IsRequestHandled(request));
if (targetRoute == null)
{
ctx.Response.StatusCode = 404;

byte[] content = Encoding.UTF8.GetBytes("<html><body>Mock Server: page not found</body></html>");
ctx.Response.OutputStream.Write(content, 0, content.Length);
}
else
{
var response = await targetRoute.ResponseTo(request);
_responseMapper.Map(response, ctx.Response);
}
}
else
catch (Exception ex)
{
var response = await targetRoute.ResponseTo(request);
ctx.Response.StatusCode = 500;

_responseMapper.Map(response, ctx.Response);
byte[] content = Encoding.UTF8.GetBytes(ex.ToString());
ctx.Response.OutputStream.Write(content, 0, content.Length);
}
finally
{
ctx.Response.Close();
}

ctx.Response.Close();
}

/// <summary>
Expand Down
12 changes: 4 additions & 8 deletions src/WireMock/HttpListenerResponseMapper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq;
using System.Net;
using System.Text;

[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]

namespace WireMock
{
/// <summary>
Expand All @@ -27,12 +21,14 @@ public class HttpListenerResponseMapper
public void Map(ResponseMessage responseMessage, HttpListenerResponse result)
{
result.StatusCode = responseMessage.StatusCode;

responseMessage.Headers.ToList().ForEach(pair => result.AddHeader(pair.Key, pair.Value));

if (responseMessage.Body != null)
{
var content = Encoding.UTF8.GetBytes(responseMessage.Body);
result.OutputStream.Write(content, 0, content.Length);
}
}
}
}
}

0 comments on commit 68f3a2f

Please sign in to comment.