All Projects → ruyadorno → Snapstub

ruyadorno / Snapstub

Licence: mit
Copy API endpoints to your fs and run a local server using them

Programming Languages

javascript
184084 projects - #8 most used programming language
js
455 projects

Projects that are alternatives of or similar to Snapstub

Ipt
Interactive Pipe To: The Node.js cli interactive workflow
Stars: ✭ 783 (+305.7%)
Mutual labels:  cli, command-line, unix
Jtc
JSON processing utility
Stars: ✭ 425 (+120.21%)
Mutual labels:  cli, command-line, unix
Survey
A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
Stars: ✭ 2,843 (+1373.06%)
Mutual labels:  cli, command-line, unix
Ed
A modern UNIX ed (line editor) clone written in Go
Stars: ✭ 44 (-77.2%)
Mutual labels:  cli, command-line, unix
Commander
🚀The framework to write type-safe and structured command line program easily in Swift.
Stars: ✭ 170 (-11.92%)
Mutual labels:  cli, command-line
Cistern
A terminal UI for Unix to monitor Continuous Integration pipelines from the command line. Current integrations include GitLab, Azure DevOps, Travis CI, AppVeyor and CircleCI.
Stars: ✭ 161 (-16.58%)
Mutual labels:  cli, unix
Mandown
man-page inspired Markdown viewer
Stars: ✭ 173 (-10.36%)
Mutual labels:  cli, command-line
Magicli
Automagically generates command-line interfaces (CLI) for any module. Expected options and help sections are created automatically based on parameters names, with support to async.
Stars: ✭ 178 (-7.77%)
Mutual labels:  cli, command-line
Cbox
convert any python function to unix-style command
Stars: ✭ 154 (-20.21%)
Mutual labels:  cli, unix
Tably
Python command-line script for converting .csv data to LaTeX tables
Stars: ✭ 173 (-10.36%)
Mutual labels:  cli, command-line
Cointop
A fast and lightweight interactive terminal based UI application for tracking cryptocurrencies 🚀
Stars: ✭ 2,912 (+1408.81%)
Mutual labels:  cli, command-line
Proji
A powerful cross-platform CLI project templating tool.
Stars: ✭ 156 (-19.17%)
Mutual labels:  cli, command-line
Prism
Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
Stars: ✭ 2,484 (+1187.05%)
Mutual labels:  cli, mock-server
Qoa
Minimal interactive command-line prompts
Stars: ✭ 2,007 (+939.9%)
Mutual labels:  cli, command-line
Handlr
A better xdg-utils
Stars: ✭ 151 (-21.76%)
Mutual labels:  cli, command-line
You Dont Need Gui
Stop relying on GUI; CLI **ROCKS**
Stars: ✭ 4,766 (+2369.43%)
Mutual labels:  cli, command-line
Commander
Test your command line interfaces on windows, linux and osx and nodes viá ssh and docker
Stars: ✭ 183 (-5.18%)
Mutual labels:  cli, command-line
Fzy
🔍 A simple, fast fuzzy finder for the terminal
Stars: ✭ 2,295 (+1089.12%)
Mutual labels:  cli, unix
Xo
Command line utility that composes regular expression matches.
Stars: ✭ 184 (-4.66%)
Mutual labels:  cli, unix
Gcli
🖥 Go CLI application, tool library, running CLI commands, support console color, user interaction, progress display, data formatting display, generate bash/zsh completion add more features. Go的命令行应用,工具库,运行CLI命令,支持命令行色彩,用户交互,进度显示,数据格式化显示,生成bash/zsh命令补全脚本
Stars: ✭ 188 (-2.59%)
Mutual labels:  cli, command-line

snapstub logo

snapstub

NPM version Build Status License

Snapshot-based stub/mocking of APIs

Small command line tool that allows you to take "snapshots" of any given API endpoint and store the response. Using the start command will spawn a server that will serve all previously stored endpoints.

Heavily inspired by Jest Snapshot testing

Table of Contents

Install

npm install -g snapstub

Usage

Make sure you're in the desired folder to host your api mock files.

⬇️ Creates a new api stub:

snapstub add http://example.com/api/foo/bar

...create as many snapshots as you want.

🚀 Starts your mock server:

snapstub start

✨ Your endpoint will be locally available at: http://localhost:8059/api/foo/bar


Advanced Usage

Using different http methods

If you want to save one or many different http methods, use the --method option:

snapstub add http://example.com/api/foo/bar --method=get,post,put

Using custom headers to add a new route

If you need to pass a custom header along with your request, let's say one is needed for a auth token or any other reason, use the --header option:

snapstub add http://example.com/api/user/42 --header "X-Token: 1234"

You can set as many custom headers as you need:

snapstub add http://example.com/api/login --header "X-User: foo" --header "X-Token: bar"

Using html or plain text

In order to save or add a html or plain text endpoint a --nojson flag must be specified in order to bypass json encode/decode:

snapstub add http://example.com/html --nojson

Sending data when adding a new route

Usually a POST/PUT method will also require data to be sent along with the request, you can do so by using the --data option:

snapstub add http://example.com/api/user/new --data "name=Foo"

Content-Type headers will be set automatically but if you specify one (using --header option) that will take precedence.

It also accepts json data (Content-Type will be set to application/json automatically):

snapstub add http://example.com/api/user/new --data '{ "name": "Lorem" }'

If no method is defined it defaults to POST, if you want to use PUT instead just use the --method option:

snapstub add http://example.com/api/user/update --data "name=Bar" --method=put

Sending data read from a file

You can also point the --data option to a file in order to use the contents of that file as a payload. This is a good way to maintain repeatable calls to POST/PUT routes. Given that there is a payload.json file in the current working directory:

snapstub add http://example.com/api/user/add --data ./payload.json

Headers will be automatically added and the content will be exactly as read from the file.

Saving standard input data into a new endpoint

This is a very useful workflow that allows you to combine Chrome's Copy as cURL web dev tools option with the ability to store a snapstub route.

curl http://example.com/api/foo | snapstub save /api/foo

or you can just save any arbitrary data:

cat foo.json | snapstub save /api/foo # similar to $ cp foo.json __mocks__/api/foo

or even:

echo '{"data":true}' | snapstub save /api/foo

Deterministic mocks using query-string or headers

snapstub provides out of the box support to deterministic mocking by using query-string, headers, cookies thanks to request-hash. Deterministic results for those cases will create a file name suffixed by a unique hash which snapstub uses in order to serve the correct value when using the snapstub start server.

Deterministic results for query strings in the urls is active by default, for headers or cookies however you'll need to configure which values you'll want to use in order to create the unique hash. It can be set using the --hashHeaders or --hashCookies options:

snapstub add http://example.com/api/user/42 --header "X-Token: 1234" --hashHeaders=x-token
✔  Successfully added: __mocks__/api/user/42/get-491db12454c89a51646710b06bc6c51f9d45.json

Both --hashHeaders and --hashCookies accepts a comma-separated list of keys.

Customizing hash algorithm

The algorithm used to create the unique hash can also be customized using the --hashAlgorithm option.

Change defaults

Using custom port and/or folder name:

export SNAPSTUB_FOLDER_NAME=my-mock-folder/
export SNAPSTUB_PORT=9000
snapstub start

More info

By default snapshots will be saved in a __mocks__ folder that resolves from the current working directory, so make sure you run the commands from the correct project folder you desire.


Programmatic API

A JavaScript programmatic api is available if you're using node.js:

const snapstub = require('snapstub');

// starts the mock server
snapstub.start({
	verbose: true,
	mockFolderName: '__mocks__',
	port: 8080
});

The following methods are available, please note all values used are just example values meant to ilustrate a common usage:

add(opts)

snapstub.add({
	url: 'http://example.com/api/v1/data',
	addOptions: {
		headers: {
			'content-type': 'application/json',
			'Cookie': 'FOO=bar;'
		},
		body: 'lorem=ipsum',
		method: 'post'
	},
	mockFolderName: '__mocks__'
});

save(opts)

snapstub.save({
	url: '/api/v1/data',
	saveOptions: {
		data: { foo: 'bar' },
		headers: { 'X-Foo': 'bar' },
		hashAlgorithm: 'sha256',
		hashHeaders: ['x-foo'],
		hashCookies: [],
		method: 'post',
		nojson: false
	},
	mockFolderName: '__mocks__'
});

start(opts)

snapstub.start({
	hashAlgorithm: 'sha256',
	hashHeaders: ['x-foo'],
	hashCookies: [],
	verbose: true,
	mockFolderName: '__mocks__',
	port: 8080
});

stop()

snapstub.stop();

Credits

  • Logo: Camera by Simon Child from the Noun Project
  • snapstub wouldn't be possible without stubborn-server - it's a very flexible mock server based on static files, if you have the need to handle more complex scenarios (handling route params, dynamic responses, etc) go take a look at it.

Maintainers

Contribute

Please do! This is an open source project. If you would like a feature, open a pull request. If you have a bug or want to discuss something, open an issue.

License

MIT © Ruy Adorno

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].