# request-test

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

Latest version **1.0.0** (published 2016-05-23) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install request-test
pnpm add request-test
yarn add request-test
bun add request-test
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-05-23 |
| First published | 2016-05-23 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mark Tiedemann |
| Maintainers | marktiedemann |
| Keywords | http, https, request, test, check, success, fail, wrapper, cancel, abort |

## Links

- npm: https://www.npmjs.com/package/request-test
- Repository: https://github.com/MarkTiedemann/request-test
- Issues: https://github.com/MarkTiedemann/request-test/issues
- npm.io page: https://npm.io/package/request-test

## Dependencies (1)

- [once](https://npm.io/package/once.md) ^1.3.3

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2016-05-23

## README

# request-test

[![](https://travis-ci.org/MarkTiedemann/request-test.svg?branch=master)](https://travis-ci.org/MarkTiedemann/request-test)
[![](https://david-dm.org/MarkTiedemann/request-test.svg)](https://david-dm.org/MarkTiedemann/request-test) [![](https://img.shields.io/node/v/request-test.svg)](https://www.npmjs.com/package/request-test)

**Convenience wrapper around [Node core requests](https://nodejs.org/api/http.html#http_http_request_options_callback) 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**

```js

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**

```js

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

```js
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](http://www.wtfpl.net/) – Do What the F*ck You Want to Public License.

Made with :heart: by [@MarkTiedemann](https://twitter.com/MarkTiedemannDE).

---
_Source: https://npm.io/package/request-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
