はなちるのマイノート

Unityをメインとした技術ブログ。自分らしくまったりやっていきたいと思いますー!

Prismを用いてOpenAPIから簡単にモックサーバーを作る

はじめに

今回はPrismについて紹介したいと思います。

stoplight.io

Prism is an open-source HTTP mock server that can mimic your API's behavior as if you already built it. Mock HTTP servers are generated from your OpenAPI v2/v3 (formerly known as Swagger) documents.

Prismは、オープンソースのHTTPモックサーバーで、あたかもAPIを構築したかのような動作を模倣することができます。モックサーバーは、OpenAPI v2/v3 (旧Swagger) ドキュメントから生成されます。

Prism | Open-Source HTTP Mock and Proxy Server | Stoplight

やり方

まずはPrismをインストールします。

# グローバルインストールをする
$ npm install -g @stoplight/prism-cli
# or
$ yarn global add @stoplight/prism-cli


あとはOpenAPIからMockサーバーを立ち上げるのですが、今回は以下のネットに転がっているOpenAPIを活用します。

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
  termsOfService: http://swagger.io/terms/
  contact:
    name: Swagger API Team
    email: apiteam@swagger.io
    url: http://swagger.io
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  - url: https://petstore.swagger.io/v2
paths:
  /pets:
    get:
      description: |
        Returns all pets from the system that the user has access to
        Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.

        Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.
      operationId: findPets
// 以下省略...

https://raw.githack.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml



以下のコマンドを打てば、サーバーが立ち上がります。

$ prism mock https://raw.githack.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml

出力結果は以下の通り。

sh-3.2$ prism mock https://raw.githack.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml
[9:11:48] › [CLI] …  awaiting  Starting Prism…
[9:11:50] › [CLI] ℹ  info      GET        http://127.0.0.1:4010/pets
[9:11:50] › [CLI] ℹ  info      POST       http://127.0.0.1:4010/pets
[9:11:50] › [CLI] ℹ  info      GET        http://127.0.0.1:4010/pets/-5268286185913520000
[9:11:50] › [CLI] ℹ  info      DELETE     http://127.0.0.1:4010/pets/4364750905575039000
[9:11:50] › [CLI] ▶  start     Prism is listening on http://127.0.0.1:4010
[9:12:01] › [HTTP SERVER] get /pets ℹ  info      Request received
[9:12:01] ›     [NEGOTIATOR] ℹ  info      Request contains an accept header: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
[9:12:01] ›     [VALIDATOR] ✔  success   The request passed the validation rules. Looking for the best response
[9:12:01] ›     [NEGOTIATOR] ✔  success   Found a compatible content for text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
[9:12:01] ›     [NEGOTIATOR] ✔  success   Responding with the requested status code 200
[9:12:01] ›     [NEGOTIATOR] ℹ  info      > Responding with "200"
[9:12:01] › [HTTP SERVER] get /favicon.ico ℹ  info      Request received
[9:12:01] › [HTTP SERVER] get /favicon.ico ✖  error     Request terminated with error: https://stoplight.io/prism/errors#NO_PATH_MATCHED_ERROR: Route not resolved, no path matched