Skip to content

Instantly share code, notes, and snippets.

@wuriyanto48
Last active November 14, 2024 06:05
Show Gist options
  • Save wuriyanto48/dc6a602c16c35dae538821452d83f782 to your computer and use it in GitHub Desktop.
Save wuriyanto48/dc6a602c16c35dae538821452d83f782 to your computer and use it in GitHub Desktop.
Create Mock Server using Prism

Install Prism

npm install -g @stoplight/prism-cli

Create Mockup Json File using OpenApi spec

{
    "openapi": "3.0.0",
    "info": {
      "title": "Sample Mock API",
      "version": "1.0.0"
    },
    "paths": {
      "/users": {
        "get": {
          "summary": "Retrieve a list of users",
          "responses": {
            "200": {
              "description": "A JSON array of user names",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "success": {
                        "type": "boolean"
                      },
                      "message": {
                        "type": "string"
                      },
                      "data": {
                          "type": "array"
                      }
                    }
                  },
                  "example": {
                    "success": true,
                    "message": "get user success",
                    "data": [
                        {
                            "id": 1,
                            "name": "alex",
                            "addresses": [
                                {
                                    "street": "jln. jambu",
                                    "postalCode": "0001"
                                },
                                {
                                    "street": "jln. apel",
                                    "postalCode": "0002"
                                }
                            ]
                        },
                        {
                            "id": 2,
                            "name": "john",
                            "addresses": [
                                {
                                    "street": "jln. kijang",
                                    "postalCode": "0011"
                                },
                                {
                                    "street": "jln. tiger",
                                    "postalCode": "0012"
                                }
                            ]
                        }
                    ]
                  }
                }
              }
            }
          }
        }
      },
      "/users/{userId}": {
        "get": {
          "summary": "Retrieve a specific user",
          "parameters": [
            {
              "name": "userId",
              "in": "path",
              "required": true,
              "schema": {
                "type": "integer",
                "example": 1
              }
            }
          ],
          "responses": {
            "200": {
              "description": "A user object",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "success": {
                        "type": "boolean"
                      },
                      "message": {
                        "type": "string"
                      },
                      "data": {
                          "type": "object"
                      }
                    }
                  },
                  "example": {
                    "success": true,
                    "message": "get user by id success",
                    "data": {
                        "id": 1,
                        "name": "alex"
                    }
                  }
                }
              }
            },
            "404": {
              "description": "User not found",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "success": {
                        "type": "boolean",
                        "example": false
                      },
                      "message": {
                        "type": "string",
                        "example": "user not found"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "/articles": {
        "get": {
          "summary": "Retrieve a list of articles",
          "parameters": [
            {
                "name": "createdAt",
                "in": "query",
                "required": false,
                "schema": {
                    "type": "string",
                    "example": "2024-09-24"
                }
            }
          ],
          "responses": {
            "200": {
              "description": "A JSON array of articles",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "success": {
                        "type": "boolean"
                      },
                      "message": {
                        "type": "string"
                      },
                      "data": {
                          "type": "array"
                      }
                    }
                  },
                  "example": {
                    "success": true,
                    "message": "ger articles success",
                    "data": [
                        {
                            "id": 1,
                            "title": "Artificial Intelligence",
                            "content": "Artificial Intelligence xxxxxx xxxxxx xxxxx",
                            "author": "Robert D.",
                            "createdAt": "2024-08-29"
                        }
                      ]
                  }
                }
              }
            },
            "404": {
                "description": "Articles empty",
                "content": {
                  "application/json": {
                    "schema": {
                      "type": "object",
                      "properties": {
                        "success": {
                          "type": "boolean",
                          "example": true
                        },
                        "message": {
                          "type": "string",
                          "example": "articles not found"
                        },
                        "data": {
                            "type": "array",
                            "example": []
                        }
                      }
                    }
                  }
                }
            }
          }
        }
      }
    }
  }  

Run Prism using Json File above

prism mock user-openapi.json

Run Prism with Custom Port Number

prism mock user-openapi.json -p 5001

Testing

Simulate User List

http://127.0.0.1:4010/users

Simulate Find User By Id

http://127.0.0.1:4010/users/1

Simulate Find User By Id with return 404 Not Found

http://127.0.0.1:4010/users/1?__code=404
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment