- Notifications
You must be signed in to change notification settings - Fork83
Record and playback HTTP responses
License
flickr/yakbak
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Record HTTP interactions The Node Way™. Inspired by ruby'svcr.
$ npm install yakbak --save-dev
The main idea behind testing HTTP clients with yakbak is:
- Make your client's target host configurable.
- Set up a yakbak server locally to proxy the target host.
- Point your client at the yakbak server.
Then develop or run your tests. If a recorded HTTP request is found on disk, it will be played back instead of hitting the target host. If no recorded request is found, the request will be forwarded to the target host and recorded to disk.
Returns a function of the signaturefunction (req, res) that you can give to anhttp.Server as its handler.
varhandler=yakbak('http://api.flickr.com',{dirname:__dirname+'/tapes'});
dirnamethe path where recorded responses will be written (required).noRecordif true, requests will return a 404 error if the tape doesn't existhash(req, body)provide your own IncomingMessage hash function
yakbak provides a handler with the same signature thathttp.Server expects so you can create your own proxy:
varhttp=require('http');varyakbak=require('yakbak');http.createServer(yakbak('http://api.flickr.com',{dirname:__dirname+'/tapes'})).listen(3000);
Now any requests tohttp://localhost:3000 will be proxied tohttp://api.flickr.com and recorded to/tapes for future playback.
Need more flexibility?express expects the same function signature, so you can use yakbak just like you would any other middleware:
varexpress=require('express');varyakbak=require('yakbak');varflickr=yakbak('http://api.flickr.com',{dirname:__dirname+'/tapes'});varupload=yakbak('http://up.flickr.com',{dirname:__dirname+'/tapes'});express().use(function(req,res,next){if(req.path.indexOf('/services/upload')===0){upload(req,res);}else{flickr(req,res);}}).listen(3000);
Each recorded response is itself a node module with the same handler signature, so if you want to create a server that replays a single response, you can do so easily:
varhttp=require('http');vartape=require('./tapes/1117f3d81490d441d826dd2fb26470f9.js');http.createServer(tape).listen(3000);
yakbak also ships with ayakbak utility that will start an HTTP server to play back a given tape.
$ yakbakError: file is requiredUsage: yakbak<file>$ yakbak ./tapes/1117f3d81490d441d826dd2fb26470f9.jsServer listening on port 3000* Connection from 127.0.0.1 port 63669< GET / HTTP/1.1< host: localhost:3000< user-agent: curl/7.43.0< accept:*/*<> HTTP/1.1 201 Created> content-type: text/html> date: Sat, 26 Oct 1985 08:20:00 GMT> connection: close> transfer-encoding: chunked>* Connection closed
Check outthis blog post about why we chose a reverse proxy over other existing approaches to recording HTTP interactions.
This software is free to use under the MIT license. See theLICENSE file for license text and copyright information.
About
Record and playback HTTP responses
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.