Skip to content

Commit 25bba89

Browse files
committed
test: add tests for misc functions and functionurl
1 parent f9e3ebf commit 25bba89

File tree

3 files changed

+224
-1
lines changed

3 files changed

+224
-1
lines changed

functionurl_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package aws_lambda_go_http_adapter
2+
3+
import (
4+
"context"
5+
"encoding/base64"
6+
"encoding/json"
7+
"github.com/aws/aws-lambda-go/events"
8+
"github.com/gofiber/fiber/v2"
9+
"github.com/its-felix/aws-lambda-go-http-adapter/adapter"
10+
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
11+
"github.com/labstack/echo/v4"
12+
"io"
13+
"net/http"
14+
"reflect"
15+
"testing"
16+
)
17+
18+
func newFunctionURLRequest() events.LambdaFunctionURLRequest {
19+
return events.LambdaFunctionURLRequest{
20+
Version: "2.0",
21+
RawPath: "/example",
22+
RawQueryString: "key=value",
23+
Cookies: []string{},
24+
Headers: map[string]string{},
25+
QueryStringParameters: map[string]string{"key": "value"},
26+
RequestContext: events.LambdaFunctionURLRequestContext{
27+
AccountID: "012345678912",
28+
RequestID: "abcdefg",
29+
Authorizer: nil,
30+
APIID: "0dhg9709da0dhg9709da0dhg9709da",
31+
DomainName: "0dhg9709da0dhg9709da0dhg9709da.lambda-url.eu-central-1.on.aws",
32+
DomainPrefix: "",
33+
Time: "",
34+
TimeEpoch: 0,
35+
HTTP: events.LambdaFunctionURLRequestContextHTTPDescription{
36+
Method: "POST",
37+
Path: "/example",
38+
Protocol: "",
39+
SourceIP: "127.0.0.1",
40+
UserAgent: "Go-http-client/1.1",
41+
},
42+
},
43+
Body: base64.StdEncoding.EncodeToString([]byte("hello world")),
44+
IsBase64Encoded: true,
45+
}
46+
}
47+
48+
func newVanillaAdapter() handler.AdapterFunc {
49+
mux := http.NewServeMux()
50+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
51+
w.Header().Set("Content-Type", "application/json")
52+
w.WriteHeader(http.StatusOK)
53+
54+
result := make(map[string]string)
55+
result["Method"] = r.Method
56+
result["URL"] = r.URL.String()
57+
result["RemoteAddr"] = r.RemoteAddr
58+
59+
defer r.Body.Close()
60+
b, _ := io.ReadAll(r.Body)
61+
result["Body"] = string(b)
62+
63+
enc := json.NewEncoder(w)
64+
_ = enc.Encode(result)
65+
})
66+
67+
return adapter.NewVanillaAdapter(mux)
68+
}
69+
70+
func newEchoAdapter() handler.AdapterFunc {
71+
app := echo.New()
72+
app.Any("*", func(c echo.Context) error {
73+
r := c.Request()
74+
75+
result := make(map[string]string)
76+
result["Method"] = r.Method
77+
result["URL"] = r.URL.String()
78+
result["RemoteAddr"] = r.RemoteAddr
79+
80+
defer r.Body.Close()
81+
b, _ := io.ReadAll(r.Body)
82+
result["Body"] = string(b)
83+
84+
c.Response().Header().Set("Content-Type", "application/json")
85+
86+
return c.JSON(http.StatusOK, result)
87+
})
88+
89+
return adapter.NewEchoAdapter(app)
90+
}
91+
92+
func newFiberAdapter() handler.AdapterFunc {
93+
app := fiber.New()
94+
app.All("*", func(ctx *fiber.Ctx) error {
95+
result := make(map[string]string)
96+
result["Method"] = ctx.Method()
97+
result["URL"] = ctx.Request().URI().String()
98+
result["RemoteAddr"] = ctx.IP()
99+
result["Body"] = string(ctx.Body())
100+
101+
return ctx.JSON(result)
102+
})
103+
104+
return adapter.NewFiberAdapter(app)
105+
}
106+
107+
func TestFunctionURLGET(t *testing.T) {
108+
adapters := map[string]handler.AdapterFunc{
109+
"vanilla": newVanillaAdapter(),
110+
"echo": newEchoAdapter(),
111+
}
112+
113+
for name, a := range adapters {
114+
t.Run(name, func(t *testing.T) {
115+
h := handler.NewFunctionURLHandler(a)
116+
117+
req := newFunctionURLRequest()
118+
res, err := h(context.Background(), req)
119+
if err != nil {
120+
t.Error(err)
121+
}
122+
123+
if res.StatusCode != http.StatusOK {
124+
t.Error("expected status to be 200")
125+
}
126+
127+
if res.Headers["Content-Type"] != "application/json" {
128+
t.Error("expected Content-Type to be application/json")
129+
}
130+
131+
if res.IsBase64Encoded {
132+
t.Error("expected body not to be base64 encoded")
133+
}
134+
135+
body := make(map[string]string)
136+
_ = json.Unmarshal([]byte(res.Body), &body)
137+
138+
expectedBody := map[string]string{
139+
"Method": "POST",
140+
"URL": "https://0dhg9709da0dhg9709da0dhg9709da.lambda-url.eu-central-1.on.aws/example?key=value",
141+
"RemoteAddr": "127.0.0.1:http",
142+
"Body": "hello world",
143+
}
144+
145+
if !reflect.DeepEqual(body, expectedBody) {
146+
t.Error("request/response didnt match")
147+
}
148+
})
149+
}
150+
}

