forked from darccio/gluo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
67 lines (63 loc) · 1.7 KB
/
request_test.go
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
package gluo
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"testing"
)
func TestBase64Body(t *testing.T) {
var (
expected = "Hello world!"
body = base64.StdEncoding.EncodeToString([]byte(expected))
buffer bytes.Buffer
)
e := events.APIGatewayProxyRequest{
IsBase64Encoded: true,
Body: body,
}
r, err := getBodyReader(e)
if err != nil {
t.Errorf("unexpected error on getBodyReader: %v", err)
}
_, err = buffer.ReadFrom(r)
if err != nil {
t.Errorf("unexpected error reading body in base 64 from getBodyReader: %v", err)
}
result := buffer.String()
if result != expected {
t.Errorf("expected '%s', got '%s'", expected, result)
}
}
func TestWrongBase64Body(t *testing.T) {
var (
body = "Hello world!"
buffer bytes.Buffer
)
e := events.APIGatewayProxyRequest{
IsBase64Encoded: true,
Body: body,
}
r, err := getBodyReader(e)
if err != nil {
t.Errorf("unexpected error on getBodyReader: %v", err)
}
_, err = buffer.ReadFrom(r)
if err == nil {
t.Errorf("expected error reading body in base 64 from getBodyReader, got nil")
}
}
func TestXRayHeader(t *testing.T) {
event := events.APIGatewayProxyRequest{}
json.Unmarshal([]byte(testRequest), &event)
// https://github.com/aws/aws-lambda-go/blob/d050bf32adc65f57141c11e49a34b67610a7c45d/lambda/function.go#L53
ctx := context.WithValue(context.Background(), "x-amzn-trace-id", "Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1")
req, err := request(ctx, event)
if err != nil {
t.Errorf("unexpected error on request: %v", err)
}
if req.Header.Get("X-Amzn-Trace-Id") == "" {
t.Errorf("expected X-Amzn-Trace-Id injected but it wasn't found")
}
}