request-test
v1.0.0
Published
Convenience wrapper around Node core requests to easily test whether a request fails or succeeds.
Downloads
5
Maintainers
Readme
request-test
Convenience wrapper around Node core requests to easily test whether a request fails or succeeds.
- Supports
http
andhttps
. - Supports timeouts and cancelling requests.
- Test results:
success
: if the request was cancelled or returned a 100, 200 or 300 HTTP status codefail
: if the request had an error, timed out, or returned 400 or 500 HTTP status code
Installation
npm install request-test
Quickstart
> setting a timeout
const test = require('request-test')
const request = test({ url: 'https://slow.com', timeout: 1000 })
request.once('fail', reason => console.log(reason)) // => 'ETIMEDOUT'
> cancelling a request
const test = require('request-test')
const request = test({ url: 'https://slow.com' })
request.once('success', reason => console.log(reason)) // => 'CANCELLED'
setTimeout(() => {
request.cancel()
}, 1000)
API
const test = require('request-test')
request = test({ url, timeout })
> Starts a request test with the specified url and timeout
- url
<String>
: the url the request will be sent to; default:http://localhost
- timeout
<Number>
: the timeout in milliseconds after which the request will be aborted; default:2000
- returns a
<ClientRequest>
which emits one of the following events:- success =>
<String> / <Number>
: the reason why the request succeeded, e.g. a 100, 200 or 300 HTTP status code orCANCELLED
- fail =>
<String> / <Number>
: the reason why the request failed, e.g.: a 400 or 500 HTTP status code or the error code of a Node network errror, e.g.ETIMEDOUT
,ENOTFOUND
,ECONNREFUSED
, etc.
- success =>
- throws an
<Error>
: if the protocol of the url is neitherhttp
norhttps
request.cancel()
> Cancels the sent request
- Note that upon calling this method the request will be considered a
success
.
License
WTFPL – Do What the F*ck You Want to Public License.
Made with :heart: by @MarkTiedemann.