Skip to content

Commit

Permalink
fix: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Aug 15, 2022
1 parent a72fb7c commit c9a8727
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 67 deletions.
4 changes: 3 additions & 1 deletion lib/check-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function checkErrors (method, res, startTime, opts) {
let parsed = body
try {
parsed = JSON.parse(body.toString('utf8'))
} catch (e) {}
} catch {
// ignore errors
}
if (res.status === 401 && res.headers.get('www-authenticate')) {
const auth = res.headers.get('www-authenticate')
.split(/,\s*/)
Expand Down
4 changes: 3 additions & 1 deletion lib/clean-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const cleanUrl = (str) => {
if (url.password) {
str = str.replace(url.password, replace)
}
} catch {}
} catch {
// ignore errors
}

return str
.replace(tokenRegex, `npm_${replace}`)
Expand Down
12 changes: 6 additions & 6 deletions test/check-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ t.test('log x-fetch-attempts header value', async t => {
let msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[, msg] = args
[, msg] = args
}
})
await t.rejects(checkResponse({
Expand All @@ -92,7 +92,7 @@ t.test('log the url fetched', t => {
let header, msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[header, msg] = args
[header, msg] = args
}
})
checkResponse({
Expand Down Expand Up @@ -121,7 +121,7 @@ t.test('redact password from log', t => {
let header, msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[header, msg] = args
[header, msg] = args
}
})
checkResponse({
Expand Down Expand Up @@ -149,7 +149,7 @@ t.test('redact well known token from log', t => {
let header, msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[header, msg] = args
[header, msg] = args
}
})
checkResponse({
Expand All @@ -176,7 +176,7 @@ t.test('report auth for registry, but not for this request', async t => {
let header, msg
process.on('log', (level, ...args) => {
if (level === 'warn') {
;[header, msg] = args
[header, msg] = args
}
})
await t.rejects(checkResponse({
Expand Down Expand Up @@ -214,7 +214,7 @@ t.test('logs the value of x-local-cache-status when set', t => {
let header, msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[header, msg] = args
[header, msg] = args
}
})
checkResponse({
Expand Down
26 changes: 10 additions & 16 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ t.test('OTP error', t => {
)
})

t.test('OTP error with prompt', t => {
t.test('OTP error with prompt', async t => {
let OTP = null
tnock(t, OPTS.registry)
.get('/otplease').times(2)
Expand All @@ -150,16 +150,13 @@ t.test('OTP error with prompt', t => {
})

const otpPrompt = async () => '12345'
return fetch('/otplease', { ...OPTS, otpPrompt })
.then(res => {
t.strictSame(res.status, 200, 'got 200 response')
return res.json()
}).then(body => {
t.strictSame(body, { ok: 'this is fine' }, 'got expected body')
})
const res = await fetch('/otplease', { ...OPTS, otpPrompt })
t.strictSame(res.status, 200, 'got 200 response')
const body = await res.json()
t.strictSame(body, { ok: 'this is fine' }, 'got expected body')
})

t.test('OTP error with prompt, expired OTP in settings', t => {
t.test('OTP error with prompt, expired OTP in settings', async t => {
let OTP = null
tnock(t, OPTS.registry)
.get('/otplease').times(2)
Expand All @@ -183,13 +180,10 @@ t.test('OTP error with prompt, expired OTP in settings', t => {
})

const otpPrompt = async () => '12345'
return fetch('/otplease', { ...OPTS, otpPrompt, otp: '98765' })
.then(res => {
t.strictSame(res.status, 200, 'got 200 response')
return res.json()
}).then(body => {
t.strictSame(body, { ok: 'this is fine' }, 'got expected body')
})
const res = await fetch('/otplease', { ...OPTS, otpPrompt, otp: '98765' })
t.strictSame(res.status, 200, 'got 200 response')
const body = await res.json()
t.strictSame(body, { ok: 'this is fine' }, 'got expected body')
})

t.test('OTP error with prompt that fails', t => {
Expand Down
76 changes: 33 additions & 43 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ t.test('stream body param', t => {
.then(json => t.same(json, { hello: 'world' }))
})

t.test('JSON body param', t => {
t.test('JSON body param', async t => {
tnock(t, defaultOpts.registry)
.matchHeader('content-type', ctype => {
t.equal(ctype[0], 'application/json', 'content-type automatically set')
Expand All @@ -143,10 +143,8 @@ t.test('JSON body param', t => {
body: { hello: 'world' },
gzip: true,
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200, 'request succeeded')
})
const res = await fetch('/hello', opts)
t.equal(res.status, 200, 'request succeeded')
})

t.test('gzip + buffer body param', t => {
Expand Down Expand Up @@ -275,43 +273,41 @@ t.test('query string with ?write=true', t => {
.then(res => t.strictSame(res, { write: 'go for it' }))
})

t.test('fetch.json.stream()', t => {
t.test('fetch.json.stream()', async t => {
tnock(t, defaultOpts.registry).get('/hello').reply(200, {
a: 1,
b: 2,
c: 3,
})
return fetch.json.stream('/hello', '$*', OPTS).collect().then(data => {
t.same(data, [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'c', value: 3 },
], 'got a streamed JSON body')
})
const data = await fetch.json.stream('/hello', '$*', OPTS).collect()
t.same(data, [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'c', value: 3 },
], 'got a streamed JSON body')
})

t.test('fetch.json.stream opts.mapJSON', t => {
t.test('fetch.json.stream opts.mapJSON', async t => {
tnock(t, defaultOpts.registry).get('/hello').reply(200, {
a: 1,
b: 2,
c: 3,
})
return fetch.json.stream('/hello', '*', {
const data = await fetch.json.stream('/hello', '*', {
...OPTS,
mapJSON (value, [key]) {
return [key, value]
},
}).collect().then(data => {
t.same(data, [
['a', 1],
['b', 2],
['c', 3],
], 'data mapped')
})
}).collect()
t.same(data, [
['a', 1],
['b', 2],
['c', 3],
], 'data mapped')
})

t.test('fetch.json.stream gets fetch error on stream', t => {
return t.rejects(fetch.json.stream('/hello', '*', {
t.test('fetch.json.stream gets fetch error on stream', async t => {
await t.rejects(fetch.json.stream('/hello', '*', {
...OPTS,
body: Promise.reject(new Error('no body for you')),
method: 'POST',
Expand All @@ -321,28 +317,24 @@ t.test('fetch.json.stream gets fetch error on stream', t => {
})
})

t.test('opts.ignoreBody', t => {
t.test('opts.ignoreBody', async t => {
tnock(t, defaultOpts.registry)
.get('/hello')
.reply(200, { hello: 'world' })
return fetch('/hello', { ...OPTS, ignoreBody: true })
.then(res => {
t.equal(res.body, null, 'body omitted')
})
const res = await fetch('/hello', { ...OPTS, ignoreBody: true })
t.equal(res.body, null, 'body omitted')
})

t.test('method configurable', t => {
t.test('method configurable', async t => {
tnock(t, defaultOpts.registry)
.delete('/hello')
.reply(200)
const opts = {
...OPTS,
method: 'DELETE',
}
return fetch('/hello', opts)
.then(res => {
t.equal(res.status, 200, 'successfully used DELETE method')
})
const res = await fetch('/hello', opts)
t.equal(res.status, 200, 'successfully used DELETE method')
})

t.test('npm-notice header logging', async t => {
Expand All @@ -355,7 +347,7 @@ t.test('npm-notice header logging', async t => {
let header, msg
process.on('log', (level, ...args) => {
if (level === 'notice') {
;[header, msg] = args
[header, msg] = args
}
})

Expand Down Expand Up @@ -463,7 +455,7 @@ t.test('pickRegistry through opts.spec', t => {
))
})

t.test('miscellaneous headers', t => {
t.test('miscellaneous headers', async t => {
tnock(t, defaultOpts.registry)
.matchHeader('npm-session', session =>
t.strictSame(session, ['foobarbaz'], 'session set from options'))
Expand All @@ -478,30 +470,28 @@ t.test('miscellaneous headers', t => {
.get('/hello')
.reply(200, { hello: 'world' })

return fetch('/hello', {
const res = await fetch('/hello', {
...OPTS,
registry: null, // always falls back on falsey registry value
npmSession: 'foobarbaz',
scope: '@foo',
userAgent: 'agent of use',
npmCommand: 'hello-world',
authType: 'auth',
}).then(res => {
t.equal(res.status, 200, 'got successful response')
})
t.equal(res.status, 200, 'got successful response')
})

t.test('miscellaneous headers not being set if not present in options', t => {
t.test('miscellaneous headers not being set if not present in options', async t => {
tnock(t, defaultOpts.registry)
.matchHeader('npm-auth-type', authType =>
t.strictSame(authType, undefined, 'auth-type not set from options'))
.get('/hello')
.reply(200, { hello: 'world' })

return fetch('/hello', {
const res = await fetch('/hello', {
...OPTS,
authType: undefined,
}).then(res => {
t.equal(res.status, 200, 'got successful response')
})
t.equal(res.status, 200, 'got successful response')
})

0 comments on commit c9a8727

Please sign in to comment.