http-server-sim 1.1.0.420-beta

This is a prerelease version of http-server-sim.
There is a newer version of this package available.
See the version list below for details.
dotnet tool install --global http-server-sim --version 1.1.0.420-beta                
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local http-server-sim --version 1.1.0.420-beta                
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=http-server-sim&version=1.1.0.420-beta&prerelease                
nuke :add-package http-server-sim --version 1.1.0.420-beta                

http-server-sim

Build Status

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 be executed from any folder. For other versions and more information, visit NuGet and dotnet tool install.

Rule file

The simulation of endpoints is based on a rule file. Here’s how it can be set up:

  1. Create a rule file defining the endpoints and their behaviors.
  2. Place the rule file in the appropriate directory.
  3. 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
      }
    }
  ]
}

Example command (assuming that the file rule.json is in the current folder where the command http-server-sim is being executed):

http-server-sim --Rules rules.json

The following POST request is handled using rule 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 200.

The following GET request:

curl --location 'http://localhost:5000/customers' -v

returns 404 with content Rule matching request not found

http-server-sim output:

Request:
HTTP/1.1 - GET - http://localhost:5000/customers
Headers:
  Accept: */*
  Host: localhost:5000
  User-Agent: curl/8.7.1
Body:
[Not present]
End of Request

warn: HttpServerSim.App[0]
      Rule matching request not found.

Response:
Status Code: 404
Headers:
[Not present]
Body:
Rule matching request not found
End of Response

http-server-sim CLI options

Option Description
--ControlUrl <url> URL for managing rules dynamically. Not required. Example: http://localhost:5001.
--Help Prints the help.
--LogControlRequestAndResponse Whether control requests and responses are logged. Default: false.
--LogRequestAndResponse Whether requests and responses are logged. Default: true.
--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.
--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

  1. Method Conditions: These specify the HTTP method (e.g., GET, POST) that the request must match.
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
1.2.0.471 60 8/27/2024
1.1.0.464 73 8/14/2024
1.1.0.435 68 8/7/2024
1.1.0.429 66 8/6/2024
1.1.0.421 51 8/3/2024
1.1.0.420-beta 45 8/3/2024
1.1.0.410 42 7/30/2024
1.1.0.402-beta 57 7/25/2024
1.1.0.394 61 7/23/2024
1.1.0.390-beta 53 7/22/2024