# mock-http-client

> Create a mock http object for stubbing and simulating ajax requests in tests.

Latest version **0.3.2** (published 2017-09-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install mock-http-client
pnpm add mock-http-client
yarn add mock-http-client
bun add mock-http-client
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2017-09-20 |
| First published | 2017-05-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jack Ellis |
| Maintainers | jackmellis |

## Links

- npm: https://www.npmjs.com/package/mock-http-client
- Repository: https://github.com/jackmellis/mock-http
- Homepage: https://github.com/jackmellis/mock-http#readme
- Issues: https://github.com/jackmellis/mock-http/issues
- npm.io page: https://npm.io/package/mock-http-client

## Recent versions

- 0.3.2 (latest) — 2017-09-20
- 0.3.1 — 2017-07-04
- 0.3.0 — 2017-07-04
- 0.2.0 — 2017-05-29
- 0.1.0 — 2017-05-25

## README

# mock-http-client
Create a mock http object for stubbing and simulating ajax requests in tests.

[![npm version](https://badge.fury.io/js/mock-http-client.svg)](https://badge.fury.io/js/mock-http-client)
[![Build Status](https://travis-ci.org/jackmellis/mock-http.svg?branch=master)](https://travis-ci.org/jackmellis/mock-http)
[![Code Climate](https://codeclimate.com/github/jackmellis/mock-http/badges/gpa.svg)](https://codeclimate.com/github/jackmellis/mock-http)
[![Test Coverage](https://codeclimate.com/github/jackmellis/mock-http/badges/coverage.svg)](https://codeclimate.com/github/jackmellis/mock-http/coverage)

Install:
```
npm install mock-http-client --save-dev
```

Create mock http and pass it into your source code:
```js
import mockHttp from 'mock-http-client';
const http = mockHttp();

(myService || myComponent || myRewiredModule || mySystemUnderTest).http = http;

// http exposes a fairly common api:
http({method : 'post', url : '/api/1', data : {}});
http.get('/api/1');
http.put('/api/1', {});
http.post('/api/1', {});
http.patch('/api/1', {});
http.delete('/api/1', {});
```

Create responses:
```js
// create responses for specific queries
http.when('get', '/api/1').return({
  status : 200,
  data : {}
});
http.when('delete', '/api/1').reject(new Error());

// create responses for patterns
http.when(/\/api\/\d+/).call(config => {
  return {
    status : 204,
    data : config.data
  };
});

// expect requests and error if they are not met
http.expect('/api/3');
// run your code here
http.assert();
```

Docs:  
https://jackmellis.gitbooks.io/vuenit/content/http/introduction.html

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