Coder Social home page Coder Social logo

mockhttpclient's Introduction

MockHttpClient

Easily mock HttpClient for testing

//Arrange
var mockHttpClient = new MockHttpClient();
mockHttpClient.When("http://www.google.com").Returns(HttpStatusCode.OK)

//Act
var response = await client.GetAsync(requestUri);

//Assert
Assert.Equal(HttpStatusCode.OK, response);

NuGet version

url matching

You can specify partial and wildcarded urls in the when condition to match across many differnt urls. Below are a few example of how to specify url queries

mockHttpClient.When([url string]).Returns(HttpStatusCode.OK)
url string matches
http://mockhttpclient.local/path/to/resource matches full Url
http://mockhttpclient.local/path/to/* matches paths starting with /path/to
http://mockhttpclient.local/path/*/resource) matches paths starting with /path and ending with /resource
http://*.local/path/to/resource) matches paths with domains ending in .local and paths starting with /path/to/resource
http//mockhttpclient.local/* matches everything at the domain mockhttpclient.local
//mockhttpclient.local/path/to/resource matches https and http
/path/to/resource matches all paths at /path/to/resource regardless of domain
/path/to/resource?type=test matches all paths at /path/to/resource and have query string value type=test
?type=test matches all urls that have query string value type=test
?type=test&q=* matches all urls that have query string value type=test and have value q that can be anything

slightly more complicated matching

If you want to do something more than the simple examples above, the when method can take a Func<HttpRequestMessage, bool>, while the then method can take a Func<HttpRequestMessage, HttpResponseMessage>. This allows you to create more complicated conditions and responses.

The library also provides some helper extension methods to do common checks on HttpRequestMessage and to set some common properties on HttpResponseMessages

client
    .When(req => req.HasHeader("accept", "application/json"))
    .Then(req => new HttpResponseMessage()
            .WithHeader("version", "1")
            .WithJsonContent(new
            {
                FirstName = "John",
                LastName = "Doe"
            }));

mockhttpclient's People

Contributors

alex-davies avatar cternes avatar kassemsandarusi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.