http-server-sim
1.1.0.435
See the version list below for details.
dotnet tool install --global http-server-sim --version 1.1.0.435
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local http-server-sim --version 1.1.0.435
#tool dotnet:?package=http-server-sim&version=1.1.0.435
nuke :add-package http-server-sim --version 1.1.0.435
http-server-sim
HTTP Server Simulator is a .NET tool that runs a Web API simulating HTTP endpoints, supporting the development and testing of components that use HTTP.
http-server-sim can be called from the shell/command line.
Usage
Installation
Install the latest version from NuGet:
dotnet tool install --global http-server-sim
Once http-server-sim
is installed as a global tool, it can run from any folder.
For other versions and more information, visit NuGet and dotnet tool install.
Running http-server-sim
Running http-server-sim
using default options:
# Start the simulator
http-server-sim
# Send a GET request to the simulator from another terminal
curl --location 'http://localhost:5000/data' -v
Simulator output
Request:
HTTP/1.1 - GET - http://localhost:5000/data
Headers:
Accept: */*
Host: localhost:5000
User-Agent: curl/8.7.1
Body:
[Not present]
End of Request
Response:
Status Code: 200
Headers:
[Not present]
Body:
[Not present]
End of Response
http-server-sim
attempts to match a request to a rule. When a rule is found, it responds with the response defined in that rule. If no matching rule is found, it responds with a configurable default
response, which has a Status Code
200 and no content.
Running http-server-sim
setting the default content:
# Start the simulator setting the default content with a json
http-server-sim --DefaultContentType application/json --DefaultContentValue "{""name"":""Juan""}"
# Send a GET request to the simulator from another terminal
curl --location 'http://localhost:5000/data' -v
Simulator output
...
Response:
Status Code: 200
Headers:
Content-Type: application/json
Body:
{"name":"Juan"}
End of Response
Another example setting the default response indicating that a resource not was found.
http-server-sim --DefaultContentType text/plain --DefaultContentValue "Resource not found" --DefaultStatusCode 404
Rule file
The simulation of endpoints is based on a rule file. Here’s how it can be set up:
- Create a rule file defining conditions and a response message.
- Place the rule file in the appropriate directory.
- Run the simulator with the rule file.
Example of rule file.
{
"rules": [
{
"name": "customers-post",
"description": "",
"conditions": [
{ "field": "Method", "operator": "Equals", "value": "POST" },
{ "field": "Path", "operator": "Contains", "value": "/customers" }
],
"response": {
"statusCode": 200
}
}
]
}
# Loading rules from a file in the current directory
http-server-sim --Rules rules.json
# Send a POST request (will be handled by the rule called customers-post):
curl --location 'http://localhost:5000/customers' --header 'Content-Type: application/json' --data '{"id":10,"name":"Juan"}' -v
http-server-sim
returns a response with Status Code
200.
Saving request and response messages to files
# Save request and response to the folder messages-history under the current directory
http-server-sim --SaveRequests messages-history --SaveResponses messages-history
- The directory can be a full path, e.g.,
C:\temp\http-server-sim-messages
, or a relative directory under the current directory, e.g.,messages-history
. - Messages are saved using a
GUID
as the name, with the.req
extension for request messages and the.res
extension for response messages. - Keep in mind that files are not deleted automatically. If you have a long-running process creating files, the hard drive may eventually run out of space.
http-server-sim CLI options
Option | Description |
---|---|
--ControlUrl <url> |
URL for managing rules dynamically. Not required. Example: http://localhost:5001 . |
--DefaultContentType <value> |
The Content-Type used in a response message when no rule matching the request is found. |
--DefaultContentValue <value> |
The Content used in a response message when no rule matching the request is found. |
--DefaultStatusCode <value> |
The HTTP status code used in a response message when no rule matching the request is found. Default: 200. |
--Help | Prints the help. |
--LogControlRequestAndResponse | Whether control requests and responses are logged. Default: false . |
--LogRequestAndResponse | Whether requests and responses are logged. Default: true . |
--RequestBodyLogLimit <limit> |
Maximum request body size to log (in bytes). Default: 4096. |
--ResponseBodyLogLimit <limit> |
Maximum response body size to log (in bytes). Default: 4096. |
--Rules <file-name> \| <path> |
Rules file. It can be a file name of a file that exists in the current directory or a full path to a file. |
--SaveRequests <directory> |
The directory where request messages are saved. |
--SaveResponses <directory> |
The directory where response messages are saved. |
--Url <url> |
URL for simulating endpoints. Default: http://localhost:5000 . |
--Url and --ControlUrl cannot share the same value. |
|
Rule conditions
When http-server-sim
processes a request, it uses rule conditions to match a rule to the request.
Example of conditions:
"conditions": [
{ "field": "Method", "operator": "Equals", "value": "POST" },
{ "field": "Path", "operator": "Contains", "value": "/customers" }
]
A rule with these conditions is applied when:
- The request method is
POST
, and - The URL path contains
/customers
.
There are two types of conditions, Method
and Path
- Method Conditions: These specify the HTTP method (e.g.,
GET
,POST
) that the request must match. - URL Path Conditions: These specify the URL path that the request must match.
The supported operators are: Equals
, StartWith
, and Contains
Response messages
When http-server-sim
identifies a rule that matches a request, it prepares a response message based on the response
section of the rule.
Response properties:
Property | Description |
---|---|
statusCode | Status code of the response message. Default: 200 |
headers | A collection of headers. Example: "headers": [ { "key": "Server", "value": ["http-server-sim"] } ] |
contentType | Content type of a response with a content, this value is used to create the header Content-Type . Example: application/json |
contentValue | Value used in the content, can be a text, a full path to a file, or a file name when the file exists in the current directory. Example: person-1.json |
contentValueType | Defines whether contentValue is a text or a file. Possible values: Text or File . Default: Text |
encoding | Defines an encoding to apply to the content. Default: None . Supported values: GZip |
Example of a response defining a message with Status Code
400.
"response": {
"statusCode": 400
}
Example of a response defining a message with Status Code
200 and headers Server
(with a single value) and Header-1
(with multiple values)
"response": {
"statusCode": 200,
"headers": [
{ "key": "Server", "value": ["http-server-sim"] },
{ "key": "Header-1", "value": ["val-1", "val-2"] }
]
}
Example of a response with a plain text content.
"response": {
"contentType": "text/plain",
"contentValue": "Thank you for the notification",
}
Example of a response using a json file that exists in the current directory.
"response": {
"contentType": "application/json",
"contentValue": "person-1.json",
"contentValueType": "File"
}
Example of a response using an in-line json.
"response": {
"contentType": "application/json",
"contentValue": "{\"name\":\"Juan\"}"
}
Example of a response using a json file that exists in the current directory and compressing the content with gzip
.
"response": {
"contentType": "application/json",
"contentValue": "person-1.json",
"contentValueType": "File",
"encoding": "GZip"
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
This package has no dependencies.
Version | Downloads | Last updated |
---|---|---|
1.3.0.523-beta | 192 | 10/16/2024 |
1.3.0.513-beta | 79 | 10/15/2024 |
1.3.0.489-beta | 80 | 9/12/2024 |
1.2.0.471 | 124 | 8/27/2024 |
1.1.0.464 | 133 | 8/14/2024 |
1.1.0.435 | 110 | 8/7/2024 |
1.1.0.429 | 105 | 8/6/2024 |
1.1.0.421 | 93 | 8/3/2024 |
1.1.0.420-beta | 75 | 8/3/2024 |
1.1.0.410 | 81 | 7/30/2024 |
1.1.0.402-beta | 91 | 7/25/2024 |
1.1.0.394 | 95 | 7/23/2024 |
1.1.0.390-beta | 80 | 7/22/2024 |