Skip to content

Commit

Permalink
Creating util function to validate handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopedrocampos committed Jul 20, 2021
1 parent ef788ee commit e3546d8
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 41 deletions.
4 changes: 2 additions & 2 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ class Agent extends Dispatcher {

dispatch (opts, handler) {
if (!handler || typeof handler !== 'object') {
throw new InvalidArgumentError('handler')
throw new InvalidArgumentError('handler must be an object.')
}

try {
if (!opts || typeof opts !== 'object') {
throw new InvalidArgumentError('opts must be a object.')
throw new InvalidArgumentError('opts must be an object.')
}

let key
Expand Down
6 changes: 3 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ class Client extends Dispatcher {

dispatch (opts, handler) {
if (!handler || typeof handler !== 'object') {
throw new InvalidArgumentError('handler')
throw new InvalidArgumentError('handler must be an object')
}

try {
if (!opts || typeof opts !== 'object') {
throw new InvalidArgumentError('opts must be a object.')
throw new InvalidArgumentError('opts must be an object.')
}

if (this[kDestroyed]) {
Expand Down Expand Up @@ -1093,7 +1093,7 @@ function connect (client) {
let { host, hostname, protocol, port } = client[kUrl]

// Resolve ipv6
if (hostname.startsWith('[')) {
if (hostname[0] === '[') {
const idx = hostname.indexOf(']')

assert(idx !== -1)
Expand Down
30 changes: 1 addition & 29 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,7 @@ class Request {
throw new InvalidArgumentError('headers must be an object or an array')
}

if (typeof handler.onConnect !== 'function') {
throw new InvalidArgumentError('invalid onConnect method')
}

if (typeof handler.onError !== 'function') {
throw new InvalidArgumentError('invalid onError method')
}

if (typeof handler.onBodySent !== 'function' && handler.onBodySent !== undefined) {
throw new InvalidArgumentError('invalid onBodySent method')
}

if (this.upgrade || this.method === 'CONNECT') {
if (typeof handler.onUpgrade !== 'function') {
throw new InvalidArgumentError('invalid onUpgrade method')
}
} else {
if (typeof handler.onHeaders !== 'function') {
throw new InvalidArgumentError('invalid onHeaders method')
}

if (typeof handler.onData !== 'function') {
throw new InvalidArgumentError('invalid onData method')
}

if (typeof handler.onComplete !== 'function') {
throw new InvalidArgumentError('invalid onComplete method')
}
}
util.validateHandler(handler, method, upgrade)

this.servername = util.getServerName(this.host)

Expand Down
39 changes: 38 additions & 1 deletion lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ function isBuffer (buffer) {
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
}

function validateHandler (handler, method, upgrade) {
if (!handler || typeof handler !== 'object') {
throw new InvalidArgumentError('handler must be an object')
}

if (typeof handler.onConnect !== 'function') {
throw new InvalidArgumentError('invalid onConnect method')
}

if (typeof handler.onError !== 'function') {
throw new InvalidArgumentError('invalid onError method')
}

if (typeof handler.onBodySent !== 'function' && handler.onBodySent !== undefined) {
throw new InvalidArgumentError('invalid onBodySent method')
}

if (upgrade || method === 'CONNECT') {
if (typeof handler.onUpgrade !== 'function') {
throw new InvalidArgumentError('invalid onUpgrade method')
}
} else {
if (typeof handler.onHeaders !== 'function') {
throw new InvalidArgumentError('invalid onHeaders method')
}

if (typeof handler.onData !== 'function') {
throw new InvalidArgumentError('invalid onData method')
}

if (typeof handler.onComplete !== 'function') {
throw new InvalidArgumentError('invalid onComplete method')
}
}
}

module.exports = {
nop,
parseOrigin,
Expand All @@ -203,5 +239,6 @@ module.exports = {
destroy,
bodyLength,
deepClone,
isBuffer
isBuffer,
validateHandler
}
2 changes: 2 additions & 0 deletions lib/handler/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class RedirectHandler {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

util.validateHandler(handler, opts.method, opts.upgrade)

this.dispatcher = dispatcher
this.location = null
this.abort = null
Expand Down
2 changes: 1 addition & 1 deletion lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Pool extends Dispatcher {

dispatch (opts, handler) {
if (!handler || typeof handler !== 'object') {
throw new InvalidArgumentError('handler')
throw new InvalidArgumentError('handler must be an object')
}

try {
Expand Down
4 changes: 2 additions & 2 deletions test/client-dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('dispatch invalid opts', (t) => {
}, null)
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'handler')
t.equal(err.message, 'handler must be an object')
}

try {
Expand All @@ -29,7 +29,7 @@ test('dispatch invalid opts', (t) => {
}, 'asd')
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'handler')
t.equal(err.message, 'handler must be an object')
}

client.dispatch({
Expand Down
91 changes: 88 additions & 3 deletions test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ test('MockAgent - get', t => {
})

test('MockAgent - dispatch', t => {
t.plan(2)
t.plan(3)

t.test('should call the dispatch method of the MockPool', (t) => {
t.plan(1)
Expand All @@ -116,7 +116,8 @@ test('MockAgent - dispatch', t => {
}, {
onHeaders: (_statusCode, _headers, resume) => resume(),
onData: () => {},
onComplete: () => {}
onComplete: () => {},
onError: () => {}
}))
})

Expand All @@ -142,9 +143,93 @@ test('MockAgent - dispatch', t => {
}, {
onHeaders: (_statusCode, _headers, resume) => resume(),
onData: () => {},
onComplete: () => {}
onComplete: () => {},
onError: () => {}
}))
})

t.test('should throw if handler is not valid on redirect', (t) => {
t.plan(7)

const baseUrl = 'http://localhost:9999'

const mockAgent = new MockAgent()
t.teardown(mockAgent.close.bind(mockAgent))

t.throws(() => mockAgent.dispatch({
origin: baseUrl,
path: '/foo',
method: 'GET'
}, {
onError: 'INVALID'
}), new InvalidArgumentError('invalid onError method'))

t.throws(() => mockAgent.dispatch({
origin: baseUrl,
path: '/foo',
method: 'GET'
}, {
onError: (err) => { throw err },
onConnect: 'INVALID'
}), new InvalidArgumentError('invalid onConnect method'))

t.throws(() => mockAgent.dispatch({
origin: baseUrl,
path: '/foo',
method: 'GET'
}, {
onError: (err) => { throw err },
onConnect: () => {},
onBodySent: 'INVALID'
}), new InvalidArgumentError('invalid onBodySent method'))

t.throws(() => mockAgent.dispatch({
origin: baseUrl,
path: '/foo',
method: 'CONNECT'
}, {
onError: (err) => { throw err },
onConnect: () => {},
onBodySent: () => {},
onUpgrade: 'INVALID'
}), new InvalidArgumentError('invalid onUpgrade method'))

t.throws(() => mockAgent.dispatch({
origin: baseUrl,
path: '/foo',
method: 'GET'
}, {
onError: (err) => { throw err },
onConnect: () => {},
onBodySent: () => {},
onHeaders: 'INVALID'
}), new InvalidArgumentError('invalid onHeaders method'))

t.throws(() => mockAgent.dispatch({
origin: baseUrl,
path: '/foo',
method: 'GET'
}, {
onError: (err) => { throw err },
onConnect: () => {},
onBodySent: () => {},
onHeaders: () => {},
onData: 'INVALID'
}), new InvalidArgumentError('invalid onData method'))

t.throws(() => mockAgent.dispatch({
origin: baseUrl,
path: '/foo',
method: 'GET'
}, {
onError: (err) => { throw err },
onConnect: () => {},
onBodySent: () => {},
onHeaders: () => {},
onData: () => {},
onComplete: 'INVALID'
}), new InvalidArgumentError('invalid onComplete method'))
})
})

test('MockAgent - .close should clean up registered pools', async (t) => {
Expand Down

0 comments on commit e3546d8

Please sign in to comment.