From 33ac3d24f69945e26ddbbef3ada7162f98405c7e Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Sun, 13 Mar 2022 20:46:36 -0700 Subject: [PATCH 1/2] fix: default mock interceptor to GET --- lib/mock/mock-interceptor.js | 2 +- test/mock-client.js | 5 ++--- test/mock-pool.js | 5 ++--- types/mock-interceptor.d.ts | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/mock/mock-interceptor.js b/lib/mock/mock-interceptor.js index a10c71debb5..699bec41287 100644 --- a/lib/mock/mock-interceptor.js +++ b/lib/mock/mock-interceptor.js @@ -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 diff --git a/test/mock-client.js b/test/mock-client.js index c90a6311dd2..e5a89af3d4c 100644 --- a/test/mock-client.js +++ b/test/mock-client.js @@ -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' })) }) }) diff --git a/test/mock-pool.js b/test/mock-pool.js index f15cc7ee781..f89c3c5cea1 100644 --- a/test/mock-pool.js +++ b/test/mock-pool.js @@ -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' })) }) }) diff --git a/types/mock-interceptor.d.ts b/types/mock-interceptor.d.ts index 0166b1f1db3..2e4272176ad 100644 --- a/types/mock-interceptor.d.ts +++ b/types/mock-interceptor.d.ts @@ -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. */ From f57d4f341cadebd32f75590e45879994228ec529 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Sat, 26 Mar 2022 19:14:15 -0700 Subject: [PATCH 2/2] update docs and add type test --- docs/api/MockAgent.md | 54 +++++---------------------- docs/api/MockClient.md | 5 +-- docs/api/MockPool.md | 6 +-- test/types/mock-interceptor.test-d.ts | 1 + 4 files changed, 12 insertions(+), 54 deletions(-) diff --git a/docs/api/MockAgent.md b/docs/api/MockAgent.md index 4500ef13c6b..f94ae339f96 100644 --- a/docs/api/MockAgent.md +++ b/docs/api/MockAgent.md @@ -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') @@ -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, @@ -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, @@ -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, @@ -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') @@ -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, @@ -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, @@ -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, diff --git a/docs/api/MockClient.md b/docs/api/MockClient.md index de73b24702e..ac546913d23 100644 --- a/docs/api/MockClient.md +++ b/docs/api/MockClient.md @@ -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, diff --git a/docs/api/MockPool.md b/docs/api/MockPool.md index df861255941..29667f3177b 100644 --- a/docs/api/MockPool.md +++ b/docs/api/MockPool.md @@ -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, diff --git a/test/types/mock-interceptor.test-d.ts b/test/types/mock-interceptor.test-d.ts index 1d04b8385ab..cfbf12ba826 100644 --- a/test/types/mock-interceptor.test-d.ts +++ b/test/types/mock-interceptor.test-d.ts @@ -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(mockInterceptor.reply(200, ''))