mirror of https://github.com/knative/func.git
example go templates for HTTP and CloudEvents
This commit is contained in:
parent
b5ffc25b5c
commit
6d720f44f2
|
@ -0,0 +1,5 @@
|
||||||
|
module function
|
||||||
|
|
||||||
|
go 1.13
|
||||||
|
|
||||||
|
require github.com/cloudevents/sdk-go/v2 v2.0.0
|
|
@ -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
|
||||||
|
}
|
|
@ -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?")
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
module function
|
||||||
|
|
||||||
|
go 1.13
|
|
@ -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
|
||||||
|
}
|
|
@ -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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue