-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathaxios-instance.spec.js
More file actions
171 lines (146 loc) · 6.09 KB
/
Copy pathaxios-instance.spec.js
File metadata and controls
171 lines (146 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const { describe, it, expect } = require('@jest/globals');
const { makeAxiosInstance } = require('../../src/utils/axios-instance');
function createStubAdapter() {
let capturedConfig = null;
const adapter = (config) => {
capturedConfig = config;
return Promise.resolve({ data: {}, status: 200, statusText: 'OK', headers: {}, config });
};
adapter.getConfig = () => capturedConfig;
return adapter;
}
describe('makeAxiosInstance', () => {
it('setting User-Agent does not clobber the axios default Accept header', async () => {
const stubAdapter = createStubAdapter();
const instance = makeAxiosInstance();
await instance({ url: 'https://api.example.com/test', method: 'get', adapter: stubAdapter });
// axios.create() sets Accept by default; assigning a new object to defaults.headers.common
// would nuke it. Guard against that regression.
expect(stubAdapter.getConfig().headers['Accept']).toMatch(/application\/json/);
});
it('sets User-Agent header to bruno-runtime version', async () => {
const stubAdapter = createStubAdapter();
const instance = makeAxiosInstance();
await instance({ url: 'https://api.example.com/test', method: 'get', adapter: stubAdapter });
expect(stubAdapter.getConfig().headers['User-Agent']).toMatch(/^bruno-runtime\//);
});
describe('cross-origin redirects authorization stripping', () => {
function createRedirectingStubAdapter(redirectUrl, redirectStatus = 302) {
const calls = [];
const adapter = (config) => {
calls.push(config);
if (calls.length === 1) {
const err = new Error('Redirect ' + redirectStatus);
err.config = config;
err.response = {
status: redirectStatus,
statusText: 'Found',
headers: {
location: redirectUrl
},
data: {}
};
return Promise.reject(err);
}
return Promise.resolve({
data: { success: true },
status: 200,
statusText: 'OK',
headers: {},
config
});
};
adapter.getCalls = () => calls;
return adapter;
}
it('should strip Authorization and Proxy-Authorization headers on cross-origin redirect when forwardAuthorizationOnRedirect is false', async () => {
const stubAdapter = createRedirectingStubAdapter('https://other-domain.com/target');
const instance = makeAxiosInstance({
followRedirects: true,
forwardAuthorizationOnRedirect: false
});
await instance({
url: 'https://api.example.com/start',
method: 'get',
headers: {
'Authorization': 'Bearer my-token',
'Proxy-Authorization': 'Bearer proxy-token',
'Custom-Header': 'keep-me'
},
adapter: stubAdapter
});
const calls = stubAdapter.getCalls();
expect(calls.length).toBe(2);
// First call should have headers
expect(calls[0].headers['Authorization']).toBe('Bearer my-token');
expect(calls[0].headers['Proxy-Authorization']).toBe('Bearer proxy-token');
expect(calls[0].headers['Custom-Header']).toBe('keep-me');
// Redirected call should strip auth headers but keep custom headers
expect(calls[1].headers['Authorization']).toBeUndefined();
expect(calls[1].headers['Proxy-Authorization']).toBeUndefined();
expect(calls[1].headers['Custom-Header']).toBe('keep-me');
});
it('should preserve Authorization and Proxy-Authorization headers on cross-origin redirect when forwardAuthorizationOnRedirect is true', async () => {
const stubAdapter = createRedirectingStubAdapter('https://other-domain.com/target');
const instance = makeAxiosInstance({
followRedirects: true,
forwardAuthorizationOnRedirect: true
});
await instance({
url: 'https://api.example.com/start',
method: 'get',
headers: {
'authorization': 'Bearer my-token',
'proxy-authorization': 'Bearer proxy-token',
'Custom-Header': 'keep-me'
},
adapter: stubAdapter
});
const calls = stubAdapter.getCalls();
expect(calls.length).toBe(2);
expect(calls[1].headers['authorization']).toBe('Bearer my-token');
expect(calls[1].headers['proxy-authorization']).toBe('Bearer proxy-token');
expect(calls[1].headers['Custom-Header']).toBe('keep-me');
});
it('should preserve Authorization and Proxy-Authorization headers on same-origin redirect even if forwardAuthorizationOnRedirect is false', async () => {
const stubAdapter = createRedirectingStubAdapter('https://api.example.com/target');
const instance = makeAxiosInstance({
followRedirects: true,
forwardAuthorizationOnRedirect: false
});
await instance({
url: 'https://api.example.com/start',
method: 'get',
headers: {
'Authorization': 'Bearer my-token',
'Proxy-Authorization': 'Bearer proxy-token'
},
adapter: stubAdapter
});
const calls = stubAdapter.getCalls();
expect(calls.length).toBe(2);
expect(calls[1].headers['Authorization']).toBe('Bearer my-token');
expect(calls[1].headers['Proxy-Authorization']).toBe('Bearer proxy-token');
});
it('should preserve Authorization and Proxy-Authorization headers on relative redirect even if forwardAuthorizationOnRedirect is false', async () => {
const stubAdapter = createRedirectingStubAdapter('/relative-target');
const instance = makeAxiosInstance({
followRedirects: true,
forwardAuthorizationOnRedirect: false
});
await instance({
url: 'https://api.example.com/start',
method: 'get',
headers: {
'Authorization': 'Bearer my-token',
'Proxy-Authorization': 'Bearer proxy-token'
},
adapter: stubAdapter
});
const calls = stubAdapter.getCalls();
expect(calls.length).toBe(2);
expect(calls[1].headers['Authorization']).toBe('Bearer my-token');
expect(calls[1].headers['Proxy-Authorization']).toBe('Bearer proxy-token');
});
});
});