Skip to content

Commit 3a4d370

Browse files
author
Oliver Jeeves
committed
Make authorizeRequest requests simple requests that don't trigger CORS preflight
Simple requests are described here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Examples_of_access_control_scenarios Fixes swagger-api#6081
1 parent 1226895 commit 3a4d370

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/core/plugins/auth/actions.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ export const authorizeRequest = ( data ) => ( { fn, getConfigs, authActions, err
188188

189189
let _headers = Object.assign({
190190
"Accept":"application/json, text/plain, */*",
191-
"Content-Type": "application/x-www-form-urlencoded",
192-
"X-Requested-With": "XMLHttpRequest"
191+
"Content-Type": "application/x-www-form-urlencoded"
193192
}, headers)
194193

195194
fn.fetch({
@@ -277,4 +276,4 @@ export const persistAuthorizationIfNeeded = () => ( { authSelectors, getConfigs
277276
const authorized = authSelectors.authorized()
278277
localStorage.setItem("authorized", JSON.stringify(authorized.toJS()))
279278
}
280-
}
279+
}

test/unit/core/plugins/auth/actions.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,73 @@ describe("auth plugin - actions", () => {
174174
expect(system.fn.fetch.mock.calls[0][0].url)
175175
.toEqual("http://google.com/authorize?q=1&myCustomParam=abc123")
176176
})
177+
178+
it("should make a simple request that doesn't trigger CORS preflight", () => {
179+
180+
// Given
181+
const data = {
182+
url: "/authorize?q=1"
183+
}
184+
const system = {
185+
fn: {
186+
fetch: jest.fn().mockImplementation(() => Promise.resolve())
187+
},
188+
errActions: {
189+
newAuthErr: () => ({})
190+
},
191+
getConfigs: () => ({}),
192+
authSelectors: {
193+
getConfigs: () => ({
194+
additionalQueryStringParams: {
195+
myCustomParam: "abc123"
196+
}
197+
})
198+
},
199+
oas3Selectors: {
200+
selectedServer: () => "http://google.com",
201+
serverEffectiveValue: () => "http://google.com"
202+
},
203+
specSelectors: {
204+
isOAS3: () => true,
205+
}
206+
}
207+
208+
// When
209+
authorizeRequest(data)(system)
210+
211+
// Then
212+
// source: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
213+
const allowed_methods = ["get", "head", "post"]
214+
const allowed_headers = [
215+
"accept",
216+
"accept-language",
217+
"content-language",
218+
"content-type",
219+
"dpr",
220+
"downlink",
221+
"save-data",
222+
"viewport-width",
223+
"width"
224+
]
225+
const allowed_types = [
226+
"application/x-www-form-urlencoded",
227+
"multipart/form-data",
228+
"text/plain"
229+
]
230+
231+
expect(system.fn.fetch.mock.calls.length).toEqual(1)
232+
233+
expect(allowed_methods)
234+
.toContain(system.fn.fetch.mock.calls[0][0].method.toLowerCase())
235+
const reqHeaders = system.fn.fetch.mock.calls[0][0].headers
236+
Object.keys(reqHeaders).forEach(header => {
237+
expect(allowed_headers).toContain(header.toLowerCase())
238+
239+
if (header.toLowerCase() == "content-type") {
240+
expect(allowed_types).toContain(reqHeaders[header])
241+
}
242+
})
243+
})
177244
})
178245

179246
describe("tokenRequest", function () {

0 commit comments

Comments
 (0)