Skip to content

Commit 1a3a015

Browse files
committed
port in basepath changes from #3641
1 parent 2c809bb commit 1a3a015

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

packages/template-mrt-reference-app/app/ssr.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const ENVS_TO_EXPOSE = [
6565
'aws_lambda_log_stream_name',
6666
'aws_region',
6767
'bundle_id',
68+
'mrt_env_base_path',
6869
// These "customer" defined environment variables are set by the Manager
6970
// and expected by the MRT smoke test suite
7071
'customer_*',
@@ -360,6 +361,19 @@ const loggingMiddleware = (req, res, next) => {
360361
return next()
361362
}
362363

364+
const envBasePathMiddleware = (req, res, next) => {
365+
const basePath = process.env.MRT_ENV_BASE_PATH
366+
console.log(`Base path: ${basePath}`)
367+
console.log(`Request path: ${req.url}`)
368+
if (basePath && req.url.startsWith(basePath)) {
369+
req.url = req.path.slice(basePath.length) || '/'
370+
console.log(
371+
`Base path: Rewrote ${basePath} -> Request path: ${req.originalUrl} -> New path: ${req.url}`
372+
)
373+
}
374+
return next()
375+
}
376+
363377
const options = {
364378
// The build directory (an absolute path)
365379
buildDir: path.resolve(process.cwd(), 'build'),
@@ -394,7 +408,8 @@ const {handler, app, server} = runtime.createHandler(options, (app) => {
394408

395409
// Add middleware to log request and response headers
396410
app.use(loggingMiddleware)
397-
411+
// Add a middleware to consume the base path from the request path if one is set
412+
app.use(envBasePathMiddleware)
398413
// Configure routes
399414
app.all('/exception', exception)
400415
app.get('/tls', tlsVersionTest)

packages/template-mrt-reference-app/app/ssr.test.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ const {
1717
const {mockClient} = require('aws-sdk-client-mock')
1818
const {ServiceException} = require('@smithy/smithy-client')
1919

20+
const pathsToCheck = [
21+
['/', 200, 'application/json; charset=utf-8'],
22+
['/tls', 200, 'application/json; charset=utf-8'],
23+
['/exception', 500, 'text/html; charset=utf-8'],
24+
['/cache', 200, 'application/json; charset=utf-8'],
25+
['/cookie', 200, 'application/json; charset=utf-8'],
26+
['/set-response-headers', 200, 'application/json; charset=utf-8'],
27+
['/isolation', 200, 'application/json; charset=utf-8'],
28+
['/memtest', 200, 'application/json; charset=utf-8'],
29+
['/streaming-large', 200, 'application/json; charset=utf-8']
30+
]
31+
const pathsToCheckWithBasePath = (basePath) => {
32+
return pathsToCheck.map((pathStatusContentType) => [
33+
basePath + pathStatusContentType[0],
34+
pathStatusContentType[1],
35+
pathStatusContentType[2]
36+
])
37+
}
38+
2039
class AccessDenied extends ServiceException {
2140
constructor(options) {
2241
super({...options, name: 'AccessDenied'})
@@ -53,23 +72,26 @@ describe('server', () => {
5372
process.env = originalEnv
5473
jest.restoreAllMocks()
5574
})
56-
test.each([
57-
['/', 200, 'application/json; charset=utf-8'],
58-
['/tls', 200, 'application/json; charset=utf-8'],
59-
['/exception', 500, 'text/html; charset=utf-8'],
60-
['/cache', 200, 'application/json; charset=utf-8'],
61-
['/cookie', 200, 'application/json; charset=utf-8'],
62-
['/multi-cookies', 200, 'application/json; charset=utf-8'],
63-
['/set-response-headers', 200, 'application/json; charset=utf-8'],
64-
['/isolation', 200, 'application/json; charset=utf-8'],
65-
['/memtest', 200, 'application/json; charset=utf-8'],
66-
['/streaming-large', 200, 'application/json; charset=utf-8']
67-
])('Path %p should render correctly', (path, expectedStatus, expectedContentType) => {
68-
return request(app)
69-
.get(path)
70-
.expect(expectedStatus)
71-
.expect('Content-Type', expectedContentType)
72-
})
75+
test.each(pathsToCheck)(
76+
'Path %p should render correctly',
77+
(path, expectedStatus, expectedContentType) => {
78+
return request(app)
79+
.get(path)
80+
.expect(expectedStatus)
81+
.expect('Content-Type', expectedContentType)
82+
}
83+
)
84+
85+
test.each(pathsToCheckWithBasePath('/test-base-path'))(
86+
'Path %p should render correctly',
87+
(path, expectedStatus, expectedContentType) => {
88+
process.env.MRT_ENV_BASE_PATH = '/test-base-path'
89+
return request(app)
90+
.get(path)
91+
.expect(expectedStatus)
92+
.expect('Content-Type', expectedContentType)
93+
}
94+
)
7395

7496
test('Path "/cache" has Cache-Control set', () => {
7597
return request(app).get('/cache').expect('Cache-Control', 's-maxage=60')

0 commit comments

Comments
 (0)