[Front-End General] JSON Server で立てた REST API をテストするツールと faker でのダミーデータ流し込み

curl よりも便利に使えるツール( Chrome Extensions, JetBrains Products)がある。
さらに faker でダミーデータを流し込む。

[markdown]
> * [JSON Server で fake REST API を立てる | deadwood](https://www.d-wood.com/blog/2016/12/16_8691.html)

## REST API TEST Tool

### Chrome 拡張

DHC を使ったことがあるが、他のツールも紹介されている。
Postman が多機能なよう。

> * [REST API テストツール – unhurried](http://unhurried.hatenablog.com/entry/rest_api_test_tool)

[Postman – REST Client – Chrome ウェブストア](https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm)

> * [【Tools】Chrome拡張「Postman」で簡単REST API確認! | unitopi – ユニトピ -](http://unitopi.com/tools-postman/)

[DHC – Restlet Client – Chrome ウェブストア](https://chrome.google.com/webstore/detail/dhc-restlet-client/aejoelaoggembcahagimdiliamlcdmfm)

> * [ChromeでREST APIをテストするならコレ!「DHC」 | mah365](http://blog.mah-lab.com/2014/08/26/chrome-rest-api-testing/)

### JetBrains

[JetBrains](https://www.jetbrains.com/img/home-page/header-bg.png) 製品には、Test RESTful Web Service というツールがあった。
これがいいかな感。

> * [IntelliJ IDEA 2016.3 Help :: Testing RESTful Web Services](https://www.jetbrains.com/help/idea/2016.3/testing-restful-web-services.html)

こちらで使い方を知った。

> * [Creating Demo APIs with json-server – node Video Tutorial #free @eggheadio](https://egghead.io/lessons/nodejs-creating-demo-apis-with-json-server)

## faker でダミーデータを流し込む

さらに json-server に faker でダミーデータを流し込む手順も説明されているので試してみる。

### インストール

“`
% npm install –save-dev faker lodash
“`

### generate.js の作成と実行

faker のメソッドを使って、データを生成するスクリプトを書く。

> * [Marak/faker.js: generate massive amounts of fake data in Node.js and the browser](https://github.com/Marak/faker.js)

“`javascript:generate.js
module.exports = function() {
var faker = require(‘faker’);
var _ = require(‘lodash’);
faker.locale = ‘ja’;
return {
posts: _.times(10, function(n) {
return {
id: n,
title: faker.lorem.sentence(),
author: faker.name.findName(),
}
})
}
}
“`

generate.js を指定してサーバを起動。
generate.js が db.json の代わりになっていて、スクリプト内で作った `posts` が使えるようになっている。
なので db.json の内容が書き換わるわけではない。

“`
json-server –watch generate.js
\{^_^}/ hi!
Loading generate.js
Done
Resources
http://localhost:3000/posts
Home
http://localhost:3000
Type s + enter at any time to create a snapshot of the database
Watching…
“`

データは下記のようになっているが、`”結菜 清水 III”` や `”Mr. 美咲 鈴木”` 等があるので、もう少し工夫が必要そう。

“`json
{
“posts”: [
{
“id”: 0,
“title”: “Eos id minus maxime rerum.”,
“author”: “樹 中村”
},
{
“id”: 1,
“title”: “Placeat hic voluptatem excepturi quam cumque non.”,
“author”: “結菜 清水 III”
},
{
“id”: 2,
“title”: “Repellendus eos labore sit adipisci porro debitis cupiditate at nisi.”,
“author”: “大和 山口”
},
{
“id”: 3,
“title”: “Error at dolores libero aut quis fuga qui ut voluptatem.”,
“author”: “杏 山口 IV”
},
{
“id”: 4,
“title”: “Vitae ea error facilis non aut rem.”,
“author”: “大和 山田”
},
{
“id”: 5,
“title”: “Quisquam est ut.”,
“author”: “Mr. 美咲 鈴木”
},
“`

### JSON 形式でスナップショットをとる

起動時の表示の通り、`s + enter` でスナップショットをとれる。
次回以降にこのファイルを指定するなり、db.json にマージして利用する事で目的を達成できる。

“`
Type s + enter at any time to create a snapshot of the database
Watching…
GET /db 304 9.996 ms – –
GET /posts 200 10.010 ms – 77
s
Saved snapshot to db-1481811331533.json
“`

## 補足:ツールの使い勝手

`posts?q=eos` や `posts?q=山` を試したが、RubyMine では動かなかった。
DHC で試した所、意図したとおりに値が返ってきた。
どうやら JetBrains では足りない模様。

## 補遺

> * [DriftwoodJP/sandbox-json-server](https://github.com/DriftwoodJP/sandbox-json-server)
> * [gems: faker, gimei, romankana | deadwood](https://www.d-wood.com/blog/2013/11/04_4984.html)

追記:2016/12/21

> * [たった30秒でREST APIのモックが作れる JSON Serverでフロントエンド開発が捗る – WPJ](https://www.webprofessional.jp/mock-rest-apis-using-json-server/?utm_content=buffer70efc&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer)
[/markdown]