Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Sep 4, 2018
1 parent 666e1ab commit 41fd1ef
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
14 changes: 14 additions & 0 deletions examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,20 @@ public static void Run()
.WithBody("linq match !!!")
);

server
.Given(Request.Create().WithPath("/myendpoint").UsingAnyMethod())
.RespondWith(Response.Create()
.WithStatusCode(500)
.WithBody(requestMessage =>
{
string returnStr = JsonConvert.SerializeObject(new
{
Message = "Test error"
});
return returnStr;
})
);

System.Console.WriteLine("Press any key to stop the server");
System.Console.ReadKey();
server.Stop();
Expand Down
31 changes: 25 additions & 6 deletions src/WireMock.Net/ResponseBuilders/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ public IResponseBuilder WithHeaders(IDictionary<string, WireMockList<string>> he
/// <inheritdoc cref="IBodyResponseBuilder.WithBody(Func{RequestMessage, string}, string, Encoding)"/>
public IResponseBuilder WithBody(Func<RequestMessage, string> bodyFactory, string destination = BodyDestinationFormat.SameAsSource, Encoding encoding = null)
{
return WithCallback(req => new ResponseMessage { Body = bodyFactory(req) });
Check.NotNull(bodyFactory, nameof(bodyFactory));

return WithCallback(req => new ResponseMessage
{
Body = bodyFactory(req),
BodyDestination = destination,
BodyEncoding = encoding ?? Encoding.UTF8
});
}

/// <inheritdoc cref="IBodyResponseBuilder.WithBody(byte[], string, Encoding)"/>
Expand Down Expand Up @@ -353,6 +360,22 @@ public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMe
await Task.Delay(Delay.Value);
}

if (Callback != null)
{
var callbackResponseMessage = Callback(requestMessage);

// Copy StatusCode from ResponseMessage
callbackResponseMessage.StatusCode = ResponseMessage.StatusCode;

// Copy Headers from ResponseMessage (if defined)
if (ResponseMessage.Headers != null)
{
callbackResponseMessage.Headers = ResponseMessage.Headers;
}

return callbackResponseMessage;
}

if (ProxyUrl != null && _httpClientForProxy != null)
{
var requestUri = new Uri(requestMessage.Url);
Expand All @@ -367,11 +390,7 @@ public async Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMe
return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage);
}

if (Callback != null)
{
return Callback(requestMessage);
}

// Just return normal defined ResponseMessage
return ResponseMessage;
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/WireMock.Net.Tests/FluentMockServerTests.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public void FluentMockServer_Admin_ResetMappings()
[Fact]
public void FluentMockServer_Admin_StartStop()
{
var server1 = FluentMockServer.Start("http://localhost:9091");
var server1 = FluentMockServer.Start("http://localhost:19091");

Check.That(server1.Urls[0]).Equals("http://localhost:9091");
Check.That(server1.Urls[0]).Equals("http://localhost:19091");

server1.Stop();

var server2 = FluentMockServer.Start("http://localhost:9091/");
var server2 = FluentMockServer.Start("http://localhost:19091/");

Check.That(server2.Urls[0]).Equals("http://localhost:9091/");
Check.That(server2.Urls[0]).Equals("http://localhost:19091/");

server2.Stop();
}
Expand Down
18 changes: 11 additions & 7 deletions test/WireMock.Net.Tests/FluentMockServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,26 @@ public async Task FluentMockServer_Should_respond_to_request_BodyAsJson_Indented
[Fact]
public async Task FluentMockServer_Should_respond_to_request_bodyAsCallback()
{
// given
// Assign
var _server = FluentMockServer.Start();

_server
.Given(Request.Create()
.WithPath("/foo")
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithBody(req => $"{{ path: '{req.Path}' }}"));
.WithStatusCode(500)
.WithHeader("H1", "X1")
.WithBody(req => $"path: {req.Path}"));

// when
var response = await new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo");
// Act
var response = await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");

// then
Check.That(response).IsEqualTo("{ path: '/foo' }");
// Assert
string content = await response.Content.ReadAsStringAsync();
Check.That(content).IsEqualTo("path: /foo");
Check.That((int) response.StatusCode).IsEqualTo(500);
Check.That(response.Headers.GetValues("H1")).ContainsExactly("X1");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Text;
using System.Text;
using System.Threading.Tasks;
using NFluent;
using WireMock.Models;
Expand Down Expand Up @@ -150,5 +149,30 @@ public async Task Response_ProvideResponse_WithBody_String_Json_Encoding()
Check.That(((dynamic)responseMessage.BodyAsJson).value).Equals(42);
Check.That(responseMessage.BodyEncoding).Equals(Encoding.ASCII);
}

[Fact]
public async Task Response_ProvideResponse_WithBody_Func()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost/test"), "GET", ClientIp);

var response = Response.Create()
.WithStatusCode(500)
.WithHeader("H1", "X1")
.WithHeader("H2", "X2")
.WithBody(req => $"path: {req.Path}");

// Act
var responseMessage = await response.ProvideResponseAsync(request);

// Assert
Check.That(responseMessage.Body).IsEqualTo("path: /test");
Check.That(responseMessage.BodyAsBytes).IsNull();
Check.That(responseMessage.BodyAsJson).IsNull();
Check.That(responseMessage.BodyEncoding.CodePage).Equals(Encoding.UTF8.CodePage);
Check.That(responseMessage.StatusCode).IsEqualTo(500);
Check.That(responseMessage.Headers["H1"].ToString()).IsEqualTo("X1");
Check.That(responseMessage.Headers["H2"].ToString()).IsEqualTo("X2");
}
}
}

0 comments on commit 41fd1ef

Please sign in to comment.