Skip to content

Commit

Permalink
📦 NEW: Enable Mocking request from undici (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jul 17, 2022
1 parent 2ef38b9 commit 3bc7461
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@ Response is normal object, it contains:
NODE_DEBUG=urllib npm test
```

## Mocking Request

export from [undici](https://undici.nodejs.org/#/docs/best-practices/mocking-request)

```ts
import { strict as assert } from 'assert';
import { MockAgent, setGlobalDispatcher, request } from 'urllib';

const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);

const mockPool = mockAgent.get('http://localhost:7001');

mockPool.intercept({
path: '/foo',
method: 'POST',
}).reply(400, {
message: 'mock 400 bad request',
});

const response = await request('http://localhost:7001/foo', {
method: 'POST',
dataType: 'json',
});
assert.equal(response.status, 400);
assert.deepEqual(response.data, { message: 'mock 400 bad request' });
```

## Benchmarks

Fork [undici benchmarks script](https://github.com/fengmk2/undici/blob/urllib-benchmark/benchmarks/benchmark.js)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function request(url: RequestURL, options?: RequestOptions) {
}

export { HttpClient, HEADER_USER_AGENT as USER_AGENT } from './HttpClient';
export { MockAgent, setGlobalDispatcher } from 'undici';

export default {
request,
Expand Down
37 changes: 37 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { strict as assert } from 'assert';
import urllib from '../src';
import { MockAgent, setGlobalDispatcher } from '../src';
import { startServer } from './fixtures/server';

describe('index.test.ts', () => {
Expand Down Expand Up @@ -69,4 +70,40 @@ describe('index.test.ts', () => {
assert.match(urllib.USER_AGENT, /urllib\//);
});
});

describe('Mocking request', () => {
it('should mocking intercept work', async () => {
const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);

const mockPool = mockAgent.get(_url.substring(0, _url.length - 1));
mockPool.intercept({
path: '/foo',
method: 'POST',
}).reply(400, {
message: 'mock 400 bad request',
});

mockPool.intercept({
path: /\.tgz$/,
method: 'GET',
}).reply(400, {
message: 'mock 400 bad request on tgz',
});

let response = await urllib.request(`${_url}foo`, {
method: 'POST',
dataType: 'json',
});
assert.equal(response.status, 400);
assert.deepEqual(response.data, { message: 'mock 400 bad request' });

response = await urllib.request(`${_url}download/foo.tgz`, {
method: 'GET',
dataType: 'json',
});
assert.equal(response.status, 400);
assert.deepEqual(response.data, { message: 'mock 400 bad request on tgz' });
});
});
});

0 comments on commit 3bc7461

Please sign in to comment.