0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ひょんなことからWireMockを触ったので残しておく

Posted at

はじめに

実はこれ WireMock なんだー ということがあり、少し触ったので、忘れないうちに残しておきます。
触った箇所をまとめておくだけなので、大した内容ではないです。

WireMockとは

HTTPサービスをmock(模擬)するためのライブラリ・ツールです。
主にテストで使用されます。

使いどき

使いどきの例です。
アプリケーションが外部APIを利用しているとします。
しかし、テスト環境ではその外部APIを利用することができず、テストができません。
外部APIを呼び出した際に、あらかじめ設定した固定レスポンスを返却できれば、テストが通るのになー。
WireMockを設定して解決!

動作の流れ

  1. アプリケーションが外部APIを呼び出す
  2. WireMockが代わりに応答する
  3. 事前に設定した固定レスポンスを返す
  4. アプリは本物のAPIから返事が来たと思ってテストが進む

使い方

今回は docker compose を使用する前提とします。

環境準備

  • ローカル環境にWireMockで設定したいリクエストとレスポンスのディレクトリ+ファイルを配置する
    (作成したディレクトリは、後でコンテナにボリュームマウントする)
  • user-api.jsonで記載したurlへ動作確認時に接続する
// ディレクトリ作成と移動
$ mkdir -p wiremock/mappings wiremock/__files && cd wiremock

// mappingsディレクトリに設定ファイルを配置
$ cd mappings
$ vi user-api.json
  • user-api.json は「このリクエストで接続があれば、このレスポンスを返す」を定義しています
user-api.json
{
  "request": {
    "method": "GET",
    "url": "/api/users/123"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "bodyFileName": "user-123.json"
  }
}
// __filesディレクトリにレスポンス内容のファイルを配置
$ cd ../__files
$ vi user-123.json
  • レスポンスのボディを定義しています
  • ファイル名はuser-api.jsonのbodyFileNameと一致させる
user-123.json
{
  "id": 123,
  "name": "Test",
  "email": "test@example.com"
}
  • docker-compose.ymlを作成
  • volumesの設定は必須ではないが、今回はコンテナを再起動した際に消えないようにボリュームマウントしている
$ cd
$ vi docker-compose.yml
docker-compose.yml
version: '3.9'

services:
  wiremock:
    # wiremockのコンテナイメージ指定
    image: wiremock/wiremock:3.13.1
    ports:
      # ポート8080を公開
      - "8080:8080"
    # ローカルのディレクトリをボリュームマウント
    volumes:
      - ./wiremock/mappings:/home/wiremock/mappings
      - ./wiremock/__files:/home/wiremock/__files
  • docker-compose を起動
// -d はデタッチドモードのオプション
$ docker-compose up -d

動作確認

  • 動作確認前に設定を確認する
  • どうせならWireMockの管理APIを利用して確認する
// mappingsの設定確認
$ curl http://localhost:8080/__admin/mappings

{
  "mappings" : [ {
    "id" : "xxxxxxxxxx",
    "request" : {
      "url" : "/api/users/123",
      "method" : "GET"
    },
    "response" : {
      "status" : 200,
      "bodyFileName" : "user-123.json",
      "headers" : {
        "Content-Type" : "application/json"
      }
    },
    "uuid" : "yyyyyyyyyy"
  } ],
  "meta" : {
    "total" : 1
  }
}


// filesの一覧確認
$ curl http://localhost:8080/__admin/files

[ "user-123.json" ]


// 各filesの内容確認
$ curl http://localhost:8080/__admin/files/user-123.json

{
  "id": 123,
  "name": "Test",
  "email": "test@example.com"
}

  • いざ動作確認
  • curlでmockのレスポンス確認
  • 8080で公開するようにコンテナを設定しているため、ポートを指定する
// mappingsに記載したurlでcurl
$ curl http://localhost:8080/api/users/123

{
  "id": 123,
  "name": "Test",
  "email": "test@example.com"
}

補足情報

  • mockするAPIを増やす場合
    • 「mappings」ディレクトリと「__files」ディレクトリにファイルを追加する(ローカル側で増やした場合はコンテナの再起動が必要)
    • 管理APIを使用する
  • リクエスト履歴の確認
    $ curl http://localhost:8080/__admin/requests

おわりに

とりあえず自分で動かしてみてなんとなーくわかりました。
既に構築されている状況であり、私がメインで触るところでもないため、
次にいつ触れ合うかわかりませんが、知っていると知らないでは違いますよね。(と言い聞かせる)
その他便利な機能もあるようですが、それはまた使いどきがきたらにします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?