Compare commits

...

2 Commits

Author SHA1 Message Date
Stef Heyenrath
9e44f7eb0e 1.0.4.15 2018-09-04 22:53:23 +02:00
Stef Heyenrath
41fd1ef99d #198 (#199) 2018-09-04 22:50:17 +02:00
9 changed files with 91 additions and 22 deletions

View File

@@ -1,3 +1,11 @@
# 1.0.4.15 (04 September 2018)
- [#199](https://github.com/WireMock-Net/WireMock.Net/pull/199) - Fix for .WithBody(Func<RequestMessage, string>...) contributed by Stef Heyenrath ([StefH](https://github.com/StefH))
- [#198](https://github.com/WireMock-Net/WireMock.Net/issues/198) - Issue : creating response using .WithBody(Func<RequestMessage, string>...) and .WithStatusCode +fix
Commits: 41fd1ef99d...41fd1ef99d
# 1.0.4.14 (02 September 2018)
- [#197](https://github.com/WireMock-Net/WireMock.Net/pull/197) - Set IsStarted = true in a IApplicationLifetime.ApplicationStarted listener contributed by ([davide-romanini](https://github.com/davide-romanini)) +fix

View File

@@ -1,5 +1,5 @@
https://github.com/GitTools/GitReleaseNotes
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.4.14
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.4.15
GitReleaseNotes.exe . /OutputFile CHANGELOG.md /allTags

View File

@@ -358,6 +358,20 @@ namespace WireMock.Net.ConsoleApplication
.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();

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<Description>Lightweight StandAlone Http Mocking Server for .Net.</Description>
<AssemblyTitle>WireMock.Net.StandAlone</AssemblyTitle>
<Version>1.0.4.14</Version>
<Version>1.0.4.15</Version>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net451;net452;net46;netstandard1.3;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

View File

@@ -169,7 +169,14 @@ namespace WireMock.ResponseBuilders
/// <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)"/>
@@ -353,6 +360,22 @@ namespace WireMock.ResponseBuilders
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);
@@ -367,11 +390,7 @@ namespace WireMock.ResponseBuilders
return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage);
}
if (Callback != null)
{
return Callback(requestMessage);
}
// Just return normal defined ResponseMessage
return ResponseMessage;
}
}

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<Description>Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape.</Description>
<AssemblyTitle>WireMock.Net</AssemblyTitle>
<Version>1.0.4.14</Version>
<Version>1.0.4.15</Version>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net451;net452;net46;net461;netstandard1.3;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

View File

@@ -48,15 +48,15 @@ namespace WireMock.Net.Tests
[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();
}

View File

@@ -106,7 +106,7 @@ namespace WireMock.Net.Tests
[Fact]
public async Task FluentMockServer_Should_respond_to_request_bodyAsCallback()
{
// given
// Assign
var _server = FluentMockServer.Start();
_server
@@ -114,14 +114,18 @@ namespace WireMock.Net.Tests
.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]

View File

@@ -1,5 +1,4 @@
using System;
using System.Text;
using System.Text;
using System.Threading.Tasks;
using NFluent;
using WireMock.Models;
@@ -150,5 +149,30 @@ namespace WireMock.Net.Tests.ResponseBuilderTests
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");
}
}
}