-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
159 lines (125 loc) · 5.02 KB
/
Copy pathmain_test.go
File metadata and controls
159 lines (125 loc) · 5.02 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
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestHandler(t *testing.T) {
mockRieServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
defer r.Body.Close()
responseBody, _ := json.Marshal(map[string]interface{}{
"message": "Hello from Lambda!",
"requestedBody": string(body),
})
if err := json.NewEncoder(w).Encode(map[string]interface{}{
"statusCode": 200,
"headers": map[string]string{
"Content-Type": "application/json",
},
"body": string(responseBody),
}); err != nil {
http.Error(w, "Failed to encode mock response", http.StatusInternalServerError)
}
}))
defer mockRieServer.Close()
rieEndpoint = mockRieServer.URL
server := httptest.NewServer(http.HandlerFunc(lambdaUrlProxyHandler))
defer server.Close()
req, _ := http.NewRequest(http.MethodPost, server.URL+"/foo/bar?testkey=testvalue", strings.NewReader("test body"))
req.Header.Set("Custom-Header", "HeaderValue")
resp, err := server.Client().Do(req)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Verify status code
if resp.StatusCode != 200 {
t.Errorf("expected statusCode to be 200, got %d", resp.StatusCode)
}
// Verify headers
if contentType := resp.Header.Get("Content-Type"); contentType != "application/json" {
t.Errorf("expected Content-Type to be application/json, got %s", contentType)
}
// Verify response body
var responseBody map[string]interface{}
if err := json.Unmarshal(body, &responseBody); err != nil {
t.Fatalf("failed to unmarshal response body: %v", err)
}
// Verify message
if responseBody["message"] != "Hello from Lambda!" {
t.Errorf("expected message to be 'Hello from Lambda!', got %v", responseBody["message"])
}
// Verify request body
requestedBody := responseBody["requestedBody"].(string)
var requestedBodyJSON map[string]interface{}
if err := json.Unmarshal([]byte(requestedBody), &requestedBodyJSON); err != nil {
t.Fatalf("failed to unmarshal requestedBody: %v", err)
}
// Verify each field
if requestedBodyJSON["version"] != "2.0" {
t.Errorf("expected version to be '2.0', got %v", requestedBodyJSON["version"])
}
if requestedBodyJSON["routeKey"] != "$default" {
t.Errorf("expected routeKey to be '$default', got %v", requestedBodyJSON["routeKey"])
}
if requestedBodyJSON["rawPath"] != "/foo/bar" {
t.Errorf("expected rawPath to be '/foo/bar', got %v", requestedBodyJSON["rawPath"])
}
if requestedBodyJSON["rawQueryString"] != "testkey=testvalue" {
t.Errorf("expected rawQueryString to be 'testkey=testvalue', got %v", requestedBodyJSON["rawQueryString"])
}
if queryParams := requestedBodyJSON["queryStringParameters"].(map[string]interface{}); queryParams["testkey"] != "testvalue" {
t.Errorf("expected query parameter 'testkey' to be 'testvalue', got %v", queryParams["testkey"])
}
headers := requestedBodyJSON["headers"].(map[string]interface{})
if headers["Custom-Header"] != "HeaderValue" {
t.Errorf("expected header 'Custom-Header' to be 'HeaderValue', got %v", headers["Custom-Header"])
}
if headers["User-Agent"] != "Go-http-client/1.1" {
t.Errorf("expected header 'User-Agent' to be 'Go-http-client/1.1', got %v", headers["User-Agent"])
}
if requestedBodyJSON["body"] != "test body" {
t.Errorf("expected body to be 'test body', got %v", requestedBodyJSON["body"])
}
if requestedBodyJSON["isBase64Encoded"] != false {
t.Errorf("expected isBase64Encoded to be false, got %v", requestedBodyJSON["isBase64Encoded"])
}
requestContext := requestedBodyJSON["requestContext"].(map[string]interface{})
if requestContext["routeKey"] != "$default" {
t.Errorf("expected routeKey to be '$default', got %v", requestContext["routeKey"])
}
if requestContext["stage"] != "$default" {
t.Errorf("expected stage to be '$default', got %v", requestContext["stage"])
}
if _, ok := requestContext["time"].(string); !ok {
t.Errorf("expected time to be a string, got %v", requestContext["time"])
}
if _, ok := requestContext["timeEpoch"].(float64); !ok {
t.Errorf("expected timeEpoch to be a float64, got %v", requestContext["timeEpoch"])
}
if requestContext["domainName"] != req.Host {
t.Errorf("expected domainName to be '%s', got %v", req.Host, requestContext["domainName"])
}
httpContext := requestContext["http"].(map[string]interface{})
if httpContext["method"] != "POST" {
t.Errorf("expected method to be 'POST', got %v", httpContext["method"])
}
if httpContext["path"] != "/foo/bar" {
t.Errorf("expected path to be '/foo/bar', got %v", httpContext["path"])
}
if httpContext["protocol"] != "HTTP/1.1" {
t.Errorf("expected protocol to be 'HTTP/1.1', got %v", httpContext["protocol"])
}
if httpContext["userAgent"] != "Go-http-client/1.1" {
t.Errorf("expected userAgent to be 'Go-http-client/1.1', got %v", httpContext["userAgent"])
}
}