-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathssr.test.js
More file actions
169 lines (154 loc) · 6.43 KB
/
ssr.test.js
File metadata and controls
169 lines (154 loc) · 6.43 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
/*
* Copyright (c) 2022, 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
*/
// Tests cannot run if this require is converted to an import
/* eslint-disable @typescript-eslint/no-var-requires */
const request = require('supertest')
const {LambdaClient, InvokeCommand} = require('@aws-sdk/client-lambda')
const {S3Client, GetObjectCommand} = require('@aws-sdk/client-s3')
const {
CloudWatchLogsClient,
CreateLogStreamCommand,
AccessDeniedException
} = require('@aws-sdk/client-cloudwatch-logs')
const {mockClient} = require('aws-sdk-client-mock')
const {ServiceException} = require('@smithy/smithy-client')
class AccessDenied extends ServiceException {
constructor(options) {
super({...options, name: 'AccessDenied'})
}
}
describe('server', () => {
let originalEnv, app, server
const lambdaMock = mockClient(LambdaClient)
const s3Mock = mockClient(S3Client)
const logsMock = mockClient(CloudWatchLogsClient)
const pathsToCheck = [
['/', 200, 'application/json; charset=utf-8'],
['/tls', 200, 'application/json; charset=utf-8'],
['/exception', 500, 'text/html; charset=utf-8'],
['/cache', 200, 'application/json; charset=utf-8'],
['/cookie', 200, 'application/json; charset=utf-8'],
['/set-response-headers', 200, 'application/json; charset=utf-8'],
['/isolation', 200, 'application/json; charset=utf-8'],
['/memtest', 200, 'application/json; charset=utf-8'],
['/streaming-large', 200, 'application/json; charset=utf-8']
]
const pathsToCheckWithBasePath = (basePath) => {
return pathsToCheck.map((pathStatusContentType) => [
basePath + pathStatusContentType[0],
pathStatusContentType[1],
pathStatusContentType[2]
])
}
beforeEach(() => {
originalEnv = process.env
process.env = Object.assign({}, process.env, {
MRT_ALLOW_COOKIES: 'true',
LISTEN_ADDRESS: '',
BUNDLE_ID: '1',
DEPLOY_TARGET: 'test',
EXTERNAL_DOMAIN_NAME: 'test.com',
MOBIFY_PROPERTY_ID: 'test',
AWS_LAMBDA_FUNCTION_NAME: 'pretend-to-be-remote',
AWS_REGION: 'us-east-2'
})
const ssr = require('./ssr')
app = ssr.app
server = ssr.server
lambdaMock.reset()
s3Mock.reset()
logsMock.reset()
})
afterEach(() => {
process.env = originalEnv
server.close()
jest.restoreAllMocks()
})
test.each(pathsToCheck)(
'Path %p should render correctly',
(path, expectedStatus, expectedContentType) => {
return request(app)
.get(path)
.expect(expectedStatus)
.expect('Content-Type', expectedContentType)
}
)
test.each(pathsToCheckWithBasePath('/test-base-path'))(
'Path %p should render correctly',
(path, expectedStatus, expectedContentType) => {
process.env.MRT_ENV_BASE_PATH = '/test-base-path'
return request(app)
.get(path)
.expect(expectedStatus)
.expect('Content-Type', expectedContentType)
}
)
test('Path "/cache" has Cache-Control set', () => {
return request(app).get('/cache').expect('Cache-Control', 's-maxage=60')
})
test('Path "/cache/:duration" has Cache-Control set correctly', () => {
return request(app).get('/cache/123').expect('Cache-Control', 's-maxage=123')
})
test('All responses have Server header set to "mrt ref app"', () => {
return request(app).get('/').expect('Server', 'mrt ref app')
})
test('Path "/headers" echoes request headers', async () => {
const response = await request(app).get('/headers').set('Random-Header', 'random')
expect(response.body.headers['random-header']).toBe('random')
})
test('Path "/cookie" sets cookie', () => {
return request(app)
.get('/cookie?name=test-cookie&value=test-value')
.expect('set-cookie', 'test-cookie=test-value; Path=/')
})
test('Path "/set-response-headers" sets response header', () => {
return request(app)
.get('/set-response-headers?header1=value1&header2=test-value')
.expect('header1', 'value1')
.expect('header2', 'test-value')
})
test('Path "/isolation" succeeds', async () => {
jest.spyOn(console, 'error')
lambdaMock.on(InvokeCommand).rejects(new AccessDeniedException())
s3Mock.on(GetObjectCommand).rejects(new AccessDenied())
logsMock.on(CreateLogStreamCommand).rejects(new AccessDeniedException())
const params = `FunctionName=name&Bucket=bucket&Key=key&logGroupName=lgName`
const response = await request(app).get(`/isolation?${params}`)
expect(response.body.origin).toBe(true)
expect(response.body.storage).toBe(true)
expect(response.body.logs).toBe(true)
})
test('Path "/isolation" fails', async () => {
jest.spyOn(console, 'error')
lambdaMock.on(InvokeCommand).resolves()
s3Mock.on(GetObjectCommand).resolves()
logsMock.on(CreateLogStreamCommand).resolves()
const params = `FunctionName=name&Bucket=bucket&Key=key&logGroupName=lgName`
const response = await request(app).get(`/isolation?${params}`)
expect(response.body.origin).toBe(false)
expect(response.body.storage).toBe(false)
expect(response.body.logs).toBe(false)
const errors = [
'Lambda isolation test failed!',
'S3 isolation test failed!',
'Log group isolation test failed!'
]
const calls = console.error.mock.calls.map((call) => call[0])
expect(errors.some((error) => calls.includes(error))).toBe(true)
})
test('Path "/ssr-shared" serves the example.json file', async () => {
const response = await request(app).get('/ssr-shared')
expect(response.body.message).toBe(
'This file is used in the E2E tests to verify that correct header values are set.'
)
})
test('Path "/streaming-large" returns streaming: false', async () => {
const response = await request(app).get('/streaming-large')
expect(response.status).toBe(200)
expect(response.body).toEqual({streaming: false})
})
})