Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for non-2XX response to Send #763

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions test/integration/http/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/integration/http/direct_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package http
import (
"fmt"
"github.com/cloudevents/sdk-go/v2/client"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion test/integration/http/tap_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down