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