example go templates for HTTP and CloudEvents

This commit is contained in:
Luke K 2020-06-08 04:59:31 +00:00
parent b5ffc25b5c
commit 6d720f44f2
No known key found for this signature in database
GPG Key ID: 4896F75BAF2E1966
7 changed files with 122 additions and 0 deletions

0
templates/README.md Normal file
View File

View File

@ -0,0 +1,5 @@
module function
go 1.13
require github.com/cloudevents/sdk-go/v2 v2.0.0

View File

@ -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
}

View File

@ -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?")
}
}

3
templates/go/http/go.mod Normal file
View File

@ -0,0 +1,3 @@
module function
go 1.13

View File

@ -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
}

View File

@ -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")
}
}