Coder Social home page Coder Social logo

simplehttpmock's Introduction

SimpleHttpMock Build status

A really simple http mock using self host service.

Match by simple url

var builder = new MockedHttpServerBuilder();
builder
    .WhenGet("/Test")
    .Respond(HttpStatusCode.OK);
using (builder.Build("http://localhost:1122"))
using (var httpClient = new HttpClient())
{
    var response = await httpClient.GetAsync("http://localhost:1122/test");
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

Return some response

var builder = new MockedHttpServerBuilder();
builder
    .WhenGet("/Test")
    .Respond(HttpStatusCode.OK, new People { Name = "John Doe"});
using (builder.Build("http://localhost:1122"))
using (var httpClient = new HttpClient())
{
    var response = await httpClient.GetAsync("http://localhost:1122/test");
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    var people = await response.ReadAsAsync<People>()
    Assert.Equal("John Doe", people.Name);
}

Create server first, build behavior afterwards

using (var server = new MockedHttpServer("http://localhost:1122"))
{
    var builder = new MockedHttpServerBuilder();
    builder.WhenGet("/test").Respond(HttpStatusCode.OK);
    builder.Build(server);
    using (var httpClient = new HttpClient())
    {
        var response = httpClient.GetAsync("http://localhost:1122/test");
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
    }
}

Retrieve request being sent

var builder = new MockedHttpServerBuilder();
var requestRetriever = builder.WhenGet("/test").Respond(HttpStatusCode.OK).Retrieve();
using (builder.Build("http://localhost:1122"))
{
    using (var httpClient = new HttpClient())
    {
        // no request being sent yet
        Assert.Null(requestRetriever());

        // when send the request
        var response = await httpClient.GetAsync("http://localhost:1122/test1");

        // should get the request by retriever
        var actualRequest = requestRetriever();
        Assert.NotNull(actualRequest);
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);                    
        Assert.Equal("GET", actualRequest.Method);
        Assert.Equal("http://localhost:1122/test", actualRequest.RequestUri.ToString());
    }
}

Hamcrest Style Matchers

  • Matchers.Regex
 var serverBuilder = new MockedHttpServerBuilder();
 serverBuilder
     .WhenGet(Matchers.Regex(@"/(staff)|(user)s"))
     .Respond(HttpStatusCode.InternalServerError);

 using (serverBuilder.Build(BaseAddress))
 {
     var response = Get("http://localhost/users"));
     Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
 }
  • Matchers.WildCard
serverBuilder
    .WhenGet(Matchers.Wildcard(@"/staffs/?"))
    .Respond(HttpStatusCode.Unauthorized);
  • Matchers.Is
serverBuilder
     .WhenGet(Matchers.Is("/staffs"))
     .Respond(HttpStatusCode.OK);

Other Matchers

serverBuilder
    .When(Matchers.Wildcard(@"/staffs/?"), HttpMethod.Patch)
    .Respond(HttpStatusCode.Unauthorized);

simplehttpmock's People

Contributors

cuiyansong avatar forki avatar lxconan avatar tw-service avatar xiaoyvr avatar yingrui avatar zhengquan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

simplehttpmock's Issues

Http headers support

Hi,

Just wonder do plan to add support for HTTP headers? In my case I need them to test scenarios with authorization.

The 1.1.36 package has no dependencies specified and fails during usage

Hello,

The newest package does not include dependencies:
https://www.nuget.org/packages/SimpleHttpMock/1.1.36

An attempt to use it, makes tests to fail:

OneTimeSetUp: System.MissingMemberException : The server factory could not be located for the given input: Microsoft.Owin.Host.HttpListener
  Exception doesn't have a stacktrace

Workaround:
Fallback to previous version that has dependencies specified: https://www.nuget.org/packages/SimpleHttpMock/1.1.5

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.