forked from darccio/gluo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgluo_test.go
132 lines (126 loc) · 3.72 KB
/
gluo_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
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
package gluo
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"github.com/aws/aws-lambda-go/events"
"net/http"
"os"
"testing"
)
func TestMain(m *testing.M) {
flag.Parse()
os.Setenv("_LAMBDA_SERVER_PORT", "3000")
os.Exit(m.Run())
}
func TestIsLambda(t *testing.T) {
if !IsLambda() {
t.Errorf("TestMain failed to set environment to simulate AWS Lambda")
}
}
const testRequest = `{
"body":"{\"name\":\"Gluo\"}",
"resource":"/{proxy+}",
"requestContext":{
"resourceId":"123456",
"apiId":"1234567890",
"resourcePath":"/{proxy+}",
"httpMethod":"POST",
"requestId":"c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"accountId":"123456789012",
"identity":{
"apiKey":null,
"userArn":null,
"cognitoAuthenticationType":null,
"caller":null,
"userAgent":"Custom User Agent String",
"user":null,
"cognitoIdentityPoolId":null,
"cognitoIdentityId":null,
"cognitoAuthenticationProvider":null,
"sourceIp":"127.0.0.1",
"accountId":null
},
"stage":"prod",
"X-Amzn-Trace-Id":"Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1"
},
"queryStringParameters":{
"foo":"bar"
},
"headers":{
"Via":"1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"Accept-Language":"en-US,en;q=0.8",
"CloudFront-Is-Desktop-Viewer":"true",
"CloudFront-Is-SmartTV-Viewer":"false",
"CloudFront-Is-Mobile-Viewer":"false",
"X-Forwarded-For":"127.0.0.1, 127.0.0.2",
"CloudFront-Viewer-Country":"US",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Upgrade-Insecure-Requests":"1",
"X-Forwarded-Port":"443",
"Host":"1234567890.execute-api.us-east-1.amazonaws.com",
"X-Forwarded-Proto":"https",
"X-Amz-Cf-Id":"cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"CloudFront-Is-Tablet-Viewer":"false",
"Cache-Control":"max-age=0",
"User-Agent":"Custom User Agent String",
"CloudFront-Forwarded-Proto":"https",
"Accept-Encoding":"gzip, deflate, sdch"
},
"pathParameters":{
"proxy":"path/to/resource"
},
"httpMethod":"POST",
"stageVariables":{
"baz":"qux"
},
"path":"/path/to/resource"
}`
type helloRequest struct {
Name string
}
func TestLambdaServe(t *testing.T) {
la := LambdaAdapter{
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
buffer := new(bytes.Buffer)
buffer.ReadFrom(r.Body)
r.Body.Close()
hr := helloRequest{}
err := json.Unmarshal(buffer.Bytes(), &hr)
if err != nil {
result := fmt.Sprintf("Sorry, I didn't understand you.")
w.Write([]byte(result))
return
}
if hr.Name == "" {
hr.Name = "stranger"
}
result := fmt.Sprintf("Hello, %s.", hr.Name)
w.Write([]byte(result))
}),
}
req := events.APIGatewayProxyRequest{}
if err := json.Unmarshal([]byte(testRequest), &req); err != nil {
t.Errorf("unexpected error on json.Unmarshal: %v", err)
}
res, err := la.Handle(context.TODO(), req)
if err != nil {
t.Error("LambdaHandler.Handle must return nil")
}
if res.StatusCode != 200 {
t.Errorf("expected HTTP status code 200, not '%d'", res.StatusCode)
}
if res.Body != "Hello, Gluo." {
t.Errorf("expected Body 'Hello, Gluo.', not '%s'", res.Body)
}
if res.IsBase64Encoded {
t.Error("unexpected base 64 encoding")
}
contentType, _ := res.Headers["Content-Type"]
if contentType != "application/json" {
t.Errorf("expected Content-Type header 'application/json', not '%s'", contentType)
}
}