0.1.3 • Published 6 years ago

yasmock v0.1.3

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

What is this?

YASMock(Yet Another Swagger Mocker) is a simple cli tool for generating a http server from a swagger contract. It works by extracting additional metadata from your swagger file and uses that to respond to requests. You can additionally pass custom generators to the cli that, in effect, controls how the mock server generates data.

How do i get it?

npm install yasmock -g

How do i use it?

Given the following v3 swagger snippet with responses definition

"responses": {
  "200": {
    "description": "User Details",
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties":{
            "name": {
              "type":"string"
              "faker": "random.findName"
            },
            "age": {
              "type": "number"
              "faker": "random"
            }
          }
          "required":["name", "age"]
        }
      }
    }
  }
}

Executing the following

$ yasmock serve --port=3000 --file=/path/to/swagger/file.json

will give you back

{
  "name":"Daniel",
  "age": "21"
}

CLI Options

  • --port- Port to bind mock server on (Omitting this cause the cli to pick a random free port to use)
  • --file - File containing swagger specification. Supports both YAML and JSON files.
  • --url - Url to download the swagger file, Supports both YAML and JSON responses.
  • --include-optional - Will include optional properties defined in the swagger file as part of the response. Defaults to false
  • --generator - Specifies a generator custom script.
  • --prefer-example - Uses the examples object as response
  • --prefer-faker - Uses fake data as response

How can I containerize my mock server?

Here is an example of what you dockerfile could look like.

The dockerfile

FROM node:8.9.4-slim

ENV APP_ROOT = /usr/app
ARG MOCK_SERVER_PORT
ARG SWAGGER_URL

WORKDIR ${APP_ROOT}

COPY ./petstore.json ${APP_ROOT}/petstore.json

RUN npm install yasmock --verbose

EXPOSE ${MOCK_SERVER_PORT}

CMD ["node_modules/.bin/yasmock", "--file", "./petstore.json"]

Build the container

docker build . -t petstore-mock

Run container

docker run petstore-mock