1
1
'use strict'
2
2
3
- const { test } = require ( 'tap' )
3
+ const { test } = require ( 'node:test' )
4
+ const assert = require ( 'node:assert' )
4
5
const { createServer } = require ( 'http' )
5
6
const { createHash, getHashes } = require ( 'crypto' )
6
7
const { gzipSync } = require ( 'zlib' )
@@ -14,49 +15,46 @@ setGlobalDispatcher(new Agent({
14
15
keepAliveMaxTimeout : 1
15
16
} ) )
16
17
17
- test ( 'request with correct integrity checksum' , ( t ) => {
18
+ test ( 'request with correct integrity checksum' , ( t , done ) => {
18
19
const body = 'Hello world!'
19
20
const hash = createHash ( 'sha256' ) . update ( body ) . digest ( 'base64' )
20
21
21
22
const server = createServer ( ( req , res ) => {
22
23
res . end ( body )
23
24
} )
24
25
25
- t . teardown ( server . close . bind ( server ) )
26
+ t . after ( server . close . bind ( server ) )
26
27
27
28
server . listen ( 0 , async ( ) => {
28
29
const response = await fetch ( `http://localhost:${ server . address ( ) . port } ` , {
29
30
integrity : `sha256-${ hash } `
30
31
} )
31
- t . strictSame ( body , await response . text ( ) )
32
- t . end ( )
32
+ assert . strictEqual ( body , await response . text ( ) )
33
+ done ( )
33
34
} )
34
35
} )
35
36
36
- test ( 'request with wrong integrity checksum' , ( t ) => {
37
+ test ( 'request with wrong integrity checksum' , async ( t ) => {
37
38
const body = 'Hello world!'
38
39
const hash = 'c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51b'
39
40
40
41
const server = createServer ( ( req , res ) => {
41
42
res . end ( body )
42
- } )
43
+ } ) . listen ( 0 )
43
44
44
- t . teardown ( server . close . bind ( server ) )
45
+ t . after ( server . close . bind ( server ) )
46
+ await once ( server , 'listening' )
45
47
46
- server . listen ( 0 , ( ) => {
47
- fetch ( `http://localhost:${ server . address ( ) . port } ` , {
48
- integrity : `sha256-${ hash } `
49
- } ) . then ( response => {
50
- t . pass ( 'request did not fail' )
51
- } ) . catch ( ( err ) => {
52
- t . equal ( err . cause . message , 'integrity mismatch' )
53
- } ) . finally ( ( ) => {
54
- t . end ( )
55
- } )
48
+ const expectedError = new TypeError ( 'fetch failed' , {
49
+ cause : new Error ( 'integrity mismatch' )
56
50
} )
51
+
52
+ await assert . rejects ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
53
+ integrity : `sha256-${ hash } `
54
+ } ) , expectedError )
57
55
} )
58
56
59
- test ( 'request with integrity checksum on encoded body' , ( t ) => {
57
+ test ( 'request with integrity checksum on encoded body' , ( t , done ) => {
60
58
const body = 'Hello world!'
61
59
const hash = createHash ( 'sha256' ) . update ( body ) . digest ( 'base64' )
62
60
@@ -65,14 +63,14 @@ test('request with integrity checksum on encoded body', (t) => {
65
63
res . end ( gzipSync ( body ) )
66
64
} )
67
65
68
- t . teardown ( server . close . bind ( server ) )
66
+ t . after ( server . close . bind ( server ) )
69
67
70
68
server . listen ( 0 , async ( ) => {
71
69
const response = await fetch ( `http://localhost:${ server . address ( ) . port } ` , {
72
70
integrity : `sha256-${ hash } `
73
71
} )
74
- t . strictSame ( body , await response . text ( ) )
75
- t . end ( )
72
+ assert . strictEqual ( body , await response . text ( ) )
73
+ done ( )
76
74
} )
77
75
} )
78
76
@@ -81,10 +79,10 @@ test('request with a totally incorrect integrity', async (t) => {
81
79
res . end ( )
82
80
} ) . listen ( 0 )
83
81
84
- t . teardown ( server . close . bind ( server ) )
82
+ t . after ( server . close . bind ( server ) )
85
83
await once ( server , 'listening' )
86
84
87
- await t . resolves ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
85
+ await assert . doesNotReject ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
88
86
integrity : 'what-integrityisthis'
89
87
} ) )
90
88
} )
@@ -97,10 +95,10 @@ test('request with mixed in/valid integrities', async (t) => {
97
95
res . end ( body )
98
96
} ) . listen ( 0 )
99
97
100
- t . teardown ( server . close . bind ( server ) )
98
+ t . after ( server . close . bind ( server ) )
101
99
await once ( server , 'listening' )
102
100
103
- await t . resolves ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
101
+ await assert . doesNotReject ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
104
102
integrity : `invalid-integrity sha256-${ hash } `
105
103
} ) )
106
104
} )
@@ -113,16 +111,16 @@ test('request with sha384 hash', { skip: !supportedHashes.includes('sha384') },
113
111
res . end ( body )
114
112
} ) . listen ( 0 )
115
113
116
- t . teardown ( server . close . bind ( server ) )
114
+ t . after ( server . close . bind ( server ) )
117
115
await once ( server , 'listening' )
118
116
119
117
// request should succeed
120
- await t . resolves ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
118
+ await assert . doesNotReject ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
121
119
integrity : `sha384-${ hash } `
122
120
} ) )
123
121
124
122
// request should fail
125
- await t . rejects ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
123
+ await assert . rejects ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
126
124
integrity : 'sha384-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
127
125
} ) )
128
126
} )
@@ -135,16 +133,16 @@ test('request with sha512 hash', { skip: !supportedHashes.includes('sha512') },
135
133
res . end ( body )
136
134
} ) . listen ( 0 )
137
135
138
- t . teardown ( server . close . bind ( server ) )
136
+ t . after ( server . close . bind ( server ) )
139
137
await once ( server , 'listening' )
140
138
141
139
// request should succeed
142
- await t . resolves ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
140
+ await assert . doesNotReject ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
143
141
integrity : `sha512-${ hash } `
144
142
} ) )
145
143
146
144
// request should fail
147
- await t . rejects ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
145
+ await assert . rejects ( fetch ( `http://localhost:${ server . address ( ) . port } ` , {
148
146
integrity : 'sha512-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs='
149
147
} ) )
150
148
} )
0 commit comments