AVA-http
Async HTTP requests.
AVA-http is a wrapper around the Request-Promise library, making it simpler to initiate HTTP requests.
It was made for succint API testing within AVA, but it can be used anywhere.
Installation
npm install --save-dev ava-http
Usage
The generic HTTP methods are included: [get
, post
, put
, delete
].
Each of these, used within the context of an AVA test, will return a Promise
that can be await
ed, yield
ed, or then
/catch
'd.
Erroneous responses will always return the full Response
object and should be caught with .catch()
.
Successful responses will return the payload
(aka, response.body
) by default.
If you would also like the full Response
object, exchange your method
for methodResponse
:
http.get
==>http.getResponse
http.post
==>http.postResponse
http.put
==>http.putResponse
http.del
==>http.delResponse
API
get(url[, options])
url
:string
options
:object
Returns: Promise
On resolve success, returns payload
. On error, returns full Response
object.
getResponse(url[, options])
url
:string
options
:object
Returns: Promise
Always returns full Response
object.
post(url[, options])
url
:string
options
:object
Returns: Promise
On resolve success, returns payload
. On error, returns full Response
object.
postResponse(url[, options])
url
:string
options
:object
Returns: Promise
Always returns full Response
object.
put(url[, options])
url
:string
options
:object
Returns: Promise
On resolve success, returns payload
. On error, returns full Response
object.
putResponse(url[, options])
url
:string
options
:object
Returns: Promise
Always returns full Response
object.
del(url[, options])
url
:string
options
:object
Returns: Promise
On resolve success, returns payload
. On error, returns full Response
object.
delResponse(url[, options])
url
:string
options
:object
Returns: Promise
Always returns full Response
object.
Options
form
Type:
object
Default:
{}
An object of key:value
pairs of formdata that will be urlencoded before reaching the server.
The request's headers
will automatically include 'content-type': 'application/x-www-form-urlencoded'
.
This simulates sending an HTML form via normal means.
body
Type:
object
Default:
{}
The payload data to be sent to the server. Leave json
as true
to automatically stringify as JSON.
headers
Type:
object
Default:
{}
The request headers to send.
params
Type:
object
Default:
{}
An alias of qs
.
qs
Type:
object
Default:
{}
The query string to append to the URL. See this example.
json
Type:
boolean
Default:
true
Whether or not the response body should be parsed as JSON.
transform
Type:
function
Default:
null
Transform the response into a custom value with which the promise is resolved. See here for info.
AVA
Setup
You must have AVA installed already.
;; ; ;
Examples
Thennables
;
Async Support
;
Generator Support
;
Response Headers
By default, successful responses will only yield their payloads. If you need/want to test a status code, for example, this will suffice:
;
Expecting Errors
In order to successfully expect and catch a Response Error, the test must be asserted witin the Promise's .catch()
.
For more information, please check out Request-Promise's excellent documentation on the Promise API.
;
Posting JSON
; // or, to also assert a statusCode...;
Posting as a HTML Form
All data within the form
object will be urlencoded
, just as any normal <form>
would!
;
Updating an Item
;
Deleting an Item
; // expecting an error...;
Pass an Authorization Token (eg, JWT)
;
Passing Query String Parameters
;
Goals
Ideally, AVA-http is the asynchronous, node-equivalent of PHPUnit. This means that similar, custom assertions will be added into AVA's Test class (as opposed to exporting an http
wrapper, as it currently stands). Methods will/should be as follows:
t.jsonEquals
t.jsonContains
t.jsonStructure
t.responseOk
t.responseNotOk
t.responseCode
t.responseType
t.headersEqual
t.headersContain
An ideal test setup may look like this:
; ; ; ; ;
TODOs
- Define a complete Micro test server, with routing
- Start-up test server on
test.before()
- Cleanup/Remove all
const url = await ...
within tests
License
MIT © Luke Edwards et al