1
1
import { IncomingMessage } from 'node:http'
2
+ import type { ServerHttp2Stream } from 'node:http2'
3
+ import { Http2ServerRequest } from 'node:http2'
2
4
import { Socket } from 'node:net'
5
+ import { Duplex } from 'node:stream'
3
6
import {
4
7
newRequest ,
5
8
Request as LightweightRequest ,
@@ -130,7 +133,7 @@ describe('Request', () => {
130
133
} ) . toThrow ( RequestError )
131
134
} )
132
135
133
- it ( 'Should be create request body from `req.rawBody` if it exists' , async ( ) => {
136
+ it ( 'Should be created request body from `req.rawBody` if it exists' , async ( ) => {
134
137
const rawBody = Buffer . from ( 'foo' )
135
138
const socket = new Socket ( )
136
139
const incomingMessage = new IncomingMessage ( socket )
@@ -152,6 +155,110 @@ describe('Request', () => {
152
155
const text = await req . text ( )
153
156
expect ( text ) . toBe ( 'foo' )
154
157
} )
158
+
159
+ describe ( 'absolute-form for request-target' , ( ) => {
160
+ it ( 'should be created from valid absolute URL' , async ( ) => {
161
+ const req = newRequest ( {
162
+ url : 'http://localhost/path/to/file.html' ,
163
+ } as IncomingMessage )
164
+ expect ( req ) . toBeInstanceOf ( GlobalRequest )
165
+ expect ( req . url ) . toBe ( 'http://localhost/path/to/file.html' )
166
+ } )
167
+
168
+ it ( 'should throw error if host header is invalid' , async ( ) => {
169
+ expect ( ( ) => {
170
+ newRequest ( {
171
+ url : 'http://' ,
172
+ } as IncomingMessage )
173
+ } ) . toThrow ( RequestError )
174
+ } )
175
+
176
+ it ( 'should throw error if absolute-form is specified via HTTP/2' , async ( ) => {
177
+ expect ( ( ) => {
178
+ newRequest (
179
+ new Http2ServerRequest (
180
+ new Duplex ( ) as ServerHttp2Stream ,
181
+ {
182
+ ':scheme' : 'http' ,
183
+ ':authority' : 'localhost' ,
184
+ ':path' : 'http://localhost/foo.txt' ,
185
+ } ,
186
+ { } ,
187
+ [ ]
188
+ )
189
+ )
190
+ } ) . toThrow ( RequestError )
191
+ } )
192
+ } )
193
+
194
+ describe ( 'HTTP/2' , ( ) => {
195
+ it ( 'should be created from "http" scheme' , async ( ) => {
196
+ const req = newRequest (
197
+ new Http2ServerRequest (
198
+ new Duplex ( ) as ServerHttp2Stream ,
199
+ {
200
+ ':scheme' : 'http' ,
201
+ ':authority' : 'localhost' ,
202
+ ':path' : '/foo.txt' ,
203
+ } ,
204
+ { } ,
205
+ [ ]
206
+ )
207
+ )
208
+ expect ( req ) . toBeInstanceOf ( GlobalRequest )
209
+ expect ( req . url ) . toBe ( 'http://localhost/foo.txt' )
210
+ } )
211
+
212
+ it ( 'should be created from "https" scheme' , async ( ) => {
213
+ const req = newRequest (
214
+ new Http2ServerRequest (
215
+ new Duplex ( ) as ServerHttp2Stream ,
216
+ {
217
+ ':scheme' : 'https' ,
218
+ ':authority' : 'localhost' ,
219
+ ':path' : '/foo.txt' ,
220
+ } ,
221
+ { } ,
222
+ [ ]
223
+ )
224
+ )
225
+ expect ( req ) . toBeInstanceOf ( GlobalRequest )
226
+ expect ( req . url ) . toBe ( 'https://localhost/foo.txt' )
227
+ } )
228
+
229
+ it ( 'should throw error if scheme is missing' , async ( ) => {
230
+ expect ( ( ) => {
231
+ newRequest (
232
+ new Http2ServerRequest (
233
+ new Duplex ( ) as ServerHttp2Stream ,
234
+ {
235
+ ':authority' : 'localhost' ,
236
+ ':path' : '/foo.txt' ,
237
+ } ,
238
+ { } ,
239
+ [ ]
240
+ )
241
+ )
242
+ } ) . toThrow ( RequestError )
243
+ } )
244
+
245
+ it ( 'should throw error if unsupported scheme is specified' , async ( ) => {
246
+ expect ( ( ) => {
247
+ newRequest (
248
+ new Http2ServerRequest (
249
+ new Duplex ( ) as ServerHttp2Stream ,
250
+ {
251
+ ':scheme' : 'ftp' ,
252
+ ':authority' : 'localhost' ,
253
+ ':path' : '/foo.txt' ,
254
+ } ,
255
+ { } ,
256
+ [ ]
257
+ )
258
+ )
259
+ } ) . toThrow ( RequestError )
260
+ } )
261
+ } )
155
262
} )
156
263
157
264
describe ( 'GlobalRequest' , ( ) => {
0 commit comments