• Stars
    star
    1,598
  • Rank 29,281 (Top 0.6 %)
  • Language
    C#
  • License
    MIT License
  • Created over 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Testing layer for Microsoft's HttpClient library. Create canned responses using a fluent API.

NuGetNuGet

Build status

MockHttp for HttpClient

MockHttp is a testing layer for Microsoft's HttpClient library. It allows stubbed responses to be configured for matched HTTP requests and can be used to test your application's service layer.

NuGet

PM> Install-Package RichardSzalay.MockHttp

How?

MockHttp defines a replacement HttpMessageHandler, the engine that drives HttpClient, that provides a fluent configuration API and provides a canned response. The caller (eg. your application's service layer) remains unaware of its presence.

Usage

var mockHttp = new MockHttpMessageHandler();

// Setup a respond for the user api (including a wildcard in the URL)
mockHttp.When("http://localhost/api/user/*")
        .Respond("application/json", "{'name' : 'Test McGee'}"); // Respond with JSON

// Inject the handler or client into your application code
var client = mockHttp.ToHttpClient();

var response = await client.GetAsync("http://localhost/api/user/1234");
// or without async: var response = client.GetAsync("http://localhost/api/user/1234").Result;

var json = await response.Content.ReadAsStringAsync();

// No network connection required
Console.Write(json); // {'name' : 'Test McGee'}

When (Backend Definitions) vs Expect (Request Expectations)

MockHttpMessageHandler defines both When and Expect, which can be used to define responses. They both expose the same fluent API, but each works in a slightly different way.

Using When specifies a "Backend Definition". Backend Definitions can be matched against multiple times and in any order, but they won't match if there are any outstanding Request Expectations present (unless BackendDefinitionBehavior.Always is specified). If no Request Expectations match, Fallback will be used.

Using Expect specifies a "Request Expectation". Request Expectations match only once and in the order they were added in. Only once all expectations have been satisfied will Backend Definitions be evaluated. Calling mockHttp.VerifyNoOutstandingExpectation() will assert that there are no expectations that have yet to be called. Calling ResetExpectations clears the the queue of expectations.

This pattern is heavily inspired by AngularJS's $httpBackend

Matchers (With*)

The With and Expect methods return a MockedRequest, which can have additional constraints (called matchers) placed on them before specifying a response with Respond.

Passing an HTTP method and URL to When or Expect is equivalent to applying a Method and Url matcher respectively. The following chart breaks down additional built in matchers and their usage:

Method Description
WithQueryString("key", "value")

WithQueryString("key=value&other=value")

WithQueryString(new Dictionary<string,string>
{
{ "key", "value" },
{ "other", "value" }
}
Matches on one or more querystring values, ignoring additional values
WithExactQueryString("key=value&other=value")

WithExactQueryString(new Dictionary<string,string>
{
{ "key", "value" },
{ "other", "value" }
}
Matches on one or more querystring values, rejecting additional values
WithFormData("key", "value")

WithFormData("key=value&other=value")

WithFormData(new Dictionary<string,string>
{
{ "key", "value" },
{ "other", "value" }
})
Matches on one or more form data values, ignoring additional values
WithExactFormData("key=value&other=value")

WithExactFormData(new Dictionary<string,string>
{
{ "key", "value" },
{ "other", "value" }
})
Matches on one or more form data values, rejecting additional values
WithContent("{'name':'McGee'}")
Matches on the (post) content of the request
WithPartialContent("McGee")
Matches on the partial (post) content of the request
WithHeaders("Authorization", "Basic abcdef")

WithHeaders(@"Authorization: Basic abcdef
Accept: application/json")

WithHeaders(new Dictionary<string,string>
{
{ "Authorization", "Basic abcdef" },
{ "Accept", "application/json" }
})
Matches on one or more HTTP header values
With(request => request.Content.Length > 50)
Applies custom matcher logic against an HttpRequestMessage

These methods are chainable, making complex requirements easy to descirbe.

Verifying Matches

When using Request Expectations via Expect, MockHttpMessageHandler.VerifyNoOutstandingExpectation() can be used to assert that there are no unmatched requests.

For other use cases, GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called. This even works with Fallback, so you can check how many unmatched requests there were.

var mockHttp = new MockHttpMessageHandler();

var request = mockHttp.When("http://localhost/api/user/*")
        .Respond("application/json", "{'name' : 'Test McGee'}");

var client = mockHttp.ToHttpClient();

await client.GetAsync("http://localhost/api/user/1234");
await client.GetAsync("http://localhost/api/user/2345");
await client.GetAsync("http://localhost/api/user/3456");

Console.Write(mockHttp.GetMatchCount(request)); // 3

Match Behavior

Each request is evaluated using the following process:

  1. If Request Expectations exist and the request matches the next expectation in the queue, the expectation is used to process the response and is then removed from the queue
  2. If no Request Expectations exist, or the handler was constructed with BackendDefinitionBehavior.Always, the first matching Backend Definition processes the response
  3. MockHttpMessageHandler.Fallback handles the request

Fallback

The Fallback property handles all requests that weren't handled by the match behavior. Since it is also a mocked request, any of the Respond overloads can be applied.

// Unhandled requests should throw an exception
mockHttp.Fallback.Throw(new InvalidOperationException("No matching mock handler"));

// Unhandled requests should be executed against the network
mockHttp.Fallback.Respond(new HttpClient());

The default fallback behavior is to return an empty response the status 404 No matching mock handler for "GET http://host/url".

Examples

This example uses Expect to test an OAuth ticket recycle process:

// Simulate an expired token
mockHttp.Expect("/users/me")
        .WithQueryString("access_token", "old_token")
        .Respond(HttpStatusCode.Unauthorized);
    
// Expect the request to refresh the token and supply a new one
mockHttp.Expect("/tokens/refresh")
        .WithFormData("refresh_token", "refresh_token")
        .Respond("application/json", "{'access_token' : 'new_token', 'refresh_token' : 'new_refresh'}");
    
// Expect the original call to be retried with the new token
mockHttp.Expect("/users/me")
        .WithQueryString("access_token", "new_token")
        .Respond("application/json", "{'name' : 'Test McGee'}");
    
var httpClient = mockHttp.ToHttpClient();

var userService = new UserService(httpClient);

var user = await userService.GetUserDetails();

Assert.Equals("Test McGee", user.Name);
mockHttp.VerifyNoOutstandingExpectation();

Platform Support

MockHttp is compiled for .NET Standard 2.0, .NET Standard 1.1, .NET 4, and .NET 4.5, as well a Portable Class Library (Profile 328) supporting:

  • .NET 4
  • Silverlight 5
  • Winodws 8
  • Windows Phone Silverlight 8
  • Windows Phone 8.1
  • Xamarin iOS
  • Xamarin Android

Build / Release

Clone the repository and build RichardSzalay.MockHttp.sln using MSBuild. NuGet package restore must be enabled.

To release, build:

msbuild Release.proj /p:PackageVersion=1.2.3

If you fork the project, simply rename the nuspec file accordingly and it will be picked up by the release script.

Contributors

Many thanks to all the members of the community that have contributed PRs to this project:

License

The MIT License (MIT)

Copyright (c) 2018 Richard Szalay

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

pshosts

PowerShell cmdlets for modifying the hosts file on Windows, Linux, and macOS
C#
119
star
2

raix

raix (Reactive And Interactive eXtensions) is a functional, composable, API for AS3 that simplifies working with data, regardless of whether its interactive (arrays) or reactive (events). raix was previously RxAs
ActionScript
90
star
3

helix-publishing-pipeline

Unified publishing for Sitecore Helix solutions that extends existing Visual Studio and command line workflows
PowerShell
68
star
4

fastlane-plugin-act

Modify plists and app icons inside xcarchives/IPAs. Publish multiple iOS configurations without recompiling
Ruby
36
star
5

iishosts

Provides local hosts file management from within IIS 7+, including bulk editing.
C#
12
star
6

msdeploy-package-publish

Adds MSBuild targets to Web Deploy to support publishing packages
10
star
7

buggy-bits-core

Debugging labs for .NET Core
C#
6
star
8

webdeploy-windows-service

Extension for Microsoft Web Deploy (MSDeploy) that adds support for deploying Windows Services
C#
6
star
9

sitecore-web-config

Comparisons of stock Web.config files from various Sitecore versions to assist with upgrades
PowerShell
6
star
10

spare-keys

Create temporary macOS keychains for CI environments
Ruby
5
star
11

semantic-logging-sentry

Sentry sink for Semantic Logging
C#
4
star
12

sitecore-azure-search-compat

Compatibility patch for Sitecore + Azure Search
C#
3
star
13

sitecore-commerce-configuration

Tools for configuring and boostrapping Sitecore Commerce Engine configuration
PowerShell
3
star
14

phone-common-strings

A set of string resources commonly used by Windows Phone applications, localized into all languages supported by the platform.
C#
2
star
15

rainbow-webpack-plugin

Output assets in Unicorn's Rainbow (yml) format for use with transparent sync
TypeScript
2
star
16

sitecore-icon-build

MSBuild support for building Sitecore icon archives
PowerShell
1
star
17

sitecore-assembly-lists

Tools for generating and validating Sitecore assembly lists
PowerShell
1
star
18

wp-themeresourcedictionary

Declarative support for light/dark themes on WP7
1
star