From 10be63c90062098ba88bf3800b346a6112041778 Mon Sep 17 00:00:00 2001 From: Alastair Crabtree Date: Wed, 16 May 2018 20:33:06 +0100 Subject: [PATCH] Added docs for MatchBehaviour.RejectOnMatch --- Stubbing-and-Request-Matching.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Stubbing-and-Request-Matching.md b/Stubbing-and-Request-Matching.md index b02882c..63ed752 100644 --- a/Stubbing-and-Request-Matching.md +++ b/Stubbing-and-Request-Matching.md @@ -158,6 +158,24 @@ server Url like /somebody and /someting ``` +### Reversing the match behaviour with `MatchBehaviour.RejectOnMatch` + +The default behaviour for Matchers is MatchBehaviour.AcceptOnMatch so that when the matcher processes a request that corresponds with the matcher, the stubbed response is returned. In some scenarios you might want to reverse this behaviour so that the stubbed response is returned with the absence of a match. + +e.g. You want to return `401 Unauthorised` if the caller does not provide a header containing the API Key: + +```csharp +server + .Given(Request.Create() + .WithPath("/needs-a-key") + .UsingGet() + .WithHeader("api-key", "*", MatchBehaviour.RejectOnMatch) + .UsingAnyMethod()) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.Unauthorized) + .WithBody(@"{ ""result"": ""api-key missing""}")); +``` + ## Response Templating Response headers and bodies can optionally be rendered using [Handlebars.Net](https://github.com/rexm/Handlebars.Net) templates. This enables attributes of the request to be used in generating the response e.g. to pass the value of a request ID header as a response header or render an identifier from part of the URL in the response body. To use this functionality, add `.WithTransformer()` to the response builder.