11import { HttpRequest } from "@smithy/protocol-http" ;
22import { 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" ;
44
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+ } ) ;
614
715// TODO(vitest): fix this test.
8- describe . skip ( FetchHttpHandler . name , ( ) => {
16+ describe ( FetchHttpHandler . name , ( ) => {
917 interface MockHttpRequestOptions {
1018 method ?: string ;
1119 body ?: any ;
@@ -19,52 +27,53 @@ describe.skip(FetchHttpHandler.name, () => {
1927 new HttpRequest ( { hostname : "example.com" , ...options } ) ;
2028
2129 describe ( "fetch" , ( ) => {
30+ beforeAll ( ( ) => {
31+ keepAliveSupport . supported = true ;
32+ } ) ;
33+
2234 afterEach ( ( ) => {
2335 vi . clearAllMocks ( ) ;
2436 } ) ;
2537
2638 it ( "sends basic fetch request" , async ( ) => {
2739 const fetchHttpHandler = new FetchHttpHandler ( ) ;
28- const winReqSpy = vi . spyOn ( window , "Request" ) ;
2940
3041 const mockHttpRequest = getMockHttpRequest ( { } ) ;
3142 await fetchHttpHandler . handle ( mockHttpRequest ) ;
3243
3344 const expectedUrl = `${ mockHttpRequest . protocol } //${ mockHttpRequest . hostname } /` ;
34- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
45+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
46+
3547 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 ) ;
3850 } ) ;
3951
4052 for ( const method of [ "GET" , "HEAD" ] ) {
4153 it ( `sets body to undefined when method: '${ method } '` , async ( ) => {
4254 const fetchHttpHandler = new FetchHttpHandler ( ) ;
43- const winReqSpy = vi . spyOn ( window , "Request" ) ;
4455
4556 const mockHttpRequest = getMockHttpRequest ( { method, body : "test" } ) ;
4657 await fetchHttpHandler . handle ( mockHttpRequest ) ;
4758
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 ) ;
5162 } ) ;
5263 }
5364
5465 it ( `sets keepalive to true if explicitly requested` , async ( ) => {
5566 const fetchHttpHandler = new FetchHttpHandler ( { keepAlive : true } ) ;
56- const winReqSpy = vi . spyOn ( window , "Request" ) ;
5767
5868 const mockHttpRequest = getMockHttpRequest ( { } ) ;
5969 await fetchHttpHandler . handle ( mockHttpRequest ) ;
6070
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 ) ;
6373 } ) ;
6474
6575 it ( `builds querystring if provided` , async ( ) => {
6676 const fetchHttpHandler = new FetchHttpHandler ( ) ;
67- const winReqSpy = vi . spyOn ( window , "Request" ) ;
6877
6978 const query = { foo : "bar" } ;
7079 const fragment = "test" ;
@@ -74,22 +83,23 @@ describe.skip(FetchHttpHandler.name, () => {
7483 const expectedUrl = `${ mockHttpRequest . protocol } //${ mockHttpRequest . hostname } /?${ Object . entries ( query )
7584 . map ( ( [ key , val ] ) => `${ key } =${ val } ` )
7685 . join ( "&" ) } #${ fragment } `;
77- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
86+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
7887 expect ( requestArgs [ 0 ] ) . toEqual ( expectedUrl ) ;
7988 } ) ;
8089
8190 it ( `sets auth if username/password are provided` , async ( ) => {
8291 const fetchHttpHandler = new FetchHttpHandler ( ) ;
83- const winReqSpy = vi . spyOn ( window , "Request" ) ;
8492
8593 const username = "foo" ;
8694 const password = "bar" ;
8795 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+ } )
8999
90100 const mockAuth = `${ mockHttpRequest . username } :${ mockHttpRequest . password } ` ;
91101 const expectedUrl = `${ mockHttpRequest . protocol } //${ mockAuth } @${ mockHttpRequest . hostname } /` ;
92- const requestArgs = winReqSpy . mock . calls [ 0 ] [ 0 ] ;
102+ const requestArgs = vi . mocked ( createRequest ) . mock . calls [ 0 ] ;
93103 expect ( requestArgs [ 0 ] ) . toEqual ( expectedUrl ) ;
94104 } ) ;
95105 } ) ;
0 commit comments