0.3.19 • Published 5 years ago

varkes-openapi-mock v0.3.19

Weekly downloads
242
License
Apache-2.0
Repository
github
Last release
5 years ago

OpenAPI Mock

npm version

Mocks res calls given an OpenAPI specification

After startup, you have access to:

Starting the application

Run local

To run it local, specify a config file (as located in the /test folder) like that:

npm install
node server/server <varkes_config.js>

Run local using docker

To run it using docker, call:

docker run -p 10000:10000 eu.gcr.io/kyma-project/incubator/develop/varkes-openapi-mock:latest

Run in Kyma

To run the mock using Kyma as runtime envrironment, run the following kubectl command to set up a namespace:

kubectl create namespace mocks
kubectl label namespace mocks env=true

and to deploy the mock

kubectl apply -n mocks -f https://raw.githubusercontent.com/kyma-incubator/varkes/master/openapi-mock/deployment/deployment.yaml

Features

  • Supports OpenAPI 3.0 specs in JSON or YAML OpenAPI Mock uses Swagger-Express-Middleware to parse, validate, and dereference OpenAPI files. You can also create your custom implementation for a response and for errors.

  • Records Every Request made to the node Creates a requests.log file that contains the urls being called, the header of the request and the body of the request if exists using the morgan logging framework.

  • Returns the OpenAPI specification as metadata By calling '/metadata' user can see the OpenAPI specification being use in text/x-yaml format

  • Returns a dummy OAuth2 token By calling the endpoint '/authorizationserver/oauth/token' and adding the OAuth2 requirements as query params user can get a dummy OAuth2 token

Installation and Use

Install using NPM.

npm install

Then you need to copy your OpenAPI yaml into the api/swagger directory as swagger.yml OR you could simply change the path in the config.js file specified by the "specification" element

You need to remove the host in order for swagger-express-middleware to do it's magic You don't need to write a custom response in your javascript code for every endpoint in the file, for the endpoints that your are satisfied with a default response you add the default key with the response object to your response of the endpoint in the file as shown

responses:
        200:
          description: "OK"
          schema:
            course:
              code:
                type: "string"
              name:
                type: "string"
            default:
              course:
                code: "C1"
                name: "course1"

Node js code

The entry point for the application is the app.js file which reads the swagger file swagger.yml and creates an instance of the mock_controller where the user

  • Return custom Error messages as response to certain error codes or messages in the customErrorResponses function the following example checks if the error status is not known or if it's one of the status that are defined in the config.js file and in response sends the corresponding error message
 app = app_modified;
app.use(function (err, req, res, next) {
    if (!err.status) {
        err.status = 500;
    }
    try {
        res.status(err.status);
        res.type('json');
        res.send(util.format(config.error_messages[err.status]));
    }
    catch (err) {
        console.error(err)
    }
});

Config.js

In this file you define the paths of all the important files like the swagger.yml file and the requests.log file.You can also add any kind of global element needed for the application. You also define all the custom error messages corresponding to their status code as following

module.exports = {
    specification: 'swagger.yaml',
    request_log_path: 'requests.log',
    added_endpoints: [
        {
            filePath: "OAuth_template.yaml",
            url: '/authorizationserver/oauth/token'
        }
    ],
    customResponsePath: '../../custom_responses',
    error_messages: {
        500: '{"error":\"Something went Wrong\"}',
        400: '{"error":\"Errorrrr\"}',
        404: '{"error":\"End Point not found\"}'
    },
    port: 10000
}
0.3.19

5 years ago

0.3.18

5 years ago

0.3.17

5 years ago

0.3.16

5 years ago

0.3.15

5 years ago

0.3.14

5 years ago

0.3.13

5 years ago

0.3.12

5 years ago

0.3.11

5 years ago

0.3.10

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.1

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.26

5 years ago

0.1.25

5 years ago

0.1.24

5 years ago

0.1.23

5 years ago

0.1.22

5 years ago

0.1.21

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago