request-test

    1.0.0 • Public • Published

    request-test

    Convenience wrapper around Node core requests to easily test whether a request fails or succeeds.

    • Supports http and https.
    • Supports timeouts and cancelling requests.
    • Test results:
      • success: if the request was cancelled or returned a 100, 200 or 300 HTTP status code
      • fail: 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 or CANCELLED
      • 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.
    • throws an <Error>: if the protocol of the url is neither http nor https

    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 ❤️ by @MarkTiedemann.

    Install

    npm i request-test

    DownloadsWeekly Downloads

    0

    Version

    1.0.0

    License

    WTFPL

    Last publish

    Collaborators

    • marktiedemann