From 6d720f44f26a2eb851c787b3c70b149fd60e3cc0 Mon Sep 17 00:00:00 2001 From: Luke K Date: Mon, 8 Jun 2020 04:59:31 +0000 Subject: [PATCH] example go templates for HTTP and CloudEvents --- templates/README.md | 0 templates/go/events/go.mod | 5 ++++ templates/go/events/handle.go | 32 ++++++++++++++++++++++++++ templates/go/events/handle_test.go | 32 ++++++++++++++++++++++++++ templates/go/http/go.mod | 3 +++ templates/go/http/handle.go | 13 +++++++++++ templates/go/http/handle_test.go | 37 ++++++++++++++++++++++++++++++ 7 files changed, 122 insertions(+) create mode 100644 templates/README.md create mode 100644 templates/go/events/go.mod create mode 100644 templates/go/events/handle.go create mode 100644 templates/go/events/handle_test.go create mode 100644 templates/go/http/go.mod create mode 100644 templates/go/http/handle.go create mode 100644 templates/go/http/handle_test.go diff --git a/templates/README.md b/templates/README.md new file mode 100644 index 00000000..e69de29b diff --git a/templates/go/events/go.mod b/templates/go/events/go.mod new file mode 100644 index 00000000..9b6de060 --- /dev/null +++ b/templates/go/events/go.mod @@ -0,0 +1,5 @@ +module function + +go 1.13 + +require github.com/cloudevents/sdk-go/v2 v2.0.0 diff --git a/templates/go/events/handle.go b/templates/go/events/handle.go new file mode 100644 index 00000000..c6c170d0 --- /dev/null +++ b/templates/go/events/handle.go @@ -0,0 +1,32 @@ +package function + +import ( + "context" + "fmt" + "os" + + cloudevents "github.com/cloudevents/sdk-go/v2" +) + +// Handle a CloudEvent. +// Supported function signatures: +// func() +// func() error +// func(context.Context) +// func(context.Context) error +// func(cloudevents.Event) +// func(cloudevents.Event) error +// func(context.Context, cloudevents.Event) +// func(context.Context, cloudevents.Event) error +// func(cloudevents.Event, *cloudevents.EventResponse) +// func(cloudevents.Event, *cloudevents.EventResponse) error +// func(context.Context, cloudevents.Event, *cloudevents.EventResponse) +// func(context.Context, cloudevents.Event, *cloudevents.EventResponse) error +func Handle(ctx context.Context, event cloudevents.Event) error { + if err := event.Validate(); err != nil { + fmt.Fprintf(os.Stderr, "invalid event received. %v", err) + return err + } + fmt.Printf("%v\n", event) + return nil +} diff --git a/templates/go/events/handle_test.go b/templates/go/events/handle_test.go new file mode 100644 index 00000000..492e2ef3 --- /dev/null +++ b/templates/go/events/handle_test.go @@ -0,0 +1,32 @@ +package function + +import ( + "context" + "testing" + + "github.com/cloudevents/sdk-go/v2/event" +) + +// TestHandle ensures that Handle accepts a valid CloudEvent without error. +func TestHandle(t *testing.T) { + // A minimal, but valid, event. + event := event.New() + event.SetID("TEST-EVENT-01") + event.SetType("com.example.event.test") + event.SetSource("http://localhost:8080/") + + // Invoke the defined handler. + if err := Handle(context.Background(), event); err != nil { + t.Fatal(err) + } +} + +// TestHandleInvalid ensures that an invalid input event generates an error. +func TestInvalidInput(t *testing.T) { + invalidEvent := event.New() // missing required fields + + // Attempt to handle the invalid event, ensuring that the handler validats events. + if err := Handle(context.Background(), invalidEvent); err == nil { + t.Fatalf("handler did not generate error on invalid event. Missing .Validate() check?") + } +} diff --git a/templates/go/http/go.mod b/templates/go/http/go.mod new file mode 100644 index 00000000..e66a0aa3 --- /dev/null +++ b/templates/go/http/go.mod @@ -0,0 +1,3 @@ +module function + +go 1.13 diff --git a/templates/go/http/handle.go b/templates/go/http/handle.go new file mode 100644 index 00000000..5c192d6e --- /dev/null +++ b/templates/go/http/handle.go @@ -0,0 +1,13 @@ +package function + +import ( + "context" + "net/http" +) + +// Handle an HTTP Request. +func Handle(ctx context.Context, res http.ResponseWriter, req *http.Request) error { + res.Header().Add("Content-Type", "text/plain") + res.Write([]byte("OK\n")) + return nil +} diff --git a/templates/go/http/handle_test.go b/templates/go/http/handle_test.go new file mode 100644 index 00000000..4b450dab --- /dev/null +++ b/templates/go/http/handle_test.go @@ -0,0 +1,37 @@ +package function + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +// TestHandle ensures that Handle executes without error, returns 2000 and sets +// and sets a content-type. +func TestHandle(t *testing.T) { + var ( + w = httptest.NewRecorder() + req = httptest.NewRequest("GET", "http://example.com/test", nil) + res *http.Response + err error + ) + + // Invoke the Handler via a standard Go http.Handler + func(w http.ResponseWriter, req *http.Request) { + err = Handle(context.Background(), w, req) + }(w, req) + res = w.Result() + + // Assert postconditions + if err != nil { + t.Fatalf("unepected error in Handle: %v", err) + } + if res.StatusCode != 200 { + t.Fatalf("unexpected response code: %v", res.StatusCode) + } + if res.Header.Get("Content-Type") == "" { + t.Fatal("Handler did not set a content type for the response") + } + +}