@@ -7,12 +7,14 @@ import (
7
7
"encoding/json"
8
8
"errors"
9
9
"fmt"
10
+ "os"
10
11
"testing"
11
12
"time"
12
13
13
14
"github.com/aws/aws-lambda-go/lambda/messages"
14
15
"github.com/aws/aws-lambda-go/lambdacontext"
15
16
"github.com/stretchr/testify/assert"
17
+ "github.com/stretchr/testify/require"
16
18
)
17
19
18
20
type testWrapperHandler func (ctx context.Context , input []byte ) (interface {}, error )
@@ -140,3 +142,65 @@ func TestContextPlumbing(t *testing.T) {
140
142
`
141
143
assert .JSONEq (t , expected , string (response .Payload ))
142
144
}
145
+
146
+ func TestXAmznTraceID (t * testing.T ) {
147
+ type XRayResponse struct {
148
+ Env string
149
+ Ctx string
150
+ }
151
+
152
+ srv := & Function {handler : testWrapperHandler (
153
+ func (ctx context.Context , input []byte ) (interface {}, error ) {
154
+ return & XRayResponse {
155
+ Env : os .Getenv ("_X_AMZN_TRACE_ID" ),
156
+ Ctx : ctx .Value ("x-amzn-trace-id" ).(string ),
157
+ }, nil
158
+ },
159
+ )}
160
+
161
+ sequence := []struct {
162
+ Input string
163
+ Expected string
164
+ }{
165
+ {
166
+ "" ,
167
+ `{"Env": "", "Ctx": ""}` ,
168
+ },
169
+ {
170
+ "dummyid" ,
171
+ `{"Env": "dummyid", "Ctx": "dummyid"}` ,
172
+ },
173
+ {
174
+ "" ,
175
+ `{"Env": "", "Ctx": ""}` ,
176
+ },
177
+ {
178
+ "123dummyid" ,
179
+ `{"Env": "123dummyid", "Ctx": "123dummyid"}` ,
180
+ },
181
+ {
182
+ "" ,
183
+ `{"Env": "", "Ctx": ""}` ,
184
+ },
185
+ {
186
+ "" ,
187
+ `{"Env": "", "Ctx": ""}` ,
188
+ },
189
+ {
190
+ "567" ,
191
+ `{"Env": "567", "Ctx": "567"}` ,
192
+ },
193
+ {
194
+ "hihihi" ,
195
+ `{"Env": "hihihi", "Ctx": "hihihi"}` ,
196
+ },
197
+ }
198
+
199
+ for i , test := range sequence {
200
+ var response messages.InvokeResponse
201
+ err := srv .Invoke (& messages.InvokeRequest {XAmznTraceId : test .Input }, & response )
202
+ require .NoError (t , err , "failed test sequence[%d]" , i )
203
+ assert .JSONEq (t , test .Expected , string (response .Payload ), "failed test sequence[%d]" , i )
204
+ }
205
+
206
+ }
0 commit comments