-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathappctxutil_test.go
More file actions
227 lines (188 loc) · 7.5 KB
/
appctxutil_test.go
File metadata and controls
227 lines (188 loc) · 7.5 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package appctx
import (
"net/http/httptest"
"strings"
"testing"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/fatalerror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aws/aws-lambda-runtime-interface-emulator/internal/lambda/interop"
)
func runTestRequestWithUserAgent(t *testing.T, userAgent string, expectedRuntimeRelease string) {
// Simple User_Agent passed.
// GIVEN
req := httptest.NewRequest("", "/", nil)
req.Header.Set("User-Agent", userAgent)
request := RequestWithAppCtx(req, NewApplicationContext())
appCtx := request.Context().Value(ReqCtxApplicationContextKey).(ApplicationContext)
// DO
ok := UpdateAppCtxWithRuntimeRelease(request, appCtx)
//ASSERT
assert.True(t, ok)
ctxRuntimeRelease, ok := appCtx.Load(AppCtxRuntimeReleaseKey)
assert.True(t, ok)
assert.Equal(t, expectedRuntimeRelease, ctxRuntimeRelease, "failed to extract runtime_release token")
}
func TestCreateRuntimeReleaseFromRequest(t *testing.T) {
tests := map[string]struct {
userAgentHeader string
lambdaRuntimeFeaturesHeader string
expectedRuntimeRelease string
}{
"No User-Agent header": {
userAgentHeader: "",
lambdaRuntimeFeaturesHeader: "httpcl/2.0 execwr",
expectedRuntimeRelease: "Unknown (httpcl/2.0 execwr)",
},
"No Lambda-Runtime-Features header": {
userAgentHeader: "Node.js/14.16.0",
lambdaRuntimeFeaturesHeader: "",
expectedRuntimeRelease: "Node.js/14.16.0",
},
"Lambda-Runtime-Features header with additional spaces": {
userAgentHeader: "Node.js/14.16.0",
lambdaRuntimeFeaturesHeader: "httpcl/2.0 execwr",
expectedRuntimeRelease: "Node.js/14.16.0 (httpcl/2.0 execwr)",
},
"Lambda-Runtime-Features header with special characters": {
userAgentHeader: "Node.js/14.16.0",
lambdaRuntimeFeaturesHeader: "httpcl/2.0@execwr-1 abcd?efg nodewr/(4.33)) nodewr/4.3",
expectedRuntimeRelease: "Node.js/14.16.0 (httpcl/2.0@execwr-1 abcd?efg nodewr/4.33 nodewr/4.3)",
},
"Lambda-Runtime-Features header with long Lambda-Runtime-Features header": {
userAgentHeader: "Node.js/14.16.0",
lambdaRuntimeFeaturesHeader: strings.Repeat("abcdef ", MaxRuntimeReleaseLength/7),
expectedRuntimeRelease: "Node.js/14.16.0 (" + strings.Repeat("abcdef ", (MaxRuntimeReleaseLength-18-6)/7) + "abcdef)",
},
"Lambda-Runtime-Features header with long Lambda-Runtime-Features header with UTF-8 characters": {
userAgentHeader: "Node.js/14.16.0",
lambdaRuntimeFeaturesHeader: strings.Repeat("我爱亚马逊 ", MaxRuntimeReleaseLength/16),
expectedRuntimeRelease: "Node.js/14.16.0 (" + strings.Repeat("我爱亚马逊 ", (MaxRuntimeReleaseLength-18-15)/16) + "我爱亚马逊)",
},
}
for _, tc := range tests {
req := httptest.NewRequest("", "/", nil)
if tc.userAgentHeader != "" {
req.Header.Set("User-Agent", tc.userAgentHeader)
}
if tc.lambdaRuntimeFeaturesHeader != "" {
req.Header.Set("Lambda-Runtime-Features", tc.lambdaRuntimeFeaturesHeader)
}
appCtx := NewApplicationContext()
request := RequestWithAppCtx(req, appCtx)
UpdateAppCtxWithRuntimeRelease(request, appCtx)
runtimeRelease := GetRuntimeRelease(appCtx)
assert.LessOrEqual(t, len(runtimeRelease), MaxRuntimeReleaseLength)
assert.Equal(t, tc.expectedRuntimeRelease, runtimeRelease)
}
}
func TestUpdateAppCtxWithRuntimeRelease(t *testing.T) {
type pair struct {
in, wanted string
}
pairs := []pair{
{"Mozilla/5.0", "Mozilla/5.0"},
{"Mozilla/6.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0", "Mozilla/6.0"},
}
for _, p := range pairs {
runTestRequestWithUserAgent(t, p.in, p.wanted)
}
}
func TestUpdateAppCtxWithRuntimeReleaseWithoutUserAgent(t *testing.T) {
// GIVEN
// No User_Agent passed.
request := RequestWithAppCtx(httptest.NewRequest("", "/", nil), NewApplicationContext())
appCtx := request.Context().Value(ReqCtxApplicationContextKey).(ApplicationContext)
// DO
ok := UpdateAppCtxWithRuntimeRelease(request, appCtx)
// ASSERT
assert.False(t, ok)
_, ok = appCtx.Load(AppCtxRuntimeReleaseKey)
assert.False(t, ok)
}
func TestUpdateAppCtxWithRuntimeReleaseWithBlankUserAgent(t *testing.T) {
// GIVEN
req := httptest.NewRequest("", "/", nil)
req.Header.Set("User-Agent", " ")
request := RequestWithAppCtx(req, NewApplicationContext())
appCtx := request.Context().Value(ReqCtxApplicationContextKey).(ApplicationContext)
// DO
ok := UpdateAppCtxWithRuntimeRelease(request, appCtx)
// ASSERT
assert.False(t, ok)
_, ok = appCtx.Load(AppCtxRuntimeReleaseKey)
assert.False(t, ok)
}
func TestUpdateAppCtxWithRuntimeReleaseWithLambdaRuntimeFeatures(t *testing.T) {
// GIVEN
// Simple LambdaRuntimeFeatures passed.
req := httptest.NewRequest("", "/", nil)
req.Header.Set("User-Agent", "Node.js/14.16.0")
req.Header.Set("Lambda-Runtime-Features", "httpcl/2.0 execwr nodewr/4.3")
request := RequestWithAppCtx(req, NewApplicationContext())
appCtx := request.Context().Value(ReqCtxApplicationContextKey).(ApplicationContext)
// DO
ok := UpdateAppCtxWithRuntimeRelease(request, appCtx)
//ASSERT
assert.True(t, ok, "runtime_release updated based only on User-Agent and valid features")
ctxRuntimeRelease, ok := appCtx.Load(AppCtxRuntimeReleaseKey)
assert.True(t, ok)
assert.Equal(t, "Node.js/14.16.0 (httpcl/2.0 execwr nodewr/4.3)", ctxRuntimeRelease)
}
// Test that RAPID allows updating runtime_release only once
func TestUpdateAppCtxWithRuntimeReleaseMultipleTimes(t *testing.T) {
// GIVEN
firstValue := "Value1"
secondValue := "Value2"
req := httptest.NewRequest("", "/", nil)
req.Header.Set("User-Agent", firstValue)
request := RequestWithAppCtx(req, NewApplicationContext())
appCtx := request.Context().Value(ReqCtxApplicationContextKey).(ApplicationContext)
// DO
ok := UpdateAppCtxWithRuntimeRelease(request, appCtx)
// ASSERT
assert.True(t, ok)
ctxRuntimeRelease, ok := appCtx.Load(AppCtxRuntimeReleaseKey)
assert.True(t, ok)
assert.Equal(t, firstValue, ctxRuntimeRelease)
// GIVEN
req.Header.Set("User-Agent", secondValue)
// DO
ok = UpdateAppCtxWithRuntimeRelease(request, appCtx)
// ASSERT
assert.False(t, ok, "failed to prevent second update of runtime_release")
ctxRuntimeRelease, ok = appCtx.Load(AppCtxRuntimeReleaseKey)
assert.True(t, ok)
assert.Equal(t, firstValue, ctxRuntimeRelease, "failed to prevent second update of runtime_release")
}
func TestFirstFatalError(t *testing.T) {
appCtx := NewApplicationContext()
_, found := LoadFirstFatalError(appCtx)
require.False(t, found)
StoreFirstFatalError(appCtx, fatalerror.AgentCrash)
v, found := LoadFirstFatalError(appCtx)
require.True(t, found)
require.Equal(t, fatalerror.AgentCrash, v)
StoreFirstFatalError(appCtx, fatalerror.AgentExitError)
v, found = LoadFirstFatalError(appCtx)
require.True(t, found)
require.Equal(t, fatalerror.AgentCrash, v)
}
func TestStoreLoadInitType(t *testing.T) {
appCtx := NewApplicationContext()
initType := LoadInitType(appCtx)
assert.Equal(t, Init, initType)
StoreInitType(appCtx, true)
initType = LoadInitType(appCtx)
assert.Equal(t, InitCaching, initType)
}
func TestStoreLoadSandboxType(t *testing.T) {
appCtx := NewApplicationContext()
sandboxType := LoadSandboxType(appCtx)
assert.Equal(t, interop.SandboxClassic, sandboxType)
StoreSandboxType(appCtx, interop.SandboxPreWarmed)
sandboxType = LoadSandboxType(appCtx)
assert.Equal(t, interop.SandboxPreWarmed, sandboxType)
}