-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_test.go
130 lines (111 loc) · 4.11 KB
/
http_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
package g8_test
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/stretchr/testify/assert"
"github.com/JSainsburyPLC/g8"
)
func TestLambdaAdapterGetMethod(t *testing.T) {
l := g8.LambdaHandler{
Handler: func(ctx *g8.APIGatewayProxyContext) error {
ctx.Response = events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{"Content-Type": "text/plain"},
MultiValueHeaders: map[string][]string{"Set-Cookie": {"cookie1", "cookie2"}},
Body: "success",
}
return nil
},
Method: http.MethodGet,
PathPattern: "/test/url/path/{var1}/{var2}",
}
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/test/url/path/value1/value2", nil)
r.Header.Set("Content-Type", "text/plain")
g8.LambdaAdapter(l)(w, r)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "text/plain", w.Header().Get("Content-Type"))
assert.Equal(t, "cookie1,cookie2", w.Header().Get("Set-Cookie"))
assert.Equal(t, "success", w.Body.String())
}
func TestLambdaAdapterPostMethod(t *testing.T) {
l := g8.LambdaHandler{
Handler: func(ctx *g8.APIGatewayProxyContext) error {
ctx.Response = events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Headers: map[string]string{"Content-Type": "text/plain"},
MultiValueHeaders: map[string][]string{"Set-Cookie": {"cookie1", "cookie2"}},
Body: "success",
}
return nil
},
Method: http.MethodPost,
PathPattern: "/test/url/path/{var1}/{var2}",
}
reqBody := strings.NewReader(`{"key":"value"}`)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodPost, "/test/url/path/value1/value2", reqBody)
r.Header.Set("Content-Type", "text/plain")
g8.LambdaAdapter(l)(w, r)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "text/plain", w.Header().Get("Content-Type"))
assert.Equal(t, "cookie1,cookie2", w.Header().Get("Set-Cookie"))
assert.Equal(t, "success", w.Body.String())
}
func TestLambdaAdapter_without_content_type(t *testing.T) {
l := g8.LambdaHandler{
Handler: func(ctx *g8.APIGatewayProxyContext) error {
ctx.Response = events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
MultiValueHeaders: map[string][]string{"Set-Cookie": {"cookie1", "cookie2"}},
Body: `{"message":"success"}`,
}
return nil
},
Method: http.MethodGet,
PathPattern: "/test/url/path/{var1}/{var2}",
}
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/test/url/path/value1/value2", nil)
g8.LambdaAdapter(l)(w, r)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, "cookie1,cookie2", w.Header().Get("Set-Cookie"))
assert.Equal(t, `{"message":"success"}`, w.Body.String())
}
func TestLambdaAdapter_g8_error(t *testing.T) {
l := g8.LambdaHandler{
Handler: func(ctx *g8.APIGatewayProxyContext) error {
return g8.ErrInternalServer
},
Method: http.MethodGet,
PathPattern: "/test/url/path",
}
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/test/url/path/var1/var2", nil)
r.Header.Set("Content-Type", "application/json")
g8.LambdaAdapter(l)(w, r)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, `{"code":"INTERNAL_SERVER_ERROR","detail":"Internal server error"}`, w.Body.String())
}
func TestLambdaAdapter_generic_error(t *testing.T) {
l := g8.LambdaHandler{
Handler: func(ctx *g8.APIGatewayProxyContext) error {
return fmt.Errorf("generic error")
},
Method: http.MethodGet,
PathPattern: "/test/url/path",
}
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/test/url/path/var1/var2", nil)
r.Header.Set("Content-Type", "application/json")
g8.LambdaAdapter(l)(w, r)
assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
assert.Equal(t, `{"code":"INTERNAL_SERVER_ERROR","detail":"Internal server error"}`, w.Body.String())
}