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: do not throw unhandled exception when data is undefined in interceptor.reply #4036

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ function getResponseData (data) {
return data
} else if (typeof data === 'object') {
return JSON.stringify(data)
} else {
} else if (data) {
return data.toString()
} else {
return ''
}
}

Expand Down
40 changes: 40 additions & 0 deletions test/mock-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,46 @@ describe('MockInterceptor - reply options callback', () => {
})
})

test('should handle undefined data', t => {
t = tspl(t, { plan: 2 })

const mockInterceptor = new MockInterceptor({
path: '',
method: ''
}, [])
const result = mockInterceptor.reply((options) => ({
statusCode: 200,
data: undefined
}))
t.ok(result instanceof MockScope)

// Test parameters

const baseUrl = 'http://localhost:9999'
const mockAgent = new MockAgent()
after(() => mockAgent.close())

const mockPool = mockAgent.get(baseUrl)

mockPool.intercept({
path: '/test',
method: 'GET'
}).reply((options) => {
t.deepStrictEqual(options, { path: '/test', method: 'GET', headers: { foo: 'bar' } })
return { statusCode: 200, data: 'hello' }
})

mockPool.dispatch({
path: '/test',
method: 'GET',
headers: { foo: 'bar' }
}, {
onHeaders: () => { },
onData: () => { },
onComplete: () => { }
})
})

test('should error if passed options invalid', async (t) => {
t = tspl(t, { plan: 4 })

Expand Down
6 changes: 6 additions & 0 deletions test/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ describe('getResponseData', () => {
const responseData = getResponseData(new TextEncoder().encode('{"test":true}').buffer)
t.ok(responseData instanceof ArrayBuffer)
})

test('it should handle undefined', (t) => {
t = tspl(t, { plan: 1 })
const responseData = getResponseData(undefined)
t.strictEqual(responseData, '')
})
})

test('getStatusText', (t) => {
Expand Down
Loading