Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📦 NEW: Enable Mocking request from undici #386

Merged
merged 2 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
});
});
});