handler/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
type httpContextKey string
99

10-
var sourceEventContextKey httpContextKey = "github.com/its-felix/aws-lambda-go-http-adapter/api::sourceEventContextKey"
10+
var sourceEventContextKey httpContextKey = "github.com/its-felix/aws-lambda-go-http-adapter/handler/api::sourceEventContextKey"
1111

1212
type AdapterFunc func(ctx context.Context, r *http.Request, w http.ResponseWriter) error
1313
type HandlerFunc[In any, Out any] func(ctx context.Context, event In, adapter AdapterFunc) (Out, error)

handler/api_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package handler
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
"net/http/httptest"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func newTestHandler(handlerFunc http.HandlerFunc) func(context.Context, *http.Request) (*http.Response, error) {
13+
return NewHandler(
14+
func(ctx context.Context, event *http.Request, adapter AdapterFunc) (*http.Response, error) {
15+
w := httptest.NewRecorder()
16+
17+
if err := adapter(ctx, event, w); err != nil {
18+
return nil, err
19+
}
20+
21+
return w.Result(), nil
22+
},
23+
func(ctx context.Context, r *http.Request, w http.ResponseWriter) error {
24+
handlerFunc(w, r.WithContext(ctx))
25+
return nil
26+
},
27+
)
28+
}
29+
30+
func TestGetSourceEvent(t *testing.T) {
31+
var caughtRequest *http.Request
32+
33+
handler := newTestHandler(func(w http.ResponseWriter, r *http.Request) {
34+
event := GetSourceEvent(r.Context()).(*http.Request)
35+
caughtRequest = event
36+
})
37+
38+
req := httptest.NewRequest(http.MethodGet, "/", strings.NewReader(""))
39+
res, err := handler(context.Background(), req)
40+
41+
if caughtRequest != req {
42+
t.Error("GetSourceEvent returned the wrong value")
43+
}
44+
45+
if res == nil {
46+
t.Error("response should not be nil")
47+
}
48+
49+
if err != nil {
50+
t.Error("expected err to be nil")
51+
}
52+
}
53+
54+
func TestWrapWithRecover(t *testing.T) {
55+
handler := newTestHandler(func(w http.ResponseWriter, r *http.Request) {
56+
panic("test panic value")
57+
})
58+
59+
handler = WrapWithRecover(handler, func(ctx context.Context, event *http.Request, panicValue any) (*http.Response, error) {
60+
return nil, errors.New(panicValue.(string))
61+
})
62+
63+
req := httptest.NewRequest(http.MethodGet, "/", strings.NewReader(""))
64+
res, err := handler(context.Background(), req)
65+
66+
if res != nil {
67+
t.Error("expected nil response")
68+
}
69+
70+
if err.Error() != "test panic value" {
71+
t.Error("expected the handler to return an error 'test panic value'")
72+
}
73+
}

0 commit comments

Comments
 (0)