/
check-response.js
204 lines (190 loc) · 4.66 KB
/
check-response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const { Readable } = require('stream')
const t = require('tap')
const checkResponse = require('../lib/check-response.js')
const errors = require('../lib/errors.js')
const registry = 'registry'
const startTime = Date.now()
class Body extends Readable {
_read () {
return ''
}
}
class Headers {
has () {}
get () {}
raw () {}
}
const mockFetchRes = {
body: new Body(),
buffer: () => Promise.resolve(),
headers: new Headers(),
status: 200,
}
t.test('any response error should be silent', t => {
const res = Object.assign({}, mockFetchRes, {
buffer: () => Promise.reject(new Error('ERR')),
status: 400,
url: 'https://example.com/',
})
t.rejects(checkResponse({
method: 'get',
res,
registry,
startTime,
opts: { ignoreBody: true },
}), errors.HttpErrorGeneral)
t.end()
})
t.test('all checks are ok, nothing to report', t => {
const res = Object.assign({}, mockFetchRes, {
buffer: () => Promise.resolve(Buffer.from('ok')),
status: 400,
url: 'https://example.com/',
})
t.rejects(checkResponse({
method: 'get',
res,
registry,
startTime,
}), errors.HttpErrorGeneral)
t.end()
})
t.test('log x-fetch-attempts header value', async t => {
const headers = new Headers()
headers.get = header => header === 'x-fetch-attempts' ? 3 : undefined
const res = Object.assign({}, mockFetchRes, {
headers,
status: 400,
})
t.plan(2)
let msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[, msg] = args
}
})
await t.rejects(checkResponse({
method: 'get',
res,
registry,
startTime,
}))
t.ok(msg.endsWith('attempt #3'), 'should log correct number of attempts')
})
t.test('log the url fetched', t => {
const headers = new Headers()
const EE = require('events')
headers.get = header => undefined
const res = Object.assign({}, mockFetchRes, {
headers,
status: 200,
url: 'http://example.com/foo/bar/baz',
body: new EE(),
})
t.plan(2)
let header, msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[header, msg] = args
}
})
checkResponse({
method: 'get',
res,
registry,
startTime,
})
res.body.emit('end')
t.equal(header, 'fetch')
t.match(msg, /^GET 200 http:\/\/example.com\/foo\/bar\/baz [0-9]+m?s/)
})
t.test('redact password from log', t => {
const headers = new Headers()
const EE = require('events')
headers.get = header => undefined
const res = Object.assign({}, mockFetchRes, {
headers,
status: 200,
url: 'http://username:password@example.com/foo/bar/baz',
body: new EE(),
})
t.plan(2)
let header, msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[header, msg] = args
}
})
checkResponse({
method: 'get',
res,
registry,
startTime,
})
res.body.emit('end')
t.equal(header, 'fetch')
t.match(msg, /^GET 200 http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s/)
})
/* eslint-disable-next-line max-len */
const moreInfoUrl = 'https://github.com/npm/cli/wiki/No-auth-for-URI,-but-auth-present-for-scoped-registry'
t.test('report auth for registry, but not for this request', async t => {
const res = Object.assign({}, mockFetchRes, {
buffer: () => Promise.resolve(Buffer.from('ok')),
status: 400,
url: 'https://example.com/',
})
t.plan(3)
let header, msg
process.on('log', (level, ...args) => {
if (level === 'warn') {
;[header, msg] = args
}
})
await t.rejects(checkResponse({
method: 'get',
res,
uri: 'https://example.com/',
registry,
startTime,
auth: {
scopeAuthKey: '//some-scope-registry.com/',
auth: null,
token: null,
},
}), errors.HttpErrorGeneral)
t.equal(header, 'registry')
t.equal(msg, `No auth for URI, but auth present for scoped registry.
URI: https://example.com/
Scoped Registry Key: //some-scope-registry.com/
More info here: ${moreInfoUrl}`)
})
t.test('logs the value of x-local-cache-status when set', t => {
const headers = new Headers()
const EE = require('events')
headers.get = header => header === 'x-local-cache-status' ? 'hit' : undefined
const res = Object.assign({}, mockFetchRes, {
headers,
status: 200,
url: 'http://username:password@example.com/foo/bar/baz',
body: new EE(),
})
t.plan(2)
let header, msg
process.on('log', (level, ...args) => {
if (level === 'http') {
;[header, msg] = args
}
})
checkResponse({
method: 'get',
res,
registry,
startTime,
})
res.body.emit('end')
t.equal(header, 'fetch')
t.match(
msg,
/^GET 200 http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s \(cache hit\)$/
)
})