Skip to content

Commit

Permalink
fix: default mock interceptor to GET (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Mar 27, 2022
1 parent 65feb05 commit 48c326f
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 63 deletions.
54 changes: 9 additions & 45 deletions docs/api/MockAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

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

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const { statusCode, body } = await request('http://localhost:3000/foo')

Expand All @@ -95,11 +91,7 @@ import { MockAgent, request } from 'undici'
const mockAgent = new MockAgent()

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

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -121,11 +113,7 @@ import { MockAgent, request } from 'undici'
const mockAgent = new MockAgent()

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

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -147,11 +135,7 @@ import { MockAgent, request } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })

const mockClient = mockAgent.get('http://localhost:3000')

mockClient.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -174,16 +158,8 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

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

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')

mockPool.intercept({
path: '/hello',
method: 'GET'
}).reply(200, 'hello')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
mockPool.intercept({ path: '/hello'}).reply(200, 'hello')

const result1 = await request('http://localhost:3000/foo')

Expand Down Expand Up @@ -250,11 +226,7 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

const mockPool = mockAgent.get(new RegExp('http://localhost:3000'))

mockPool.intercept({
path: '/foo',
method: 'GET',
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -277,11 +249,7 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

const mockPool = mockAgent.get((origin) => origin === 'http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down Expand Up @@ -328,11 +296,7 @@ import { MockAgent } from 'undici'
const mockAgent = new MockAgent()

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

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down
5 changes: 1 addition & 4 deletions docs/api/MockClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ import { MockAgent } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })

const mockClient = mockAgent.get('http://localhost:3000')
mockClient.intercept({
path: '/foo',
method: 'GET',
}).reply(200, 'foo')
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down
6 changes: 1 addition & 5 deletions docs/api/MockPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ setGlobalDispatcher(mockAgent)

// MockPool
const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET',
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down
2 changes: 1 addition & 1 deletion lib/mock/mock-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MockInterceptor {
throw new InvalidArgumentError('opts.path must be defined')
}
if (typeof opts.method === 'undefined') {
throw new InvalidArgumentError('opts.method must be defined')
opts.method = 'GET'
}
// See https://github.com/nodejs/undici/issues/1245
// As per RFC 3986, clients are not supposed to send URI
Expand Down
5 changes: 2 additions & 3 deletions test/mock-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,13 @@ test('MockClient - intercept validation', (t) => {
t.throws(() => mockClient.intercept({}), new InvalidArgumentError('opts.path must be defined'))
})

t.test('it should error if no method specified in the intercept', t => {
t.test('it should default to GET if no method specified in the intercept', t => {
t.plan(1)
const mockAgent = new MockAgent({ connections: 1 })
t.teardown(mockAgent.close.bind(mockAgent))

const mockClient = mockAgent.get('http://localhost:9999')

t.throws(() => mockClient.intercept({ path: '/foo' }), new InvalidArgumentError('opts.method must be defined'))
t.doesNotThrow(() => mockClient.intercept({ path: '/foo' }))
})
})

Expand Down
5 changes: 2 additions & 3 deletions test/mock-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,13 @@ test('MockPool - intercept validation', (t) => {
t.throws(() => mockPool.intercept({}), new InvalidArgumentError('opts.path must be defined'))
})

t.test('it should error if no method specified in the intercept', t => {
t.test('it should default to GET if no method specified in the intercept', t => {
t.plan(1)
const mockAgent = new MockAgent()
t.teardown(mockAgent.close.bind(mockAgent))

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

t.throws(() => mockPool.intercept({ path: '/foo' }), new InvalidArgumentError('opts.method must be defined'))
t.doesNotThrow(() => mockPool.intercept({ path: '/foo' }))
})
})

Expand Down
1 change: 1 addition & 0 deletions test/types/mock-interceptor.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MockInterceptor, MockScope } from '../../types/mock-interceptor'
{
const mockPool: MockPool = new MockAgent().get('')
const mockInterceptor = mockPool.intercept({ path: '', method: 'GET' })
const mockInterceptorDefaultMethod = mockPool.intercept({ path: '' })

// reply
expectAssignable<MockScope>(mockInterceptor.reply(200, ''))
Expand Down
4 changes: 2 additions & 2 deletions types/mock-interceptor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ declare namespace MockInterceptor {
export interface Options {
/** Path to intercept on. */
path: string | RegExp | ((path: string) => boolean);
/** Method to intercept on. */
method: string | RegExp | ((method: string) => boolean);
/** Method to intercept on. Defaults to GET. */
method?: string | RegExp | ((method: string) => boolean);
/** Body to intercept on. */
body?: string | RegExp | ((body: string) => boolean);
/** Headers to intercept on. */
Expand Down

0 comments on commit 48c326f

Please sign in to comment.