-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional cookie jar support (#85)
If the `jar` field is set om the base service, the axios instance is wrapped in a library that adds cookie support. Values for `jar` can be an instance of cookie jar library or `true`, which will cause a default cookie jar to be created using the `tough-cookie` package. Co-authored-by: Christian Compton <[email protected]>
- Loading branch information
Showing
7 changed files
with
188 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
// the `toBeInstanceOf` assertion compares for function reference equality | ||
// importing our own `tough-cookie` dependency would create a different function | ||
// reference and render the assertion unusable. the solution is to use the | ||
// dependency within axios-cookiejar-support | ||
const tough = require('axios-cookiejar-support/node_modules/tough-cookie'); | ||
const { RequestWrapper } = require('../../dist/lib/request-wrapper'); | ||
|
||
describe('cookie jar support', () => { | ||
it('should not wrap the axios instance by default', () => { | ||
const wrapper = new RequestWrapper(); | ||
expect(wrapper.axiosInstance.defaults.withCredentials).not.toBeDefined(); | ||
expect(wrapper.axiosInstance.interceptors.request.handlers.length).toBe(0); | ||
}); | ||
|
||
it('passing a value for `jar` should produce interceptors and set flags', () => { | ||
const wrapper = new RequestWrapper({ jar: true }); | ||
expect(wrapper.axiosInstance.defaults.withCredentials).toBe(true); | ||
expect(wrapper.axiosInstance.interceptors.request.handlers.length).toBe(1); | ||
}); | ||
|
||
it('given `true` for `jar`, the interceptors should create an instance of tough-cookie', () => { | ||
const wrapper = new RequestWrapper({ jar: true }); | ||
|
||
expect(wrapper.axiosInstance.interceptors.request.handlers.length).toBe(1); | ||
expect(wrapper.axiosInstance.interceptors.request.handlers[0].fulfilled).toBeInstanceOf( | ||
Function | ||
); | ||
|
||
// should initially set the default to true | ||
expect(wrapper.axiosInstance.defaults.jar).toBe(true); | ||
|
||
// invoke the interceptor - it should be the one added by the cookie jar library | ||
// it should see that `jar` is `true` and create a default instance of tough.CookieJar | ||
// this would noramlly happen just before a request is sent | ||
wrapper.axiosInstance.interceptors.request.handlers[0].fulfilled( | ||
wrapper.axiosInstance.defaults | ||
); | ||
|
||
expect(wrapper.axiosInstance.defaults.jar).toBeInstanceOf(tough.CookieJar); | ||
}); | ||
|
||
it('given arbitrary value for `jar`, the interceptor should use it as cookie jar', () => { | ||
// the axios-cookiejar-support interceptor requires the jar object | ||
// to have the method `getCookieString` | ||
const mockCookieJar = { getCookieString: () => 'mock-string' }; | ||
const wrapper = new RequestWrapper({ jar: mockCookieJar }); | ||
|
||
// should still set interceptors and withCredentials flag | ||
expect(wrapper.axiosInstance.interceptors.request.handlers.length).toBe(1); | ||
expect(wrapper.axiosInstance.defaults.withCredentials).toBe(true); | ||
expect(wrapper.axiosInstance.defaults.jar).toEqual(mockCookieJar); | ||
|
||
// invoke the interceptor, the default jar should remain the same | ||
wrapper.axiosInstance.interceptors.request.handlers[0].fulfilled( | ||
wrapper.axiosInstance.defaults | ||
); | ||
|
||
expect(wrapper.axiosInstance.defaults.jar).toEqual(mockCookieJar); | ||
}); | ||
}); |