-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlh_test.go
127 lines (121 loc) · 3.22 KB
/
lh_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
package lh
import (
"encoding/json"
"net"
"net/http"
"net/rpc"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda/messages"
)
func TestInvoke(t *testing.T) {
s := &service{http: http.DefaultServeMux}
req := new(messages.InvokeRequest)
req.Payload = []byte(payload)
res := new(messages.InvokeResponse)
err := s.Invoke(req, res)
if err != nil {
t.Fatal(err)
}
response := events.APIGatewayProxyResponse{}
if err := json.Unmarshal(res.Payload, &response); err != nil {
t.Fatal(err)
}
if response.StatusCode != 404 {
t.Fatalf("Expected 404 StatusCode, got %v", response.StatusCode)
}
if !response.IsBase64Encoded {
t.Fatal("Response not base64 encoded.")
}
}
func TestRoundTrip(t *testing.T) {
s := &service{http: http.DefaultServeMux}
err := rpc.RegisterName("Function", s)
if err != nil {
t.Fatal(err)
}
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
go rpc.Accept(l)
c, err := rpc.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatal(err)
}
defer c.Close()
req := new(messages.InvokeRequest)
req.Payload = []byte(payload)
res := new(messages.InvokeResponse)
err = c.Call("Function.Invoke", req, res)
if err != nil {
t.Fatal(err)
}
response := events.APIGatewayProxyResponse{}
if err := json.Unmarshal(res.Payload, &response); err != nil {
t.Fatal(err)
}
if response.StatusCode != 404 {
t.Fatalf("Expected 404 StatusCode, got %v", response.StatusCode)
}
if !response.IsBase64Encoded {
t.Fatal("Response not base64 encoded.")
}
}
const payload = `{
"body": "{\"test\":\"body\"}",
"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"
},
"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"
}`