Skip to content

Commit

Permalink
Improve the default fallback handler
Browse files Browse the repository at this point in the history
Deprecated FallbackResponse property also removed (breaking change)
  • Loading branch information
Jericho authored and richardszalay committed Apr 16, 2018
1 parent c9f323d commit 8dd12d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 45 deletions.
29 changes: 5 additions & 24 deletions RichardSzalay.MockHttp.Shared/MockHttpMessageHandler.cs
Expand Up @@ -29,7 +29,7 @@ public MockHttpMessageHandler(BackendDefinitionBehavior backendDefinitionBehavio

AutoFlush = true;
fallback = new MockedRequest();
fallback.Respond(req => (fallbackResponse = CreateDefaultFallbackMessage()));
fallback.Respond(req => CreateDefaultFallbackMessage(req));
}

private bool autoFlush;
Expand Down Expand Up @@ -180,7 +180,6 @@ private void IncrementMatchCount(IMockedRequest handler)
}
}

private HttpResponseMessage fallbackResponse = null;
private MockedRequest fallback;

/// <summary>
Expand All @@ -194,30 +193,12 @@ public MockedRequest Fallback
}
}

/// <summary>
/// Gets or sets the response that will be returned for requests that were not matched
/// </summary>
[Obsolete("Please use Fallback. FallbackResponse will be removed in a future release")]
public HttpResponseMessage FallbackResponse
HttpResponseMessage CreateDefaultFallbackMessage(HttpRequestMessage req)
{
get
{
return fallbackResponse;
}
set
var message = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound)
{
if (value == null)
throw new ArgumentNullException();

fallbackResponse = value;
fallback.Respond(value);
}
}

HttpResponseMessage CreateDefaultFallbackMessage()
{
HttpResponseMessage message = new HttpResponseMessage(System.Net.HttpStatusCode.NotFound);
message.ReasonPhrase = "No matching mock handler";
ReasonPhrase = $"No matching mock handler for \"{req.Method.ToString().ToUpperInvariant()} {req.RequestUri.AbsoluteUri}\""
};
return message;
}

Expand Down
24 changes: 3 additions & 21 deletions RichardSzalay.MockHttp.Tests/MockHttpMessageHandlerTests.cs
Expand Up @@ -157,26 +157,6 @@ public void Should_return_fallback_for_unmatched_requests()
Assert.Equal("Awesome", result.ReasonPhrase);
}

[Fact]
public void Should_return_fallbackresponse_for_unmatched_requests()
{
var mockHandler = new MockHttpMessageHandler();
var client = new HttpClient(mockHandler);

mockHandler
.When("/test")
.Respond(System.Net.HttpStatusCode.OK, "application/json", "{'status' : 'OK'}");

mockHandler.FallbackResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
ReasonPhrase = "Awesome"
};

var result = client.GetAsync("http://invalid/test2").Result;

Assert.Equal("Awesome", result.ReasonPhrase);
}

[Fact]
public void Should_match_expect_before_when()
{
Expand Down Expand Up @@ -233,9 +213,11 @@ public void Should_not_match_expect_out_of_order()
.Expect("/test2")
.Respond("application/json", "{'status' : 'Second'}");

mockHandler.Fallback.Respond(HttpStatusCode.NotFound);

var result = client.GetAsync("http://invalid/test2").Result;

Assert.Equal(mockHandler.FallbackResponse.StatusCode, result.StatusCode);
Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
}

[Fact]
Expand Down

0 comments on commit 8dd12d2

Please sign in to comment.