今やシステム開発時においてWeb APIを使わない手はなくなっています。開発を効率化したり、自分たちが持っていない技術もWeb APIを使うことで即座に手に入れることができます。しかし、開発中にもネットワークが必要になったり、負荷テストはしづらいといった欠点もあります。 そこで参考にしたいのがstripe-mockです。決済APIを提供するStripeが開発したモックサーバです。

stripe-mockの使い方

stripe-mockを立てた後はStripeのAPIと同じようにアクセスできます。

$ curl -i http://localhost:12111/v1/charges -H "Authorization: Bearer sk_test_123"
Request: GET /v1/charges
Response: elapsed=3.677ms status=200
HTTP/1.1 200 OK
Request-Id: req_123
Stripe-Mock-Version: master
Date: Mon, 13 Aug 2018 12:23:02 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked

{
  "data": [
    {
      "amount": 100,
      "amount_refunded": 0,
      "application": null,
      "application_fee": null,
      "balance_transaction": "txn_1CuhR0KF7bt86x7ql6wVnTym",
      "captured": false,
      "created": 1234567890,
      "currency": "usd",
      "customer": null,
    }
    :
  ]
}

返ってくるデータはデモデータなので、すべての実装ができる訳ではありません。しかし、開発中はstripe-mockを使うことで思わぬアクセスを防止したり、取得されるデータを学ぶこともできるでしょう。何より公式で提供されていることのメリットが大きいでしょう。

stripe-mockはGo製のオープンソース・ソフトウェア(MIT License)です。

stripe/stripe-mock: stripe-mock is a mock HTTP server that responds like the real Stripe API. It can be used instead of Stripe’s testmode to make test suites integrating with Stripe faster and less brittle.