Giter Site home page Giter Site logo

ganesanarun / declarative-openapi-mock-server Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 38 KB

This project provides a declarative OpenAPI mock server with conditional responses, supporting rules such as "and" and "or" conditions, case sensitivity, and comparison operators.

Dockerfile 2.32% JavaScript 97.68%

declarative-openapi-mock-server's Introduction

Declarative OpenAPI Mock Server

This project provides a declarative OpenAPI mock server with conditional responses, supporting rules such as "and" and " or" conditions, case sensitivity, and comparison operators.

Features

  • Declarative configuration using OpenAPI specifications
  • Conditional responses based on request payload, query and headers, path parameters
  • Supports "and" / "or" conditions
  • Case sensitivity and comparison operators (e.g., eq, lt, lte, gt, gte)
  • Default random success response according to OpenAPI spec when no custom logic is specified

Note: Since yaml is a superset of json, you could put json body directly.

Prerequisites

  • Node.js (v20 or higher)
  • npm (v10 or higher)

Installation

  1. Clone the repository:

    git clone https://github.com/ganesanarun/declarative-openapi-mock-node-server.git
    cd declarative-openapi-mock-server
  2. Install the dependencies:

    npm install

Usage

  1. Place your OpenAPI specification files in the specs directory. Ensure each spec file has the custom logic defined under x-custom-logic.

  2. Run the mock server:

    npm start

    The mock server will start on http://localhost:3000.

Docker

Build and Run the Docker Image

  1. Build the Docker Image: Navigate to the directory containing your Dockerfile and run:

    docker build -t openapi-mock-server .
  2. Run the Docker Container: Run the container, mapping the exposed ports:

    docker run -p 3000:3000 -p 4010:4010 openapi-mock-server

OpenAPI Specification Example

openapi: 3.0.0
info:
  title: Simple API
  version: 1.0.0
paths:
  /users:
    post:
      summary: Create a user
      operationId: createUser
      tags:
        - User
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
      responses:
        '200':
          description: User created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/User'
      x-custom-logic:
        type: conditionalResponse
        conditions:
          - type: or
            rules:
              - location: body
                field: name
                value: special
                caseSensitive: false
                operator: eq
              - location: body
                field: metadata.role
                value: admin
                operator: eq
            response:
              statusCode: 200
              body:
                id: 1000
                name: Admin User
              headers:
                X-Custom-Header: custom_value
          - type: and
            rules:
              - location: body
                field: name
                value: John
                caseSensitive: true
                operator: eq
              - location: body
                field: metadata.role
                value: manager
                operator: eq
            response:
              statusCode: 201
              body: {
                "id": 1002,
                "name": "Age 30 or below User",
                "metadata": {
                  "age": 25
                }
              }
              headers:
                X-Custom-Header: manager_value
          - type: and
            rules:
              - location: body
                field: age
                value: 30
                operator: lte
            response:
              statusCode: 200
              body:
                id: 1002
                name: Age 30 or below User
components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          example: 1
        name:
          type: string
          example: "John Doe"
        metadata:
          type: object
          properties:
            role:
              type: string
              example: "user"
        age:
          type: integer
          example: 25

Custom Logic Configuration

The x-custom-logic attribute allows you to define custom conditional responses. Each condition can contain multiple rules that are either and or or conditions. Each rule specifies:

  • location: The part of the request to evaluate (body, header, query, 'path').
  • field: The field to match.
  • value: The value to compare against.
  • caseSensitive: Whether the comparison is case-sensitive.
  • operator: The comparison operator (eq, lt, lte, gt, gte).

The response section of each condition specifies the statusCode, body, and headers to return if the condition is met.

Example

For a POST request to /users with the body:

{
  "name": "special",
  "metadata": {
    "role": "user"
  },
  "age": 25
}

The server will respond with:

{
  "id": 1000,
  "name": "Admin User"
}

with headers:

{
  "X-Custom-Header": "custom_value"
}

if the request matches the first condition in x-custom-logic.

If the request matches no custom logic conditions, a random success response, according to the OpenAPI specification, will be returned.

declarative-openapi-mock-server's People

Contributors

architect-at-work avatar

Watchers

Ganesan Arunachalam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.