File tree 2 files changed +28
-7
lines changed
2 files changed +28
-7
lines changed Original file line number Diff line number Diff line change 31
31
} )
32
32
}
33
33
34
+ function __handleMethod ( method , url , data , asJson ) {
35
+ var cleaned = { url : url } ;
36
+ var isGet = method . toUpperCase ( ) === 'GET' ;
37
+ if ( asJson && ! isGet ) {
38
+ cleaned . body = JSON . stringify ( { query : data . query , variables : data . variables } ) ;
39
+ } else {
40
+ cleaned . url = url + '?' + "query=" + encodeURIComponent ( data . query ) + "&variables=" + encodeURIComponent ( JSON . stringify ( data . variables ) )
41
+ }
42
+ return cleaned ;
43
+ }
44
+
34
45
var __doRequest
35
46
36
47
if ( typeof XMLHttpRequest !== 'undefined' ) {
87
98
if ( ! url ) {
88
99
return ;
89
100
}
90
- if ( asJson ) {
91
- var body = JSON . stringify ( { query : data . query , variables : data . variables } ) ;
92
- } else {
93
- var body = "query=" + encodeURIComponent ( data . query ) + "&variables=" + encodeURIComponent ( JSON . stringify ( data . variables ) )
94
- }
101
+ var cleaned = __handleMethod ( method , url , data , asJson ) ;
102
+
95
103
if ( debug ) {
96
104
console . groupCollapsed ( '[graphql]: '
97
105
+ method . toUpperCase ( ) + ' ' + url + ': '
110
118
111
119
__doRequest (
112
120
method ,
113
- url ,
121
+ cleaned . url ,
114
122
asJson ? 'application/json' : 'application/x-www-form-urlencoded' ,
115
123
'application/json' ,
116
124
headers ,
117
- body ,
125
+ cleaned . body ,
118
126
onRequestError ,
119
127
callback
120
128
)
Original file line number Diff line number Diff line change @@ -200,6 +200,19 @@ fragment auth_error on Error {messages}`;
200
200
201
201
set ( 'url' , ( ) => 'https://example.org' ) ;
202
202
203
+ describe ( 'when method is GET' , ( ) => {
204
+ set ( 'method' , ( ) => 'get' ) ;
205
+
206
+ it ( 'makes the request passing the parameters as query arguments' , ( ) => {
207
+ let xhr = mockXHR ( 200 , { } ) ;
208
+ xhr . send = jest . fn ( ) ;
209
+ fetchPost ( { id : 123 } ) ;
210
+ expect ( xhr . send ) . toHaveBeenCalledWith ( undefined ) ;
211
+ expect ( xhr . open ) . toHaveBeenCalledWith ( method , expect . stringMatching ( url ) , true )
212
+ expect ( xhr . open ) . toHaveBeenCalledWith ( method , expect . stringMatching ( / \? q u e r y = .+ & v a r i a b l e s = / ) , true )
213
+ } ) ;
214
+ } ) ;
215
+
203
216
describe ( 'when executing the queries normally' , ( ) => {
204
217
it ( 'sends a network request right away' , ( ) => {
205
218
let xhr = mockXHR ( 200 , { } ) ;
You can’t perform that action at this time.
0 commit comments