@@ -4,123 +4,101 @@ import { describe, expect, test } from 'vitest'
44
55const ctx = setupMockServer ( )
66
7+ const headerFoo = {
8+ name : `foo` ,
9+ value : `bar` ,
10+ }
11+ const headers = new Headers ( { [ headerFoo . name ] : headerFoo . value } )
12+
13+ describe ( `request()` , ( ) => {
14+ test ( `can set headers` , async ( ) => {
15+ const mock = ctx . res ( )
16+ await request ( ctx . url , `{ me { id } }` , { } , headers )
17+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( headerFoo . value )
18+ } )
19+ } )
20+
721describe ( `using class` , ( ) => {
8- test ( `.setHeader() sets a header that get sent to server ` , async ( ) => {
22+ test ( `.setHeader() sets a header` , async ( ) => {
923 const client = new GraphQLClient ( ctx . url )
10- client . setHeader ( `x-foo` , `bar` )
24+ client . setHeader ( headerFoo . name , headerFoo . value )
1125 const mock = ctx . res ( )
1226 await client . request ( `{ me { id } }` )
13- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `bar` )
27+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( headerFoo . value )
1428 } )
15-
16- describe ( `.setHeaders() sets headers that get sent to the server` , ( ) => {
17- test ( `with headers instance` , async ( ) => {
18- const client = new GraphQLClient ( ctx . url )
19- client . setHeaders ( new Headers ( { 'x-foo' : `bar` } ) )
20- const mock = ctx . res ( )
21- await client . request ( `{ me { id } }` )
22- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `bar` )
23- } )
24- test ( `with headers object` , async ( ) => {
25- const client = new GraphQLClient ( ctx . url )
26- client . setHeaders ( { 'x-foo' : `bar` } )
27- const mock = ctx . res ( )
28- await client . request ( `{ me { id } }` )
29- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `bar` )
30- } )
31- test ( `with header tuples` , async ( ) => {
32- const client = new GraphQLClient ( ctx . url )
33- client . setHeaders ( [ [ `x-foo` , `bar` ] ] )
34- const mock = ctx . res ( )
35- await client . request ( `{ me { id } }` )
36- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `bar` )
37- } )
29+ test ( `.setHeaders() sets headers` , async ( ) => {
30+ const client = new GraphQLClient ( ctx . url )
31+ client . setHeaders ( headers )
32+ const mock = ctx . res ( )
33+ await client . request ( `{ me { id } }` )
34+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( headerFoo . value )
3835 } )
3936
4037 describe ( `custom header in the request` , ( ) => {
41- describe . each < HeadersInit [ ] > ( [
42- [ new Headers ( { 'x-request-foo' : `request-bar` } ) ] ,
43- [ { 'x-request-foo' : `request-bar` } ] ,
44- [ [ [ `x-request-foo` , `request-bar` ] ] ] ,
45- ] ) ( `request unique header with request` , ( headerCase ) => {
46- test ( `with request method` , async ( ) => {
38+ describe ( `request unique header with request` , ( ) => {
39+ test ( `.request()` , async ( ) => {
4740 const client = new GraphQLClient ( ctx . url )
4841
49- client . setHeaders ( new Headers ( { 'x-foo' : `bar` } ) )
42+ client . setHeaders ( headers )
5043 const mock = ctx . res ( )
51- await client . request ( `{ me { id } }` , { } , headerCase )
44+ await client . request ( `{ me { id } }` , { } , new Headers ( { a : `b` } ) )
5245
53- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `bar` )
54- expect ( mock . requests [ 0 ] ?. headers [ `x-request-foo ` ] ) . toEqual ( `request-bar ` )
46+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( headerFoo . value )
47+ expect ( mock . requests [ 0 ] ?. headers [ `a ` ] ) . toEqual ( `b ` )
5548 } )
5649
57- test ( `with rawRequest method ` , async ( ) => {
50+ test ( `. rawRequest() ` , async ( ) => {
5851 const client = new GraphQLClient ( ctx . url )
5952
60- client . setHeaders ( new Headers ( { 'x-foo' : `bar` } ) )
53+ client . setHeaders ( headers )
6154 const mock = ctx . res ( )
62- await client . rawRequest ( `{ me { id } }` , { } , headerCase )
55+ await client . rawRequest ( `{ me { id } }` , { } , new Headers ( { a : `b` } ) )
6356
64- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `bar` )
65- expect ( mock . requests [ 0 ] ?. headers [ `x-request-foo` ] ) . toEqual ( `request-bar` )
57+ expect ( mock . requests [ 0 ] ) . toMatchObject ( {
58+ headers : {
59+ [ headerFoo . name ] : headerFoo . value ,
60+ a : `b` ,
61+ } ,
62+ } )
6663 } )
6764 } )
6865
69- describe . each < HeadersInit [ ] > ( [
70- [ new Headers ( { 'x-foo' : `request-bar` } ) ] ,
71- [ { 'x-foo' : `request-bar` } ] ,
72- [ [ [ `x-foo` , `request-bar` ] ] ] ,
73- ] ) ( `request header overriding the client header` , ( headerCase ) => {
74- test ( `with request method` , async ( ) => {
66+ describe ( `request overriding instance` , ( ) => {
67+ test ( `.request()` , async ( ) => {
7568 const client = new GraphQLClient ( ctx . url )
76- client . setHeader ( `x-foo` , `bar` )
69+ client . setHeader ( headerFoo . name , headerFoo . value )
7770 const mock = ctx . res ( )
78- await client . request ( `{ me { id } }` , { } , headerCase )
79- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `request-bar` )
71+ await client . request ( `{ me { id } }` , { } , headers )
72+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( headerFoo . value )
8073 } )
8174
82- test ( `with rawRequest method ` , async ( ) => {
75+ test ( `. rawRequest() ` , async ( ) => {
8376 const client = new GraphQLClient ( ctx . url )
84- client . setHeader ( `x-foo` , `bar` )
77+ client . setHeader ( headerFoo . name , headerFoo . value )
8578 const mock = ctx . res ( )
86- await client . rawRequest ( `{ me { id } }` , { } , headerCase )
87- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `request-bar` )
79+ await client . rawRequest ( `{ me { id } }` , { } , headers )
80+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( headerFoo . value )
8881 } )
8982 } )
9083
9184 describe ( `gets fresh dynamic headers before each request` , ( ) => {
92- test ( `with request method ` , async ( ) => {
93- const objectChangedThroughReference = { 'x-foo' : `old` }
85+ test ( `. request() ` , async ( ) => {
86+ const objectChangedThroughReference = { [ headerFoo . name ] : `old` }
9487 const client = new GraphQLClient ( ctx . url , { headers : ( ) => objectChangedThroughReference } )
95- objectChangedThroughReference [ `x-foo` ] = `new`
88+ objectChangedThroughReference [ headerFoo . name ] = `new`
9689 const mock = ctx . res ( )
9790 await client . request ( `{ me { id } }` )
98- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `new` )
91+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( `new` )
9992 } )
10093
101- test ( `with rawRequest method ` , async ( ) => {
102- const objectChangedThroughReference = { 'x-foo' : `old` }
94+ test ( `. rawRequest() ` , async ( ) => {
95+ const objectChangedThroughReference = { [ headerFoo . name ] : `old` }
10396 const client = new GraphQLClient ( ctx . url , { headers : ( ) => objectChangedThroughReference } )
104- objectChangedThroughReference [ `x-foo` ] = `new`
97+ objectChangedThroughReference [ headerFoo . name ] = `new`
10598 const mock = ctx . res ( )
10699 await client . rawRequest ( `{ me { id } }` )
107- expect ( mock . requests [ 0 ] ?. headers [ `x-foo` ] ) . toEqual ( `new` )
100+ expect ( mock . requests [ 0 ] ?. headers [ headerFoo . name ] ) . toEqual ( `new` )
108101 } )
109102 } )
110103 } )
111104} )
112-
113- describe ( `using request function` , ( ) => {
114- describe . each < HeadersInit [ ] > ( [
115- [ new Headers ( { 'x-request-foo' : `request-bar` } ) ] ,
116- [ { 'x-request-foo' : `request-bar` } ] ,
117- [ [ [ `x-request-foo` , `request-bar` ] ] ] ,
118- ] ) ( `request unique header with request` , ( headerCase ) => {
119- test ( `sets header` , async ( ) => {
120- const mock = ctx . res ( )
121- await request ( ctx . url , `{ me { id } }` , { } , headerCase )
122-
123- expect ( mock . requests [ 0 ] ?. headers [ `x-request-foo` ] ) . toEqual ( `request-bar` )
124- } )
125- } )
126- } )
0 commit comments