|
| 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 | +}) |
0 commit comments