Skip to content

Commit

Permalink
fix http result errors by wrapping the upstream error, allowing for i…
Browse files Browse the repository at this point in the history
…sNack (#764)

* Add test for non-2XX response to Send

Signed-off-by: Stephen Greene <stephen.greene@sap.com>

* fix http result errors by wrapping the upstream error, allowing for isNack

Signed-off-by: Scott Nichols <n3wscott@chainguard.dev>

* special case http errors if the first error is not wrapped

Signed-off-by: Scott Nichols <n3wscott@chainguard.dev>

* Update test/integration/http/tap_handler.go

Signed-off-by: Scott Nichols <n3wscott@chainguard.dev>

* a receiver error will result in no event, the tap is returning an error before the handler

Signed-off-by: Scott Nichols <n3wscott@chainguard.dev>

Co-authored-by: Greene <stephen.greene@sap.com>
  • Loading branch information
n3wscott and sbgreene1307 committed Mar 31, 2022
1 parent 7b7049a commit 9380b70
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
14 changes: 9 additions & 5 deletions test/integration/http/direct.go
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
32 changes: 32 additions & 0 deletions test/integration/http/direct_v1_test.go
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,37 @@ 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: nil,
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
7 changes: 6 additions & 1 deletion test/integration/http/tap_handler.go
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,10 @@ func (t *tapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
return
}
if t.statusCode > 299 {
w.WriteHeader(t.statusCode)
return
}

rec := httptest.NewRecorder()
t.handler.ServeHTTP(rec, r)
Expand Down
9 changes: 8 additions & 1 deletion v2/protocol/http/protocol.go
Expand Up @@ -157,7 +157,14 @@ func (p *Protocol) Send(ctx context.Context, m binding.Message, transformers ...
buf := new(bytes.Buffer)
buf.ReadFrom(message.BodyReader)
errorStr := buf.String()
err = NewResult(res.StatusCode, "%s", errorStr)
// If the error is not wrapped, then append the original error string.
if og, ok := err.(*Result); ok {
og.Format = og.Format + "%s"
og.Args = append(og.Args, errorStr)
err = og
} else {
err = NewResult(res.StatusCode, "%w: %s", err, errorStr)
}
}
}
}
Expand Down

0 comments on commit 9380b70

Please sign in to comment.