|
| 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 | +} |
0 commit comments