Skip to content

Commit

Permalink
refactor: default function arguments (#1640)
Browse files Browse the repository at this point in the history
Make use of in-line default args, available in Node since v6.
  • Loading branch information
mastermatt committed Jul 23, 2019
1 parent 9c504f6 commit 87cac20
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/common.js
Expand Up @@ -44,6 +44,7 @@ const normalizeRequestOptions = function(options) {
* accordingly. We've inadvertently gotten it flipped.
*
* @param {Object} buffer - a Buffer object
* @returns {boolean}
*/
const isUtf8Representable = function(buffer) {
if (!Buffer.isBuffer(buffer)) {
Expand Down
1 change: 0 additions & 1 deletion lib/intercept.js
Expand Up @@ -74,7 +74,6 @@ function isEnabledForNetConnect(options) {
/**
* Disable all real requests.
* @public
* @param {String|RegExp} matcher=RegExp.new('.*') Expression to match
* @example
* nock.disableNetConnect();
*/
Expand Down
19 changes: 8 additions & 11 deletions lib/interceptor.js
Expand Up @@ -79,11 +79,13 @@ function Interceptor(scope, uri, method, requestBody, interceptorOptions) {
}
}

Interceptor.prototype.optionally = function optionally(value) {
Interceptor.prototype.optionally = function optionally(flag = true) {
// The default behaviour of optionally() with no arguments is to make the mock optional.
value = typeof value === 'undefined' ? true : value
if (typeof flag !== 'boolean') {
throw new Error('Invalid arguments: argument should be a boolean')
}

this.optional = value
this.optional = flag

return this
}
Expand Down Expand Up @@ -441,14 +443,9 @@ Interceptor.prototype.matchHeader = function matchHeader(name, value) {
return this
}

Interceptor.prototype.basicAuth = function basicAuth(options) {
const username = options['user']
const password = options['pass'] || ''
const name = 'authorization'
const value = `Basic ${Buffer.from(`${username}:${password}`).toString(
'base64'
)}`
this.interceptorMatchHeaders.push({ name, value })
Interceptor.prototype.basicAuth = function basicAuth({ user, pass = '' }) {
const encoded = Buffer.from(`${user}:${pass}`).toString('base64')
this.matchHeader('authorization', `Basic ${encoded}`)
return this
}

Expand Down
6 changes: 3 additions & 3 deletions lib/scope.js
Expand Up @@ -231,11 +231,11 @@ class Scope extends EventEmitter {
return this
}

persist(flag) {
this._persist = flag == null ? true : flag
if (typeof this._persist !== 'boolean') {
persist(flag = true) {
if (typeof flag !== 'boolean') {
throw new Error('Invalid arguments: argument should be a boolean')
}
this._persist = flag
return this
}

Expand Down
9 changes: 9 additions & 0 deletions tests/test_persist_optionally.js
Expand Up @@ -44,6 +44,15 @@ test('calling optionally(false) on a mock leaves it as required', t => {
t.end()
})

test('calling optionally() with a non-boolean argument throws an error', t => {
const interceptor = nock('http://example.test').get('/')

t.throws(() => interceptor.optionally('foo'), {
message: 'Invalid arguments: argument should be a boolean',
})
t.end()
})

test('optional mocks are still functional', t => {
nock('http://example.test')
.get('/abc')
Expand Down

0 comments on commit 87cac20

Please sign in to comment.