-
Notifications
You must be signed in to change notification settings - Fork 500
/
integrity.js
52 lines (41 loc) · 1.23 KB
/
integrity.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
'use strict'
const { test } = require('tap')
const { createServer } = require('http')
const { fetch } = require('../..')
test('request with correct integrity checksum', (t) => {
t.plan(1)
const payload = {
body: 'Hello world!',
checksum: 'sha256-c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51a'
}
const server = createServer((req, res) => {
res.end(payload.body)
})
t.teardown(server.close.bind(server))
server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}`, {
integrity: payload.checksum
})
t.strictSame(payload.body, await body.text())
})
})
test('request with wrong integrity checksum', (t) => {
t.plan(1)
const payload = {
body: 'Hello world!',
checksum: 'sha256-c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51b'
}
const server = createServer((req, res) => {
res.end(payload.body)
})
t.teardown(server.close.bind(server))
server.listen(0, () => {
fetch(`http://localhost:${server.address().port}`, {
integrity: payload.checksum
}).then(response => {
t.fail('fetch did not fail')
}).catch((err) => {
t.equal(err.cause.message, 'integrity mismatch')
})
})
})