Skip to content

Commit

Permalink
Merge pull request #5 from twharmon/test-handler
Browse files Browse the repository at this point in the history
More tests
  • Loading branch information
twharmon authored Apr 4, 2022
2 parents 55f532d + de714ad commit e44583c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions golamb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ type Config struct {
}

func Start(handlerFunc WrappedHandler, config ...*Config) {
h := getHandler(handlerFunc, config...)
lambda.StartHandler(h)
}

func getHandler(handlerFunc WrappedHandler, config ...*Config) lambda.Handler {
cfg := getConfig(config...)
h := &handler{
return &handler{
cfg: cfg,
handler: func(r *events.APIGatewayV2HTTPRequest) (resp *events.APIGatewayV2HTTPResponse, err error) {
ctx := &handlerContext{
Expand All @@ -32,7 +37,6 @@ func Start(handlerFunc WrappedHandler, config ...*Config) {
return handlerFunc(ctx).Respond()
},
}
lambda.StartHandler(h)
}

func getConfig(configs ...*Config) *Config {
Expand Down
27 changes: 27 additions & 0 deletions golamb_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package golamb

import (
"context"
"encoding/json"
"errors"
"net/http"
"reflect"
Expand All @@ -9,6 +11,31 @@ import (
"github.com/aws/aws-lambda-go/events"
)

func TestGetHandler(t *testing.T) {
f := func(c Context) Responder {
return c.Response(http.StatusOK)
}
h := getHandler(f)
payload, err := json.Marshal(&events.APIGatewayV2HTTPRequest{})
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
resp, err := h.Invoke(context.Background(), payload)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
var got events.APIGatewayV2HTTPResponse
if err := json.Unmarshal(resp, &got); err != nil {
t.Fatalf("unexpected err: %s", err)
}
want := events.APIGatewayV2HTTPResponse{
StatusCode: http.StatusOK,
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestGetConfigEmpty(t *testing.T) {
want := &Config{
AWSServiceProvider: &AWSServiceProviderConfig{},
Expand Down

0 comments on commit e44583c

Please sign in to comment.