Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to load their mocked endpoint via json spec file #13

Open
phalvesr opened this issue Feb 4, 2023 · 1 comment
Open

Allow users to load their mocked endpoint via json spec file #13

phalvesr opened this issue Feb 4, 2023 · 1 comment

Comments

@phalvesr
Copy link

phalvesr commented Feb 4, 2023

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 😊

@phalvesr phalvesr changed the title Allow for users to load their mocked endpoint via json spec file Allow users to load their mocked endpoint via json spec file Feb 4, 2023
@benyblack
Copy link
Owner

Sorry for the very late response. Was super busy recently. Looks good to me but a bit difficult to find a time to implement. Pull requests are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants