-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathconfigure-proxy.basic.test.js
More file actions
241 lines (211 loc) · 7.63 KB
/
configure-proxy.basic.test.js
File metadata and controls
241 lines (211 loc) · 7.63 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {applyProxyRequestHeaders, applyScapiAuthHeaders, configureProxy} from './configure-proxy'
import * as ssrProxying from '../ssr-proxying'
import * as utils from './utils'
import cookie from 'cookie'
jest.mock('cookie')
jest.mock('./utils', () => ({
...jest.requireActual('./utils'),
isScapiDomain: jest.fn()
}))
describe('applyProxyRequestHeaders', () => {
it('removes a header not present in new headers', () => {
const incomingRequest = {
url: '/path',
headers: {x: '1', toremove: '2'}
}
const proxyRequest = {
setHeader: jest.fn(),
removeHeader: jest.fn()
}
jest.spyOn(ssrProxying, 'rewriteProxyRequestHeaders').mockImplementation(() => ({x: '99'}))
applyProxyRequestHeaders({
proxyRequest,
incomingRequest,
logging: false,
caching: false,
proxyPath: '/proxy/',
targetHost: 'localhost',
targetProtocol: 'http'
})
expect(proxyRequest.setHeader).toHaveBeenCalledWith('x', '99')
expect(proxyRequest.removeHeader).toHaveBeenCalledWith('toremove')
ssrProxying.rewriteProxyRequestHeaders.mockRestore()
})
})
describe('configureProxy ALLOWED_CACHING_PROXY_REQUEST_METHODS', () => {
it('returns 405 for disallowed method', () => {
const wrapper = configureProxy({
appHostname: 'localhost',
proxyPath: '/mobify/caching/base/',
targetProtocol: 'http',
targetHost: 'api.test.com',
caching: true
})
const req = {method: 'POST'} // not HEAD, GET, OPTIONS
const res = {
status: jest.fn().mockReturnThis(),
send: jest.fn().mockReturnThis(),
end: jest.fn()
}
wrapper(req, res, jest.fn())
expect(res.status).toHaveBeenCalledWith(405)
expect(res.send).toHaveBeenCalledWith('Method POST not supported for caching proxy')
expect(res.end).toHaveBeenCalled()
})
it('calls next/proxyFunc for allowed method', () => {
const wrapper = configureProxy({
appHostname: 'localhost',
proxyPath: '/mobify/caching/base/',
targetProtocol: 'http',
targetHost: 'api.test.com',
caching: true
})
const req = {method: 'GET'}
const res = {}
const next = jest.fn()
// proxyFunc will try to execute, so just check next is a function
expect(typeof wrapper).toBe('function')
// safe: don't actually assert calls for proxyFunc, just ensure it's a function
})
it('returns a plain proxyFunc for non-caching proxy', () => {
const result = configureProxy({
appHostname: 'localhost',
proxyPath: '/mobify/proxy/base/',
targetProtocol: 'http',
targetHost: 'api.test.com',
caching: false
})
expect(typeof result).toBe('function')
})
})
describe('applyScapiAuthHeaders', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('applies Bearer token for non-SLAS Shopper API endpoints', () => {
utils.isScapiDomain.mockReturnValue(true)
cookie.parse.mockReturnValue({'cc-at_RefArch': 'test-access-token'})
const proxyRequest = {
setHeader: jest.fn(),
removeHeader: jest.fn()
}
const incomingRequest = {
url: '/shopper/products/v1/products',
headers: {cookie: 'cc-at_RefArch=test-access-token'}
}
applyScapiAuthHeaders({
proxyRequest,
incomingRequest,
caching: false,
siteId: 'RefArch',
targetHost: 'abc-001.api.commercecloud.salesforce.com'
})
expect(proxyRequest.setHeader).toHaveBeenCalledWith(
'authorization',
'Bearer test-access-token'
)
})
it('skips all SLAS auth endpoints (handled by SLAS private proxy)', () => {
utils.isScapiDomain.mockReturnValue(true)
cookie.parse.mockReturnValue({'cc-at_RefArch': 'test-access-token'})
const proxyRequest = {
setHeader: jest.fn()
}
const incomingRequest = {
url: '/shopper/auth/v1/oauth2/logout',
headers: {cookie: 'cc-at_RefArch=test-access-token'}
}
applyScapiAuthHeaders({
proxyRequest,
incomingRequest,
caching: false,
siteId: 'RefArch',
targetHost: 'abc-001.api.commercecloud.salesforce.com'
})
// SLAS auth endpoints are handled by the SLAS private client proxy
expect(proxyRequest.setHeader).not.toHaveBeenCalled()
})
it('does not apply Bearer token when caching is true', () => {
utils.isScapiDomain.mockReturnValue(true)
cookie.parse.mockReturnValue({'cc-at_RefArch': 'test-access-token'})
const proxyRequest = {
setHeader: jest.fn()
}
const incomingRequest = {
url: '/shopper/products/v1/products',
headers: {cookie: 'cc-at_RefArch=test-access-token'}
}
applyScapiAuthHeaders({
proxyRequest,
incomingRequest,
caching: true,
siteId: 'RefArch',
targetHost: 'abc-001.api.commercecloud.salesforce.com'
})
// Caching proxies don't use auth
expect(proxyRequest.setHeader).not.toHaveBeenCalled()
})
it('does not apply Bearer token when siteId is not provided', () => {
utils.isScapiDomain.mockReturnValue(true)
cookie.parse.mockReturnValue({})
const proxyRequest = {
setHeader: jest.fn()
}
const incomingRequest = {
url: '/shopper/products/v1/products',
headers: {}
}
applyScapiAuthHeaders({
proxyRequest,
incomingRequest,
caching: false,
siteId: null,
targetHost: 'abc-001.api.commercecloud.salesforce.com'
})
expect(proxyRequest.setHeader).not.toHaveBeenCalled()
})
it('does not apply Bearer token when target is not SCAPI domain', () => {
utils.isScapiDomain.mockReturnValue(false)
cookie.parse.mockReturnValue({'cc-at_RefArch': 'test-access-token'})
const proxyRequest = {
setHeader: jest.fn()
}
const incomingRequest = {
url: '/api/products',
headers: {cookie: 'cc-at_RefArch=test-access-token'}
}
applyScapiAuthHeaders({
proxyRequest,
incomingRequest,
caching: false,
siteId: 'RefArch',
targetHost: 'external-api.example.com'
})
expect(proxyRequest.setHeader).not.toHaveBeenCalled()
})
it('does not apply Bearer token when cookie is not present', () => {
utils.isScapiDomain.mockReturnValue(true)
cookie.parse.mockReturnValue({}) // No access token cookie
const proxyRequest = {
setHeader: jest.fn()
}
const incomingRequest = {
url: '/shopper/products/v1/products',
headers: {}
}
applyScapiAuthHeaders({
proxyRequest,
incomingRequest,
caching: false,
siteId: 'RefArch',
targetHost: 'abc-001.api.commercecloud.salesforce.com'
})
expect(proxyRequest.setHeader).not.toHaveBeenCalled()
})
})