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

fix: default mock interceptor to GET #1285

Merged
merged 2 commits into from Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mock/mock-interceptor.js
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
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
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
4 changes: 2 additions & 2 deletions types/mock-interceptor.d.ts
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for the type? We use tsd!

/** Body to intercept on. */
body?: string | RegExp | ((body: string) => boolean);
/** Headers to intercept on. */
Expand Down