@@ -22,11 +22,11 @@ describe('perform', () => {
22
22
23
23
const testRequest = new FetchRequest ( "get" , "localhost" )
24
24
const testResponse = await testRequest . perform ( )
25
-
25
+
26
26
expect ( window . fetch ) . toHaveBeenCalledTimes ( 1 )
27
27
expect ( window . fetch ) . toHaveBeenCalledWith ( "localhost" , testRequest . fetchOptions )
28
28
expect ( testResponse ) . toStrictEqual ( new FetchResponse ( mockResponse ) )
29
- } )
29
+ } )
30
30
31
31
test ( 'request is performed with 401' , async ( ) => {
32
32
const mockResponse = new Response ( undefined , { status : 401 , headers : { 'WWW-Authenticate' : 'https://localhost/login' } } )
@@ -111,20 +111,20 @@ describe('header handling', () => {
111
111
expect ( customRequest . fetchOptions . headers )
112
112
. toStrictEqual ( { ...defaultHeaders , "Content-Type" : 'any/thing' } )
113
113
} )
114
- test ( 'is not set by formData' , ( ) => {
114
+ test ( 'is not set by formData' , ( ) => {
115
115
const formData = new FormData ( )
116
116
formData . append ( "this" , "value" )
117
117
const formDataRequest = new FetchRequest ( "get" , "localhost" , { body : formData } )
118
118
expect ( formDataRequest . fetchOptions . headers )
119
119
. toStrictEqual ( defaultHeaders )
120
120
} )
121
- test ( 'is set by file body' , ( ) => {
121
+ test ( 'is set by file body' , ( ) => {
122
122
const file = new File ( [ "contenxt" ] , "file.txt" , { type : "text/plain" } )
123
123
const fileRequest = new FetchRequest ( "get" , "localhost" , { body : file } )
124
124
expect ( fileRequest . fetchOptions . headers )
125
- . toStrictEqual ( { ...defaultHeaders , "Content-Type" : "text/plain" } )
125
+ . toStrictEqual ( { ...defaultHeaders , "Content-Type" : "text/plain" } )
126
126
} )
127
- test ( 'is set by json body' , ( ) => {
127
+ test ( 'is set by json body' , ( ) => {
128
128
const jsonRequest = new FetchRequest ( "get" , "localhost" , { body : { some : "json" } } )
129
129
expect ( jsonRequest . fetchOptions . headers )
130
130
. toStrictEqual ( { ...defaultHeaders , "Content-Type" : "application/json" } )
@@ -138,13 +138,13 @@ describe('header handling', () => {
138
138
request . addHeader ( "test" , "header" )
139
139
expect ( request . fetchOptions . headers )
140
140
. toStrictEqual ( { ...defaultHeaders , custom : "Header" , "Content-Type" : "application/json" , "test" : "header" } )
141
- } )
141
+ } )
142
142
143
143
test ( 'headers win over contentType' , ( ) => {
144
144
const request = new FetchRequest ( "get" , "localhost" , { contentType : "application/json" , headers : { "Content-Type" : "this/overwrites" } } )
145
145
expect ( request . fetchOptions . headers )
146
146
. toStrictEqual ( { ...defaultHeaders , "Content-Type" : "this/overwrites" } )
147
- } )
147
+ } )
148
148
149
149
test ( 'serializes JSON to String' , ( ) => {
150
150
const jsonBody = { some : "json" }
@@ -169,7 +169,7 @@ describe('header handling', () => {
169
169
request = new FetchRequest ( "get" , "localhost" , { redirect } )
170
170
expect ( request . fetchOptions . redirect ) . toBe ( redirect )
171
171
}
172
-
172
+
173
173
request = new FetchRequest ( "get" , "localhost" )
174
174
expect ( request . fetchOptions . redirect ) . toBe ( "follow" )
175
175
} )
@@ -191,7 +191,30 @@ describe('header handling', () => {
191
191
// has no effect
192
192
request = new FetchRequest ( "get" , "localhost" , { credentials : "omit" } )
193
193
expect ( request . fetchOptions . credentials ) . toBe ( 'same-origin' )
194
- } )
194
+ } )
195
+
196
+ describe ( 'csrf token inclusion' , ( ) => {
197
+ // window.location.hostname is "localhost" in the test suite
198
+ test ( 'csrf token is not included in headers if url hostname is not the same as window.location' , ( ) => {
199
+ const request = new FetchRequest ( "get" , "http://removeservice.com/test.json" )
200
+ expect ( request . fetchOptions . headers ) . not . toHaveProperty ( "X-CSRF-Token" )
201
+ } )
202
+
203
+ test ( 'csrf token is included in headers if url hostname is the same as window.location' , ( ) => {
204
+ const request = new FetchRequest ( "get" , "http://localhost/test.json" )
205
+ expect ( request . fetchOptions . headers ) . toHaveProperty ( "X-CSRF-Token" )
206
+ } )
207
+
208
+ test ( 'csrf token is included if url is a realative path' , async ( ) => {
209
+ const defaultRequest = new FetchRequest ( "get" , "/somepath" )
210
+ expect ( defaultRequest . fetchOptions . headers ) . toHaveProperty ( "X-CSRF-Token" )
211
+ } )
212
+
213
+ test ( 'csrf token is included if url is not parseable' , async ( ) => {
214
+ const defaultRequest = new FetchRequest ( "get" , "not-a-url" )
215
+ expect ( defaultRequest . fetchOptions . headers ) . toHaveProperty ( "X-CSRF-Token" )
216
+ } )
217
+ } )
195
218
} )
196
219
197
220
describe ( 'query params are parsed' , ( ) => {
0 commit comments