1
1
import { HttpRequest } from "@smithy/protocol-http" ;
2
2
import { QueryParameterBag } from "@smithy/types" ;
3
- import { afterEach , describe , expect , test as it , vi } from "vitest" ;
3
+ import { afterEach , beforeAll , describe , expect , test as it , vi } from "vitest" ;
4
4
5
- import { FetchHttpHandler } from "./fetch-http-handler" ;
5
+ import { createRequest } from "./create-request" ;
6
+ import { FetchHttpHandler , keepAliveSupport } from "./fetch-http-handler" ;
6
7
7
- // TODO(vitest): fix this test.
8
- describe . skip ( FetchHttpHandler . name , ( ) => {
8
+ vi . mock ( "./create-request" , async ( ) => {
9
+ const actual : any = await vi . importActual ( "./create-request" ) ;
10
+ return {
11
+ createRequest : vi . fn ( ) . mockImplementation ( actual . createRequest ) ,
12
+ } ;
13
+ } ) ;
14
+
15
+ describe ( FetchHttpHandler . name , ( ) => {
9
16
interface MockHttpRequestOptions {
10
17
method ?: string ;
11
18
body ?: any ;
@@ -19,52 +26,53 @@ describe.skip(FetchHttpHandler.name, () => {
19
26
new HttpRequest ( { hostname : "example.com" , ...options } ) ;
20
27
21
28
describe ( "fetch" , ( ) => {
29
+ beforeAll ( ( ) => {
30
+ keepAliveSupport . supported = true ;
31
+ } ) ;
32
+
22
33
afterEach ( ( ) => {
23
34
vi . clearAllMocks ( ) ;
24
35
} ) ;
25
36
26
37
it ( "sends basic fetch request" , async ( ) => {
27
38
const fetchHttpHandler = new FetchHttpHandler ( ) ;
28
- const winReqSpy = vi . spyOn ( window , "Request" ) ;
29
39
30
40
const mockHttpRequest = getMockHttpRequest ( { } ) ;
31
41
await fetchHttpHandler . handle ( mockHttpRequest ) ;
32
42
33
43
const expectedUrl = `${ mockHttpRequest . protocol } //${ mockHttpRequest . hostname } /` ;
34
- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
44
+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
45
+
35
46
expect ( requestArgs [ 0 ] ) . toEqual ( expectedUrl ) ;
36
- expect ( requestArgs [ 1 ] . method ) . toEqual ( mockHttpRequest . method ) ;
37
- expect ( requestArgs [ 1 ] . keepalive ) . toEqual ( false ) ;
47
+ expect ( requestArgs [ 1 ] ! . method ) . toEqual ( mockHttpRequest . method ) ;
48
+ expect ( requestArgs [ 1 ] ! . keepalive ) . toEqual ( false ) ;
38
49
} ) ;
39
50
40
51
for ( const method of [ "GET" , "HEAD" ] ) {
41
52
it ( `sets body to undefined when method: '${ method } '` , async ( ) => {
42
53
const fetchHttpHandler = new FetchHttpHandler ( ) ;
43
- const winReqSpy = vi . spyOn ( window , "Request" ) ;
44
54
45
55
const mockHttpRequest = getMockHttpRequest ( { method, body : "test" } ) ;
46
56
await fetchHttpHandler . handle ( mockHttpRequest ) ;
47
57
48
- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
49
- expect ( requestArgs [ 1 ] . method ) . toEqual ( mockHttpRequest . method ) ;
50
- expect ( requestArgs [ 1 ] . body ) . toEqual ( undefined ) ;
58
+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
59
+ expect ( requestArgs [ 1 ] ! . method ) . toEqual ( mockHttpRequest . method ) ;
60
+ expect ( requestArgs [ 1 ] ! . body ) . toEqual ( undefined ) ;
51
61
} ) ;
52
62
}
53
63
54
64
it ( `sets keepalive to true if explicitly requested` , async ( ) => {
55
65
const fetchHttpHandler = new FetchHttpHandler ( { keepAlive : true } ) ;
56
- const winReqSpy = vi . spyOn ( window , "Request" ) ;
57
66
58
67
const mockHttpRequest = getMockHttpRequest ( { } ) ;
59
68
await fetchHttpHandler . handle ( mockHttpRequest ) ;
60
69
61
- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
62
- expect ( requestArgs [ 1 ] . keepalive ) . toEqual ( true ) ;
70
+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
71
+ expect ( requestArgs [ 1 ] ! . keepalive ) . toEqual ( true ) ;
63
72
} ) ;
64
73
65
74
it ( `builds querystring if provided` , async ( ) => {
66
75
const fetchHttpHandler = new FetchHttpHandler ( ) ;
67
- const winReqSpy = vi . spyOn ( window , "Request" ) ;
68
76
69
77
const query = { foo : "bar" } ;
70
78
const fragment = "test" ;
@@ -74,22 +82,23 @@ describe.skip(FetchHttpHandler.name, () => {
74
82
const expectedUrl = `${ mockHttpRequest . protocol } //${ mockHttpRequest . hostname } /?${ Object . entries ( query )
75
83
. map ( ( [ key , val ] ) => `${ key } =${ val } ` )
76
84
. join ( "&" ) } #${ fragment } `;
77
- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
85
+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
78
86
expect ( requestArgs [ 0 ] ) . toEqual ( expectedUrl ) ;
79
87
} ) ;
80
88
81
89
it ( `sets auth if username/password are provided` , async ( ) => {
82
90
const fetchHttpHandler = new FetchHttpHandler ( ) ;
83
- const winReqSpy = vi . spyOn ( window , "Request" ) ;
84
91
85
92
const username = "foo" ;
86
93
const password = "bar" ;
87
94
const mockHttpRequest = getMockHttpRequest ( { username, password } ) ;
88
- await fetchHttpHandler . handle ( mockHttpRequest ) ;
95
+ await fetchHttpHandler . handle ( mockHttpRequest ) . catch ( error => {
96
+ expect ( String ( error ) ) . toContain ( "TypeError: Request cannot be constructed from a URL that includes credentials" ) ;
97
+ } )
89
98
90
99
const mockAuth = `${ mockHttpRequest . username } :${ mockHttpRequest . password } ` ;
91
100
const expectedUrl = `${ mockHttpRequest . protocol } //${ mockAuth } @${ mockHttpRequest . hostname } /` ;
92
- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
101
+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
93
102
expect ( requestArgs [ 0 ] ) . toEqual ( expectedUrl ) ;
94
103
} ) ;
95
104
} ) ;
0 commit comments