Skip to content

Commit 79b5033

Browse files
maxkostowsoedirgo
authored andcommitted
fix: do not cache Authorization header (and add tests)
1 parent e88e521 commit 79b5033

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
4+
clearMocks: true,
45
collectCoverage: false,
56
coverageDirectory: './test/coverage',
67
coverageReporters: ['json', 'html', 'lcov'],

src/SupabaseClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export default class SupabaseClient {
265265
}
266266

267267
private _getAuthHeaders(): GenericObject {
268-
const headers: GenericObject = this.headers
268+
const headers: GenericObject = { ...this.headers }
269269
const authBearer = this.auth.session()?.access_token ?? this.supabaseKey
270270
headers['apikey'] = this.supabaseKey
271271
headers['Authorization'] = headers['Authorization'] || `Bearer ${authBearer}`

test/client.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,40 @@ test('it should throw an error if no valid params are provided', async () => {
1616
expect(() => createClient(URL, '')).toThrowError('supabaseKey is required.')
1717
})
1818

19-
describe('Custom Headers', () => {
20-
const customHeader = { 'X-Test-Header': 'value' }
19+
test('it should not cache Authorization header', async () => {
20+
const checkHeadersSpy = jest.spyOn(SupabaseClient.prototype as any, '_getAuthHeaders')
21+
22+
supabase.auth.setAuth('token1')
23+
supabase.rpc('') // Calling public method `rpc` calls private method _getAuthHeaders which result we want to test
24+
supabase.auth.setAuth('token2')
25+
supabase.rpc('') // Calling public method `rpc` calls private method _getAuthHeaders which result we want to test
26+
27+
expect(checkHeadersSpy.mock.results[0].value).toHaveProperty('Authorization', 'Bearer token1')
28+
expect(checkHeadersSpy.mock.results[1].value).toHaveProperty('Authorization', 'Bearer token2')
29+
})
2130

31+
describe('Custom Headers', () => {
2232
test('should have custom header set', () => {
33+
const customHeader = { 'X-Test-Header': 'value' }
34+
2335
const checkHeadersSpy = jest.spyOn(SupabaseClient.prototype as any, '_getAuthHeaders')
2436
createClient(URL, KEY, { headers: customHeader }).rpc('') // Calling public method `rpc` calls private method _getAuthHeaders which result we want to test
2537
const getHeaders = checkHeadersSpy.mock.results[0].value
2638

2739
expect(checkHeadersSpy).toBeCalled()
2840
expect(getHeaders).toHaveProperty('X-Test-Header', 'value')
2941
})
42+
43+
test('should allow custom Authorization header', () => {
44+
const customHeader = { Authorization: 'Bearer custom_token' }
45+
supabase.auth.setAuth('override_me')
46+
const checkHeadersSpy = jest.spyOn(SupabaseClient.prototype as any, '_getAuthHeaders')
47+
createClient(URL, KEY, { headers: customHeader }).rpc('') // Calling public method `rpc` calls private method _getAuthHeaders which result we want to test
48+
const getHeaders = checkHeadersSpy.mock.results[0].value
49+
50+
expect(checkHeadersSpy).toBeCalled()
51+
expect(getHeaders).toHaveProperty('Authorization', 'Bearer custom_token')
52+
})
3053
})
3154

3255
// Socket should close when there are no open connections

0 commit comments

Comments
 (0)