From 608798c48cd2ee96b0dcc2a205f8dea6ac166202 Mon Sep 17 00:00:00 2001 From: Greene Date: Tue, 22 Mar 2022 17:35:42 -0600 Subject: [PATCH] Add test for non-2XX response to Send Signed-off-by: Stephen Greene --- test/integration/http/direct.go | 14 ++++++--- test/integration/http/direct_v1_test.go | 42 +++++++++++++++++++++++++ test/integration/http/tap_handler.go | 6 +++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/test/integration/http/direct.go b/test/integration/http/direct.go index 48a35adab..15f3712d8 100644 --- a/test/integration/http/direct.go +++ b/test/integration/http/direct.go @@ -27,17 +27,20 @@ import ( // Client is a set to binary or type DirectTapTest struct { - now time.Time - event *cloudevents.Event - want *cloudevents.Event - wantResult cloudevents.Result - asSent *TapValidation + now time.Time + event *cloudevents.Event + serverReturnedStatusCode int + want *cloudevents.Event + wantResult cloudevents.Result + asSent *TapValidation } type DirectTapTestCases map[string]DirectTapTest func ClientDirect(t *testing.T, tc DirectTapTest, copts ...client.Option) { tap := NewTap() + tap.statusCode = tc.serverReturnedStatusCode + server := httptest.NewServer(tap) defer server.Close() @@ -83,6 +86,7 @@ func ClientDirect(t *testing.T, tc DirectTapTest, copts ...client.Option) { t.Errorf("expected ACK, got %s", result) } } else if !cloudevents.ResultIs(result, tc.wantResult) { + t.Errorf("result.IsUndelivered = %v", cloudevents.IsUndelivered(result)) t.Fatalf("expected %s, got %s", tc.wantResult, result) } } diff --git a/test/integration/http/direct_v1_test.go b/test/integration/http/direct_v1_test.go index 47a3a637b..8e86ed6f6 100644 --- a/test/integration/http/direct_v1_test.go +++ b/test/integration/http/direct_v1_test.go @@ -8,6 +8,7 @@ package http import ( "fmt" "github.com/cloudevents/sdk-go/v2/client" + "net/http" "testing" "time" @@ -58,6 +59,47 @@ func TestSenderReceiver_binary_v1(t *testing.T) { ContentLength: 20, }, }, + "Binary v1.0 Sender Result Are NACK For Non-2XX": { + now: now, + event: &cloudevents.Event{ + Context: cloudevents.EventContextV1{ + ID: "ABC-123", + Type: "unit.test.client.sent", + Source: *cloudevents.ParseURIRef("/unit/test/client"), + Subject: strptr("resource"), + DataContentType: cloudevents.StringOfApplicationJSON(), + }.AsV1(), + DataEncoded: toBytes(map[string]interface{}{"hello": "unittest"}), + }, + serverReturnedStatusCode: http.StatusInternalServerError, + want: &cloudevents.Event{ + Context: cloudevents.EventContextV1{ + ID: "ABC-123", + Type: "unit.test.client.sent", + Time: &cloudevents.Timestamp{Time: now}, + Source: *cloudevents.ParseURIRef("/unit/test/client"), + Subject: strptr("resource"), + DataContentType: cloudevents.StringOfApplicationJSON(), + }.AsV1(), + DataEncoded: toBytes(map[string]interface{}{"hello": "unittest"}), + }, + wantResult: cloudevents.ResultNACK, + asSent: &TapValidation{ + Method: "POST", + URI: "/", + Header: map[string][]string{ + "ce-specversion": {"1.0"}, + "ce-id": {"ABC-123"}, + "ce-time": {now.UTC().Format(time.RFC3339Nano)}, + "ce-type": {"unit.test.client.sent"}, + "ce-source": {"/unit/test/client"}, + "ce-subject": {"resource"}, + "content-type": {"application/json"}, + }, + Body: `{"hello":"unittest"}`, + ContentLength: 20, + }, + }, } for n, tc := range testCases { diff --git a/test/integration/http/tap_handler.go b/test/integration/http/tap_handler.go index 9d14ab0c1..e786630d8 100644 --- a/test/integration/http/tap_handler.go +++ b/test/integration/http/tap_handler.go @@ -25,7 +25,8 @@ type TapValidation struct { } type tapHandler struct { - handler http.Handler + handler http.Handler + statusCode int req map[string]TapValidation resp map[string]TapValidation @@ -113,6 +114,9 @@ func (t *tapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) return } + if t.statusCode > 299 { + w.WriteHeader(t.statusCode) + } rec := httptest.NewRecorder() t.handler.ServeHTTP(rec, r)