1
0

More than 1 year has passed since last update.

モックサーバ(instant-mock)で送信できるContent-Typeメモ

Last updated at Posted at 2021-11-30

モックサーバ(instant-mock)で送信できるContent-Typeメモ

前回下記の記事でinstant-mockの構築手順を(ほぼ自分用に)まとめた。

今回はこのモックサーバーはresponse bodyにどんなデータの形式を入れ込んで送信できるのかを検証してみた。
下記に実際のコードを格納したので、ご興味のある方はどうぞ。

検証結果

bodyに入れ込むことができたデータの形式は以下のものであった。

  • JSONファイル
  • PDFファイル
  • EXCELファイル( .xlsx OFFICE 2007以降)
  • CSVファイル
  • HTMLファイル
  • CSSファイル
  • テキストファイル
  • JPEGファイル(.jpg)
  • PNGファイル
  • Bitmapファイル

bodyにデータを格納する際にContent-Typeの参考にさせていただいたのは下記サイトである。

ymlファイル

APIを定義したymlファイルか下記通り。
mockサーバを初期化した際にできたymlファイルに書き加えていった。

parser-default.yml
- if:
    query:
      group: team-a
  then:
    status: 200
    headers:
      Content-Type: application/json
    body: team-a.json

- if:
    query:
      group: team-b
  then:
    status: 200
    headers:
      Content-Type: application/json
    body: team-b.json

- if:
    query:
      group: team-pdf
  then:
    status: 200
    headers:
      Content-Type: application/pdf
    body: PDFファイルだお.pdf

- if:
    query:
      group: team-sheet
  then:
    status: 200
    headers:
      Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    body: test.xlsx
    
- if:
    query:
      group: team-csv
  then:
    status: 200
    headers:
      Content-Type: text/csv
    body: hell.csv

- if:
    query:
      group: team-html
  then:
    status: 200
    headers:
      Content-Type: text/html
    body: test.html

- if:
    query:
      group: team-css
  then:
    status: 200
    headers:
      Content-Type: text/css
    body: test.css

- if:
    query:
      group: team-text
  then:
    status: 200
    headers:
      Content-Type: text/plain
    body: hello.txt

- if:
    query:
      group: team-jpeg
  then:
    status: 200
    headers:
      Content-Type: image/jpeg
    body: テスト.jpg

- if:
    query:
      group: team-png
  then:
    status: 200
    headers:
      Content-Type: image/png
    body: テスト.png

- if:
    query:
      group: team-bmp
  then:
    status: 200
    headers:
      Content-Type: image/bmp
    body: テスト.bmp

- then:
    status: 200
    headers:
      Content-Type: application/json
    body: all.json

実行結果

ARC(Advanced REST client)を使って定義したAPIのレスポンスを確認しようとしたところ、
レスポンス結果がバイナリーコードのまま表示され、上手く送れてるのかの判別がしにくかった。

ARCのスクショ
2021-11-30_19h19_51.png

そこで今回はコンソールでgetコマンドを使用して動作検証を行った。

JSONファイル

※team-aとteam-bはmockサーバを初期化した際にできたymlファイルに元々記載してあったもの。
http://localhost:3000/mock/bodyTest?group=team-a

team-aスクショ
2021-11-30_19h27_14.png

http://localhost:3000/mock/bodyTest?group=team-b

team-bスクショ
2021-11-30_19h27_26.png

PDFファイル

http://localhost:3000/mock/bodyTest?group=team-pdf

team-pdfスクショ
2021-11-30_19h27_49.png

EXCELファイル( .xlsx OFFICE 2007以降)

ブラウザの画面の表示はymlファイルの一番最後に記述してあるthenのjsonファイルの中身が表示された。
http://localhost:3000/mock/bodyTest?group=team-sheet

team-sheetスクショ
2021-11-30_19h26_01.png

CSVファイル

http://localhost:3000/mock/bodyTest?group=team-csv

team-csvスクショ
2021-11-30_19h26_50.png

HTMLファイル

http://localhost:3000/mock/bodyTest?group=team-html

team-htmlスクショ
2021-11-30_19h28_08.png

CSSファイル

http://localhost:3000/mock/bodyTest?group=team-css

team-cssスクショ
2021-11-30_19h28_21.png

テキストファイル

http://localhost:3000/mock/bodyTest?group=team-text

team-textスクショ
2021-11-30_19h28_34.png

JPEGファイル(.jpg)

http://localhost:3000/mock/bodyTest?group=team-jpeg

team-jpegスクショ
2021-11-30_19h28_41.png

PNGファイル

http://localhost:3000/mock/bodyTest?group=team-png

team-pngスクショ
2021-11-30_19h28_49.png

Bitmapファイル

http://localhost:3000/mock/bodyTest?group=team-bmp

team-bmpスクショ
2021-11-30_19h29_15.png

以上が試した結果になる。
もし気が向いたら他のContent-Typeも試して追記するカモ。。。

1
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
1
0