1- import fetch from ' ../src/index.mjs' ;
2- import fetchDist from '..' ;
1+ import fetch from " ../src/index.mjs" ;
2+ import fetchDist from ".." ;
33
4- describe ( ' unfetch' , ( ) => {
5- it ( ' should be a function' , ( ) => {
4+ describe ( " unfetch" , ( ) => {
5+ it ( " should be a function" , ( ) => {
66 expect ( fetch ) . toEqual ( expect . any ( Function ) ) ;
77 } ) ;
88
9- it ( ' should be compiled correctly' , ( ) => {
9+ it ( " should be compiled correctly" , ( ) => {
1010 expect ( fetchDist ) . toEqual ( expect . any ( Function ) ) ;
1111 expect ( fetchDist ) . toHaveLength ( 2 ) ;
1212 } ) ;
1313
14- describe ( ' fetch()' , ( ) => {
14+ describe ( " fetch()" , ( ) => {
1515 let xhr ;
1616
1717 beforeEach ( ( ) => {
1818 xhr = {
1919 setRequestHeader : jest . fn ( ) ,
20- getAllResponseHeaders : jest . fn ( ) . mockReturnValue ( 'X-Foo: bar\nX-Foo:baz' ) ,
20+ getAllResponseHeaders : jest
21+ . fn ( )
22+ . mockReturnValue (
23+ "X-Foo: bar\r\nX-Foo: baz\r\nX-Bar: bar, baz\r\nx-test: \r\nX-Baz: bar, baz\r\ndate: 18:23:22"
24+ ) ,
25+ getResponseHeader : jest . fn ( ) . mockReturnValue ( "bar, baz" ) ,
2126 open : jest . fn ( ) ,
2227 send : jest . fn ( ) ,
2328 status : 200 ,
24- statusText : 'OK' ,
29+ statusText : "OK" ,
2530 responseText : '{"a":"b"}' ,
26- responseURL : ' /foo?redirect'
31+ responseURL : " /foo?redirect" ,
2732 } ;
2833
2934 global . XMLHttpRequest = jest . fn ( ( ) => xhr ) ;
@@ -33,29 +38,36 @@ describe('unfetch', () => {
3338 delete global . XMLHttpRequest ;
3439 } ) ;
3540
36- it ( ' sanity test' , ( ) => {
37- let p = fetch ( ' /foo' , { headers : { a : 'b' } } )
38- . then ( r => {
41+ it ( " sanity test" , ( ) => {
42+ let p = fetch ( " /foo" , { headers : { a : "b" } } )
43+ . then ( ( r ) => {
3944 expect ( r ) . toMatchObject ( {
4045 text : expect . any ( Function ) ,
4146 json : expect . any ( Function ) ,
4247 blob : expect . any ( Function ) ,
4348 clone : expect . any ( Function ) ,
44- headers : expect . any ( Object )
49+ headers : expect . any ( Object ) ,
4550 } ) ;
4651 expect ( r . clone ( ) ) . not . toBe ( r ) ;
47- expect ( r . clone ( ) . url ) . toEqual ( ' /foo?redirect' ) ;
52+ expect ( r . clone ( ) . url ) . toEqual ( " /foo?redirect" ) ;
4853 expect ( r . headers . get ) . toEqual ( expect . any ( Function ) ) ;
49- expect ( r . headers . get ( 'x-foo' ) ) . toEqual ( 'bar,baz' ) ;
54+ expect ( r . headers . get ( "x-foo" ) ) . toEqual ( "bar, baz" ) ;
55+ expect ( r . headers . keys ( ) ) . toEqual ( [
56+ "x-foo" ,
57+ "x-bar" ,
58+ "x-test" ,
59+ "x-baz" ,
60+ "date" ,
61+ ] ) ;
5062 return r . json ( ) ;
5163 } )
52- . then ( data => {
53- expect ( data ) . toEqual ( { a : 'b' } ) ;
64+ . then ( ( data ) => {
65+ expect ( data ) . toEqual ( { a : "b" } ) ;
5466
5567 expect ( xhr . setRequestHeader ) . toHaveBeenCalledTimes ( 1 ) ;
56- expect ( xhr . setRequestHeader ) . toHaveBeenCalledWith ( 'a' , 'b' ) ;
68+ expect ( xhr . setRequestHeader ) . toHaveBeenCalledWith ( "a" , "b" ) ;
5769 expect ( xhr . open ) . toHaveBeenCalledTimes ( 1 ) ;
58- expect ( xhr . open ) . toHaveBeenCalledWith ( ' get' , ' /foo' , true ) ;
70+ expect ( xhr . open ) . toHaveBeenCalledWith ( " get" , " /foo" , true ) ;
5971 expect ( xhr . send ) . toHaveBeenCalledTimes ( 1 ) ;
6072 expect ( xhr . send ) . toHaveBeenCalledWith ( null ) ;
6173 } ) ;
@@ -68,13 +80,21 @@ describe('unfetch', () => {
6880 return p ;
6981 } ) ;
7082
71- it ( 'handles empty header values' , ( ) => {
72- xhr . getAllResponseHeaders = jest . fn ( ) . mockReturnValue ( 'Server: \nX-Foo:baz' ) ;
73- let p = fetch ( '/foo' )
74- . then ( r => {
75- expect ( r . headers . get ( 'server' ) ) . toEqual ( '' ) ;
76- expect ( r . headers . get ( 'X-foo' ) ) . toEqual ( 'baz' ) ;
77- } ) ;
83+ it ( "handles empty header values" , ( ) => {
84+ xhr . getAllResponseHeaders = jest
85+ . fn ( )
86+ . mockReturnValue ( "Server: \nX-Foo:baz" ) ;
87+ const headers = {
88+ server : "" ,
89+ "x-foo" : "baz" ,
90+ } ;
91+ xhr . getResponseHeader = jest . fn (
92+ ( header ) => headers [ header . toLowerCase ( ) ] ?? null
93+ ) ;
94+ let p = fetch ( "/foo" ) . then ( ( r ) => {
95+ expect ( r . headers . get ( "server" ) ) . toEqual ( "" ) ;
96+ expect ( r . headers . get ( "X-foo" ) ) . toEqual ( "baz" ) ;
97+ } ) ;
7898
7999 xhr . onload ( ) ;
80100
0 commit comments