Skip to content

Commit ff5f990

Browse files
committed
test(check-response): Added missing tests
- Added test coverage for warning header usage cases - Added missing check for silenced errors when reading Responses - Added check for correctly logging x-fetch-attempts header value
1 parent 49059f0 commit ff5f990

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

test/check-response.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const { Readable } = require('stream')
2+
const test = require('tap').test
3+
4+
const config = require('../config.js')
5+
const checkResponse = require('../check-response.js')
6+
const errors = require('./errors.js')
7+
const silentLog = require('../silentlog.js')
8+
9+
class Body extends Readable {
10+
_read () { return '' }
11+
}
12+
class Headers {
13+
has () {}
14+
get () {}
15+
raw () {}
16+
}
17+
const mockFetchRes = {
18+
body: new Body(),
19+
buffer: () => Promise.resolve(),
20+
headers: new Headers(),
21+
status: 200
22+
}
23+
24+
test('any response error should be silent', t => {
25+
const res = Object.assign({}, mockFetchRes, {
26+
buffer: () => Promise.reject(new Error('ERR')),
27+
status: 400
28+
})
29+
t.rejects(checkResponse('get', res, 'registry', Date.now(), { ignoreBody: true }), errors.HttpErrorGeneral)
30+
t.done()
31+
})
32+
33+
test('log x-fetch-attempts header value', t => {
34+
const headers = new Headers()
35+
headers.get = header => header === 'x-fetch-attempts' ? 3 : undefined
36+
const res = Object.assign({}, mockFetchRes, {
37+
headers,
38+
status: 400
39+
})
40+
t.rejects(checkResponse('get', res, 'registry', Date.now(), config({
41+
log: Object.assign({}, silentLog, {
42+
http (header, msg) {
43+
t.ok(msg.endsWith('attempt #3'), 'should log correct number of attempts')
44+
}
45+
})
46+
})))
47+
t.plan(2)
48+
})
49+
50+
test('bad-formatted warning headers', t => {
51+
const headers = new Headers()
52+
headers.has = header => header === 'warning' ? 'foo' : undefined
53+
headers.raw = () => ({
54+
warning: ['100 - foo']
55+
})
56+
const res = Object.assign({}, mockFetchRes, {
57+
headers
58+
})
59+
t.ok(checkResponse('get', res, 'registry', Date.now(), config({
60+
log: Object.assign({}, silentLog, {
61+
warn (header, msg) {
62+
t.fail('should not log warnings')
63+
}
64+
})
65+
})))
66+
t.plan(1)
67+
})

test/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,23 @@ test('pickRegistry through opts.spec', t => {
411411
))
412412
})
413413

414+
test('log warning header info', t => {
415+
tnock(t, OPTS.registry)
416+
.get('/hello')
417+
.reply(200, {hello: 'world'}, { Warning: '199 - "ENOTFOUND" "Wed, 21 Oct 2015 07:28:00 GMT"' })
418+
const opts = OPTS.concat({
419+
log: Object.assign({}, silentLog, {
420+
warn (header, msg) {
421+
t.equal(header, 'registry', 'expected warn log header')
422+
t.equal(msg, `Using stale data from ${OPTS.registry} because the host is inaccessible -- are you offline?`, 'logged out at WARNING level')
423+
}
424+
})
425+
})
426+
t.plan(3)
427+
return fetch('/hello', opts)
428+
.then(res => t.equal(res.status, 200, 'got successful response'))
429+
})
430+
414431
// TODO
415432
// * npm-session
416433
// * npm-in-ci

0 commit comments

Comments
 (0)