Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled exception when target is unavailable #469

Merged
merged 4 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion azure-pipelines-ci-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ variables:

steps:
- script: |
dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration $(buildConfiguration) --framework netcoreapp2.1 --logger trx
dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration $(buildConfiguration) --framework netcoreapp3.1 --logger trx
- task: PublishTestResults@2
inputs:
testRunner: VSTest
Expand Down
6 changes: 3 additions & 3 deletions azure-pipelines-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ steps:
displayName: Begin SonarScanner
condition: and(succeeded(), eq(variables['RUN_SONAR'], 'yes'))

# Build source, tests and run tests for net452 and netcoreapp2.1 (with coverage)
# Build source, tests and run tests for net452 and netcoreapp3.1 (with coverage)
- script: |
dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration Debug --framework net452
dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration Debug --framework netcoreapp2.1 --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
displayName: 'Build source, tests and run tests for net452 and netcoreapp2.1 (with coverage)'
dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration Debug --framework netcoreapp3.1 --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
displayName: 'Build source, tests and run tests for net452 and netcoreapp3.1 (with coverage)'

# End SonarScanner
- script: |
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Owin/WireMockMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private async Task InvokeInternal(IContext ctx)
catch (Exception ex)
{
_options.Logger.Error($"Providing a Response for Mapping '{result?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
response = ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500);
response = ResponseMessageBuilder.Create(ex.Message, 500);
}
finally
{
Expand Down
3 changes: 1 addition & 2 deletions src/WireMock.Net/Serialization/LogEntryMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
using WireMock.Admin.Mappings;
using WireMock.Admin.Requests;
using WireMock.Logging;
using WireMock.Types;
using WireMock.ResponseBuilders;
using WireMock.Util;
using WireMock.Types;

namespace WireMock.Serialization
{
Expand Down
2 changes: 1 addition & 1 deletion test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Authors>Stef Heyenrath</Authors>
<!--<TargetFrameworks>net452;netcoreapp2.1</TargetFrameworks>-->
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DebugType>full</DebugType>
<AssemblyName>WireMock.Net.Tests</AssemblyName>
<PackageId>WireMock.Net.Tests</PackageId>
Expand Down
57 changes: 46 additions & 11 deletions test/WireMock.Net.Tests/WireMockServer.Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using NFluent;
using WireMock.Admin.Mappings;
using WireMock.Logging;
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
Expand Down Expand Up @@ -50,7 +53,7 @@ public async Task WireMockServer_Proxy_Should_log_proxied_requests()
public async Task WireMockServer_Proxy_Should_proxy_responses()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath(path))
Expand All @@ -76,7 +79,7 @@ public async Task WireMockServer_Proxy_Should_proxy_responses()
public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -123,7 +126,7 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied
public async Task WireMockServer_Proxy_Should_exclude_blacklisted_content_header_in_mapping()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -165,7 +168,7 @@ public async Task WireMockServer_Proxy_Should_exclude_blacklisted_content_header
public async Task WireMockServer_Proxy_Should_exclude_blacklisted_cookies_in_mapping()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -214,7 +217,7 @@ public async Task WireMockServer_Proxy_Should_exclude_blacklisted_cookies_in_map
public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -246,7 +249,7 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied
public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_response()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -277,7 +280,7 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied
public async Task WireMockServer_Proxy_Should_change_absolute_location_header_in_proxied_response()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var settings = new WireMockServerSettings { AllowPartialMapping = false };

var serverForProxyForwarding = WireMockServer.Start(settings);
Expand Down Expand Up @@ -310,7 +313,7 @@ public async Task WireMockServer_Proxy_Should_change_absolute_location_header_in
public async Task WireMockServer_Proxy_Should_preserve_cookie_header_in_proxied_request()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -379,7 +382,7 @@ public async Task WireMockServer_Proxy_Should_preserve_binary_request_content()
public async Task WireMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -410,7 +413,7 @@ public async Task WireMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response
public async Task WireMockServer_Proxy_Should_set_Body_in_multipart_proxied_response()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -440,7 +443,7 @@ public async Task WireMockServer_Proxy_Should_set_Body_in_multipart_proxied_resp
public async Task WireMockServer_Proxy_Should_Not_overrule_AdminMappings()
{
// Assign
string path = $"/prx_{Guid.NewGuid().ToString()}";
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
Expand Down Expand Up @@ -482,5 +485,37 @@ public async Task WireMockServer_Proxy_Should_Not_overrule_AdminMappings()
string content2 = await response2.Content.ReadAsStringAsync();
Check.That(content2).IsEqualTo("[]");
}

[Fact]
public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_CorrectResponse()
{
// Assign
var settings = new WireMockServerSettings
{
ProxyAndRecordSettings = new ProxyAndRecordSettings
{
Url = $"http://error{Guid.NewGuid()}:12345"
}
};
var server = WireMockServer.Start(settings);

// Act
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(server.Urls[0])
};
var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false };
var result = await new HttpClient(httpClientHandler).SendAsync(requestMessage);

// Assert
result.StatusCode.Should().Be(500);

var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("known"); // On Linux it's "Name or service not known". On Windows it's "No such host is known.".

server.LogEntries.Should().HaveCount(1);
((StatusModel) server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status.Should().Contain("known");
}
}
}