-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper function to create event from HTTP Request or Response (#799)
* Helper function to create event from HTTP Request or Response * Add missing use of encoding options and remove redundant function call Signed-off-by: Adam Ross <[email protected]> * Update v2/protocol/http/utility.go Co-authored-by: Scott Nichols <[email protected]> * Update v2/protocol/http/utility.go Co-authored-by: Scott Nichols <[email protected]> * Update v2/protocol/http/utility.go Co-authored-by: Scott Nichols <[email protected]> * Update v2/protocol/http/utility.go Co-authored-by: Scott Nichols <[email protected]> * capitalize HTTP and WIP clean-up Signed-off-by: Adam Ross <[email protected]> Signed-off-by: Adam Ross <[email protected]> Co-authored-by: Scott Nichols <[email protected]>
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright 2022 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
nethttp "net/http" | ||
|
||
"github.com/cloudevents/sdk-go/v2/binding" | ||
"github.com/cloudevents/sdk-go/v2/event" | ||
) | ||
|
||
// NewEventFromHTTPRequest returns an Event. | ||
func NewEventFromHTTPRequest(req *nethttp.Request) (*event.Event, error) { | ||
msg := NewMessageFromHttpRequest(req) | ||
return binding.ToEvent(context.Background(), msg) | ||
} | ||
|
||
// NewEventFromHTTPResponse returns an Event. | ||
func NewEventFromHTTPResponse(resp *nethttp.Response) (*event.Event, error) { | ||
msg := NewMessageFromHttpResponse(resp) | ||
return binding.ToEvent(context.Background(), msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
Copyright 2022 The CloudEvents Authors | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package http | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cloudevents/sdk-go/v2/binding" | ||
"github.com/cloudevents/sdk-go/v2/event" | ||
"github.com/cloudevents/sdk-go/v2/test" | ||
) | ||
|
||
func TestNewEventFromHttpRequest(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
encoding binding.Encoding | ||
}{{ | ||
name: "Structured encoding", | ||
encoding: binding.EncodingStructured, | ||
}, { | ||
name: "Binary encoding", | ||
encoding: binding.EncodingBinary, | ||
}} | ||
|
||
for _, tt := range tests { | ||
test.EachEvent(t, test.Events(), func(t *testing.T, eventIn event.Event) { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctx := context.TODO() | ||
if tt.encoding == binding.EncodingStructured { | ||
ctx = binding.WithForceStructured(ctx) | ||
} else if tt.encoding == binding.EncodingBinary { | ||
ctx = binding.WithForceBinary(ctx) | ||
} | ||
|
||
req := httptest.NewRequest("POST", "http://localhost", nil) | ||
require.NoError(t, WriteRequest(ctx, (*binding.EventMessage)(&eventIn), req)) | ||
|
||
got, err := NewEventFromHTTPRequest(req) | ||
require.NoError(t, err) | ||
test.AssertEvent(t, *got, test.IsValid()) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func TestNewEventFromHttpResponse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resp *http.Response | ||
}{{ | ||
name: "Structured encoding", | ||
resp: &http.Response{ | ||
Header: http.Header{ | ||
"Content-Type": {event.ApplicationCloudEventsJSON}, | ||
}, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"data":"foo","datacontenttype":"application/json","id":"id","source":"source","specversion":"1.0","type":"type"}`))), | ||
ContentLength: 113, | ||
}, | ||
}, { | ||
name: "Binary encoding", | ||
resp: &http.Response{ | ||
Header: func() http.Header { | ||
h := http.Header{} | ||
h.Set("ce-specversion", "1.0") | ||
h.Set("ce-source", "unittest") | ||
h.Set("ce-type", "unittest") | ||
h.Set("ce-id", "unittest") | ||
h.Set("Content-Type", "application/json") | ||
return h | ||
}(), | ||
}, | ||
}} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := NewEventFromHTTPResponse(tt.resp) | ||
require.NoError(t, err) | ||
test.AssertEvent(t, *got, test.IsValid()) | ||
}) | ||
} | ||
} |