Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const { Readable } = require('stream') | ||
const test = require('tap').test | ||
|
||
const config = require('../config.js') | ||
const checkResponse = require('../check-response.js') | ||
const errors = require('./errors.js') | ||
const silentLog = require('../silentlog.js') | ||
|
||
class Body extends Readable { | ||
_read () { return '' } | ||
} | ||
class Headers { | ||
has () {} | ||
get () {} | ||
raw () {} | ||
} | ||
const mockFetchRes = { | ||
body: new Body(), | ||
buffer: () => Promise.resolve(), | ||
headers: new Headers(), | ||
status: 200 | ||
} | ||
|
||
test('any response error should be silent', t => { | ||
const res = Object.assign({}, mockFetchRes, { | ||
buffer: () => Promise.reject(new Error('ERR')), | ||
status: 400 | ||
}) | ||
t.rejects(checkResponse('get', res, 'registry', Date.now(), { ignoreBody: true }), errors.HttpErrorGeneral) | ||
t.done() | ||
}) | ||
|
||
test('log x-fetch-attempts header value', t => { | ||
const headers = new Headers() | ||
headers.get = header => header === 'x-fetch-attempts' ? 3 : undefined | ||
const res = Object.assign({}, mockFetchRes, { | ||
headers, | ||
status: 400 | ||
}) | ||
t.rejects(checkResponse('get', res, 'registry', Date.now(), config({ | ||
log: Object.assign({}, silentLog, { | ||
http (header, msg) { | ||
t.ok(msg.endsWith('attempt #3'), 'should log correct number of attempts') | ||
} | ||
}) | ||
}))) | ||
t.plan(2) | ||
}) | ||
|
||
test('bad-formatted warning headers', t => { | ||
const headers = new Headers() | ||
headers.has = header => header === 'warning' ? 'foo' : undefined | ||
headers.raw = () => ({ | ||
warning: ['100 - foo'] | ||
}) | ||
const res = Object.assign({}, mockFetchRes, { | ||
headers | ||
}) | ||
t.ok(checkResponse('get', res, 'registry', Date.now(), config({ | ||
log: Object.assign({}, silentLog, { | ||
warn (header, msg) { | ||
t.fail('should not log warnings') | ||
} | ||
}) | ||
}))) | ||
t.plan(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters