benyblack / rest-mock-core Goto Github PK
View Code? Open in Web Editor NEWA simple HTTP server for using as a mock in test projects which test dotnet based projects.
License: MIT License
A simple HTTP server for using as a mock in test projects which test dotnet based projects.
License: MIT License
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):
public sealed class JsonSpec
{
public IEnumerable<JsonSpecItem> Routes { get; init; } = default!;
}
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!;
}
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 ๐
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
The project needs more unit test and also better naming for methods in the test project.
Can you say something for this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.