|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const events = require('events') |
| 4 | +const nock = require('nock') |
| 5 | +const ssri = require('ssri') |
| 6 | +const t = require('tap') |
| 7 | + |
| 8 | +const fetch = require('../lib/index.js') |
| 9 | + |
| 10 | +const CONTENT = Buffer.from('hello, world!', { encoding: 'utf8' }) |
| 11 | +const HOST = 'https://make-fetch-happen.npm' |
| 12 | + |
| 13 | +nock.disableNetConnect() |
| 14 | +t.beforeEach(() => { |
| 15 | + nock.cleanAll() |
| 16 | +}) |
| 17 | + |
| 18 | +t.test('emits integrity and size events', t => { |
| 19 | + t.test('when response is cacheable', async t => { |
| 20 | + const INTEGRITY = ssri.fromData(CONTENT) |
| 21 | + const CACHE = t.testdir() |
| 22 | + const srv = nock(HOST) |
| 23 | + .get('/test') |
| 24 | + .reply(200, CONTENT) |
| 25 | + |
| 26 | + const res = await fetch(`${HOST}/test`, { cachePath: CACHE }) |
| 27 | + t.equal(res.status, 200, 'successful status code') |
| 28 | + t.equal(res.headers.get('x-local-cache-status'), 'miss', 'is a cache miss') |
| 29 | + t.equal(res.body.hasIntegrityEmitter, true, 'flag is set on body') |
| 30 | + const gotIntegrity = events.once(res.body, 'integrity').then(i => i[0]) |
| 31 | + const gotSize = events.once(res.body, 'size').then(s => s[0]) |
| 32 | + const [integrity, size, buf] = await Promise.all([gotIntegrity, gotSize, res.buffer()]) |
| 33 | + t.same(buf, CONTENT, 'request succeeded') |
| 34 | + t.same(integrity, INTEGRITY, 'got the right integrity') |
| 35 | + t.same(size, CONTENT.byteLength, 'got the right size') |
| 36 | + t.ok(srv.isDone()) |
| 37 | + }) |
| 38 | + |
| 39 | + t.test('when expected integrity is provided', async t => { |
| 40 | + const INTEGRITY = ssri.fromData(CONTENT) |
| 41 | + const srv = nock(HOST) |
| 42 | + .get('/test') |
| 43 | + .reply(200, CONTENT) |
| 44 | + |
| 45 | + const res = await fetch(`${HOST}/test`, { integrity: INTEGRITY }) |
| 46 | + t.equal(res.status, 200, 'successful status code') |
| 47 | + t.notOk(res.headers.has('x-local-cache-status'), 'should not touch the cache') |
| 48 | + t.equal(res.body.hasIntegrityEmitter, true, 'flag is set on body') |
| 49 | + const gotIntegrity = events.once(res.body, 'integrity').then(i => i[0]) |
| 50 | + const gotSize = events.once(res.body, 'size').then(s => s[0]) |
| 51 | + const [integrity, size, buf] = await Promise.all([gotIntegrity, gotSize, res.buffer()]) |
| 52 | + t.same(buf, CONTENT, 'request succeeded') |
| 53 | + t.same(integrity, INTEGRITY, 'got the right integrity') |
| 54 | + t.same(size, CONTENT.byteLength, 'got the right size') |
| 55 | + t.ok(srv.isDone()) |
| 56 | + }) |
| 57 | + |
| 58 | + t.test('when both expected integrity is provided and response is cacheable', async t => { |
| 59 | + const INTEGRITY = ssri.fromData(CONTENT) |
| 60 | + const CACHE = t.testdir() |
| 61 | + const srv = nock(HOST) |
| 62 | + .get('/test') |
| 63 | + .reply(200, CONTENT) |
| 64 | + |
| 65 | + const res = await fetch(`${HOST}/test`, { cachePath: CACHE, integrity: INTEGRITY }) |
| 66 | + t.equal(res.status, 200, 'successful status code') |
| 67 | + t.equal(res.headers.get('x-local-cache-status'), 'miss', 'is a cache miss') |
| 68 | + t.equal(res.body.hasIntegrityEmitter, true, 'flag is set on body') |
| 69 | + const gotIntegrity = events.once(res.body, 'integrity').then(i => i[0]) |
| 70 | + const gotSize = events.once(res.body, 'size').then(s => s[0]) |
| 71 | + const [integrity, size, buf] = await Promise.all([gotIntegrity, gotSize, res.buffer()]) |
| 72 | + t.same(buf, CONTENT, 'request succeeded') |
| 73 | + t.same(integrity, INTEGRITY, 'got the right integrity') |
| 74 | + t.same(size, CONTENT.byteLength, 'got the right size') |
| 75 | + t.ok(srv.isDone()) |
| 76 | + }) |
| 77 | + |
| 78 | + t.end() |
| 79 | +}) |
0 commit comments