Git Product home page Git Product logo

stripe-stateful-mock's Introduction

stripe-stateful-mock

Simulates a stateful Stripe server for local unit testing. Makes Stripe calls 50-100x faster than testing against the official server.

Supported features:

  • charges: create with the most common test tokens or customer card, retrieve, list, update, capture
  • refunds: create, retrieve, list
  • customers: create, retrieve, list, update, create card, retrieve card, delete card
  • products: create, retrieve, list
  • plans: create, retrieve, list
  • subscriptions: create, retrieve, list
  • connect accounts: create and delete
  • idempotency

Correctness of this test server is not guaranteed! Set up unit testing to work against either the Stripe server with a test account or this mock server with a flag to switch between them. Test against the official Stripe server occasionally to ensure correctness on the fine details.

Usage

The server can be started in multiple ways but in each case it starts an HTTP server (default port is 8000) which can be connected to with any Stripe client. For example in JavaScript...

const Stripe = require("stripe");
let client = new Stripe("sk_test_foobar");
client.setHost("localhost", 8000, "http");

The server supports the following settings through environment variables:

  • LOG_LEVEL sets the log output verbosity. Values are: silent, error, warn, info, debug.
  • PORT the port to start the server on. Defaults to 8000.

As a shell command

Install globally: npm i -g stripe-stateful-mock

Run: node stripe-stateful-mock

As part of a Mocha unit test

Install as a development dependency: npm i -D stripe-stateful-mock

In your NPM test script: mocha --require stripe-stateful-mock/autostart

Custom

The server is implemented as an Express 4 application and can be fully controlled. This does not start the server by default.

const http = require("http");
const https = require("https");
const stripeStatefulMock = require("stripe-stateful-mock");

const app = stripeStatefulMock.createExpressApp();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);

Bonus features

This server supports a few bonus parameters to test scenarios not testable against the real server.

Source token tok_429

Use the charge source token tok_429 to get a 429 (rate limit) response from the server.

Source token tok_500

Use the charge source token tok_500 to get a 500 (server error) response from the server.

Source token tok_forget

Use the charge source token tok_forget to get a normal charge response from the server that is not saved.

Source token chains

Send a source token composed of multiple test tokens separated by pipes (|) to get the response for each call in the order the tokens are composed.

Example 1

tok_chargeDeclinedInsufficientFunds|tok_visa

The first time this charge source token is used an insufficient funds response will be sent. The second time it's used the charge will succeed with a visa transaction.

Example 2

tok_500|tok_500|tok_visa|asdfasdf

The first and second time this charge source token is used a 500 response is returned. The third time the charge will succeed with a visa transaction.

A random string is appended to the end to guarantee this sequence won't be confused for a similar test (eg tok_500|tok_500|tok_visa|hjklhjkl) that may be running simultaneously. It's a lazy hack that accomplishes namespacing.

Existing work

stripe-mock - Stripe's official mock server has better POST body checking but is stateless.

stripe-ruby-mock - Patches the Stripe Ruby client so it only works with Ruby.

mock-stripe - Written in Go and I can't be bothered.

stripe-stateful-mock's People

Contributors

raynos avatar pushplay avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.