Double Agent
doubleagent
is an ES7 async/await
compatible wrapper on top of superagent that makes testing of expressjs/connect apps easier in a modern environment.
Lets say you have an expressjs app, that looks somewhat like this:
; const app = ; app; ;
Now, doubleagent
allows you to test the app like it's 2016 out there:
;;; ;
There are two main points to this whole thing:
- Using ES7
async/await
syntax for more maintainable tests codebase - Use your favorite assertion library for testing
API & Usage
Installation is straight forward
npm install doubleagent --save-dev
As for the API, the agent(app)
call returns a object with all the basic HTTP methods that all have the same API:
agent(app)[method](path[, params[, headers[, files]]]) -> Promise
NOTE: params
are contextual, they are handled as query
for GET
/HEAD
requests and as body
for POST
, PUT
, etc. When files are passed the request changes into a multi-part form and params is treated as fields.
Default Headers
If you need to specify app wide default HTTP headers, just assign them to the defaultHeaders
property:
agentdefaultHeaders = Authorization: `Bearer ` ;agent; // <- will automatically send the headers
NOTE any headers that you send through with specific #get
, #post
, etc. requests will override the defaultHeaders
values.
Full URL Locations
If you need to access a full URL location to the http server that runs underneath the doubleagent
interface, please use the #urlFor(path)
method;
agent; // -> 'http://127.0.0.1:0/users'
Custom Query String Encoding
Under the hood, doubleagent
uses superagent
. Superagent uses the qs
library for both query string stringification and parsing. However you may use a different form of querystring encoding such that it matches your application. You can optionally pass in a custom parser to doubleagent
.
;;; /* define a custom stringify function */const stringify = qs;const test = ; ;
Copyright & License
All code in this library is released under the terms of the MIT license
Copyright (C) 2016 Nikolay Nemshilov