openapi-mocker
Create a mock server from an OpenAPI 3.0 specification.
Installation
- Install with cargo:
Usage
-
Create an OpenAPI 3.0 specification file. For example,
openapi.yaml
:openapi: 3.0.0 info: title: Example API version: 1.0.0 paths: /hello: get: responses: '200': description: OK content: application/json: schema: type: object properties: message: type: string example: message: Hello, world! '400': description: Bad Request content: application/json: schema: type: object properties: message: type: string example: message: Bad request
Note: The
example
field under thecontent.schema
object is used to generate the mock response. -
Run the mock server:
- Or, Run with docker:
-
Perform an http request to the mock server:
The response should be:
json {"message":"Hello, world!"}
Requesting non existent route
The response should be 404 Not Found.
Options
--port
or-p
: Port to run the server on. Default is8080
.
Performing requests
The mock server will respond to any request with a response defined in the OpenAPI specification. If the request does not match any of the paths defined in the specification, the server will respond with a 404 Not Found.
By default, requesting an existing path defined in the specification will return a 200 response with the example response defined in the specification.
Request different status codes
To request a different status code, use the base url with the status code as a path parameter.
For example, to request a 400 response example:
The response should be:
Note: The status code must be defined in the OpenAPI specification. Requesting a status code that is not defined in the specification will return a 404 Not Found.
License
MIT