@@ -4,123 +4,101 @@ import { describe, expect, test } from 'vitest'
4
4
5
5
const ctx = setupMockServer ( )
6
6
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
+
7
21
describe ( `using class` , ( ) => {
8
- test ( `.setHeader() sets a header that get sent to server ` , async ( ) => {
22
+ test ( `.setHeader() sets a header` , async ( ) => {
9
23
const client = new GraphQLClient ( ctx . url )
10
- client . setHeader ( `x-foo` , `bar` )
24
+ client . setHeader ( headerFoo . name , headerFoo . value )
11
25
const mock = ctx . res ( )
12
26
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 )
14
28
} )
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 )
38
35
} )
39
36
40
37
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 ( ) => {
47
40
const client = new GraphQLClient ( ctx . url )
48
41
49
- client . setHeaders ( new Headers ( { 'x-foo' : `bar` } ) )
42
+ client . setHeaders ( headers )
50
43
const mock = ctx . res ( )
51
- await client . request ( `{ me { id } }` , { } , headerCase )
44
+ await client . request ( `{ me { id } }` , { } , new Headers ( { a : `b` } ) )
52
45
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 ` )
55
48
} )
56
49
57
- test ( `with rawRequest method ` , async ( ) => {
50
+ test ( `. rawRequest() ` , async ( ) => {
58
51
const client = new GraphQLClient ( ctx . url )
59
52
60
- client . setHeaders ( new Headers ( { 'x-foo' : `bar` } ) )
53
+ client . setHeaders ( headers )
61
54
const mock = ctx . res ( )
62
- await client . rawRequest ( `{ me { id } }` , { } , headerCase )
55
+ await client . rawRequest ( `{ me { id } }` , { } , new Headers ( { a : `b` } ) )
63
56
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
+ } )
66
63
} )
67
64
} )
68
65
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 ( ) => {
75
68
const client = new GraphQLClient ( ctx . url )
76
- client . setHeader ( `x-foo` , `bar` )
69
+ client . setHeader ( headerFoo . name , headerFoo . value )
77
70
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 )
80
73
} )
81
74
82
- test ( `with rawRequest method ` , async ( ) => {
75
+ test ( `. rawRequest() ` , async ( ) => {
83
76
const client = new GraphQLClient ( ctx . url )
84
- client . setHeader ( `x-foo` , `bar` )
77
+ client . setHeader ( headerFoo . name , headerFoo . value )
85
78
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 )
88
81
} )
89
82
} )
90
83
91
84
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` }
94
87
const client = new GraphQLClient ( ctx . url , { headers :( ) => objectChangedThroughReference } )
95
- objectChangedThroughReference [ `x-foo` ] = `new`
88
+ objectChangedThroughReference [ headerFoo . name ] = `new`
96
89
const mock = ctx . res ( )
97
90
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` )
99
92
} )
100
93
101
- test ( `with rawRequest method ` , async ( ) => {
102
- const objectChangedThroughReference = { 'x-foo' :`old` }
94
+ test ( `. rawRequest() ` , async ( ) => {
95
+ const objectChangedThroughReference = { [ headerFoo . name ] :`old` }
103
96
const client = new GraphQLClient ( ctx . url , { headers :( ) => objectChangedThroughReference } )
104
- objectChangedThroughReference [ `x-foo` ] = `new`
97
+ objectChangedThroughReference [ headerFoo . name ] = `new`
105
98
const mock = ctx . res ( )
106
99
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` )
108
101
} )
109
102
} )
110
103
} )
111
104
} )
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
- } )