GithubHelp home page GithubHelp logo

benyblack / rest-mock-core Goto Github PK

View Code? Open in Web Editor NEW
37.0 37.0 9.0 104 KB

A simple HTTP server for using as a mock in test projects which test dotnet based projects.

License: MIT License

C# 100.00%
csharp dotnet dotnet-core http http-server httpserver mock rest unit-testing

rest-mock-core's Introduction

Hi there ๐Ÿ‘‹

rest-mock-core's People

Contributors

benyblack avatar dutts avatar jtone123 avatar mojtabakiani avatar rgparkins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rest-mock-core's Issues

Allow users to load their mocked endpoint via json spec file

I've just discovered this library recently and it's quite nice, but there is a feature I was wondering if we could get add. The feature would be allow users to load their endpoints using a json file, similarly to what json server does. Thinking about user experience, maybe follow the below example:

var httpServer = new HttpServer();
httpServer.Config.FromJsonFile("path/to/json_spec.json");

Considering a json spec file in the below format:

{
  "Routes": [
      {
        "Method": "GET",
        "Url": "/hello-world",
        "Body": "Hello from route.json!",
        "StatusCode": 200,
        "RequestHeaders": {
          "Authorization": "Bearer ey..."
        },
        "ResponseHeaders": {
          "CorrelationId": "00000000-0000-0000-0000-00000000"
        }
      },
      {
        "Method": "POST",
        "Url": "/hello-world",
        "Body": "{\"Data\": { \"Fullname\": \"JOHN DOE\", \"Age\": 42 }}",
        "StatusCode": 200
      }
    ]
}

We can get the proposed experience with the following changes (using .Net 6 sintax):

  • Create a model for the JsonSpec file:
public sealed class JsonSpec
{
    public IEnumerable<JsonSpecItem> Routes { get; init; } = default!;
}
  • Create a model for the route definition
public sealed class JsonSpecItem
{
    public string Method { get; init; } = default!;
    public string Url { get; init; } = default!;
    public string Body { get; init; } = default!;
    public int StatusCode { get; init; } = default!;
    public Dictionary<string, string> RequestHeaders { get; init; } = default!;
    public Dictionary<string, string> ResponseHeaders { get; init; } = default!;
} 
  • Implement an extension method to handle the route registration
public static void FromJsonFile(this IRequestBuilder builder, string jsonSpecPath)
{
        var specAsJson = File.ReadAllText(jsonSpecPath);

        var spec = JsonSerializer.Deserialize<JsonSpec>(specAsJson);

        foreach (var route in spec.Routes)
        {
            builder.Request(route.Method, route.Url, route.RequestHeaders)
                .Send(route.Body, route.StatusCode, route.ResponseHeaders);
        }
}

Calling the endpoint, for example, the "hello-world" from the json example with the following curl command:

curl -v -H "Authorization: Bearer ey..." http://localhost:5000/hello-world

would produce the following output:

*   Trying 127.0.0.1:5000...
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET /hello-world HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.83.1
> Accept: */*
> Authorization: Bearer ey...
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: Kestrel
< Transfer-Encoding: chunked
< CorrelationId: 00000000-0000-0000-0000-00000000
<
Hello from test route.json!

I would be happy to implement the pointed changes (with maybe some changes) in the project ๐Ÿ˜Š

mockServer is throwing an exception on Run

When I call โ€œRunโ€ on the mockServer it throws an exception: Could not load type 'Microsoft.AspNetCore.Server.Kestrel.KestrelServerOptions' from assembly 'Microsoft.AspNetCore.Server.Kestrel, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

I am using .netcore v2 under Visual Studio 2017

Create more unit tests

The project needs more unit test and also better naming for methods in the test project.

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.