Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Conversation

@RaeesBhatti
Copy link
Contributor

@RaeesBhatti RaeesBhatti commented May 9, 2018

Add supports for proper coercion of HTTP and Invoke events to CloudEvent format.

This PR also introduces new CloudEvents processing flow:

  1. Is the Content-Type "application/cloudevents+json"?
  • If yes, we're in Structured mode for CloudEvents HTTP Transport Binding. Check if the request body is a valid CloudEvent. Pass along the CloudEvent if yes, reject (400 Bad Request) if not.
  1. Are the binary CloudEvent headers present? CE-EventType, CE-CloudEventsVersion, etc.
  • If yes, we're in Binary mode for CloudEvents HTTP Transport Binding. Check if the request is a valid CloudEvent. Pass along the CloudEvent if yes, reject if not.
  1. Is there a header of "Event" that's not the value of "invoke"?
  • If yes, we're in legacy Custom Event mode. Use the following steps:
    • If the Content-Type is "application/json" or "application/cloudevents+json", check the request body to see if it is a valid CloudEvent.
      • If yes, pass along the unaltered request body to any subscriptions.
      • If no, create a new CloudEvent with default envelope fields, and put the request body into the "data" property on the CloudEvent.
    • If it's any other Content-Type, create a new CloudEvent with default envelope fields, and put the request body into the "data" property on the CloudEvent.
  1. Default:
  • We have a "http.request" Event Type. Make a CloudEvent with default envelope fields, and include the HTTP request details in the "data" property of the CloudEvent.

Todos:

  • Write tests
  • Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: YES

@mthenw mthenw changed the title HTTP CloudEvent coercion [WIP] HTTP CloudEvent coercion May 9, 2018
event/request.go Outdated
return eventType
}

func mapHeadersToEvent(event *Event, headers http.Header) *Event {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's rename this function to parseAsCloudEventBinary

expectedEvent *eventpkg.Event
}{
{
url: "https://something.eventgateway.com/myspace",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need similar test case for application/json

expectedEvent *eventpkg.Event
}{
{
url: "https://something.eventgateway.com/myspace",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change that to example.com. For some moment I was confused because I thought it's hosted EG url.

CloudEventsVersion: eventpkg.TransformationVersion,
Source: "/mysource",
ContentType: "text/plain",
Data: &eventpkg.HTTPEvent{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this stuct to HTTPRequestData

event/request.go Outdated
)

const (
mimeCloudEventsJSON = "application/cloudevents+json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not used

event/event.go Outdated
return nil, errors.New("couldn't cast to []byte")
}

func isJSONContent(mime string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this function to isJSONMimeType?

}
}

var fromRequestTests = []struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need more cases here. also with Event header set

event/request.go Outdated
return eventType
}

func parseAsCloudEventBinary(event *Event, headers http.Header) *Event {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more request here. It should not get Event as argument. It should be similar to parseAsCloudEvent. So the arguments should be only what's needed which is headers and body []byte. Event should be created inside.

event/http.go Outdated

// NewHTTPEvent returns a new instance of HTTPEvent
func NewHTTPEvent(r *http.Request, eventData interface{}) *HTTPEvent {
func NewHTTPEvent(r *http.Request, eventData interface{}) *HTTPRequestData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be renamed to NewHTTPRequestData

event/request.go Outdated
@@ -0,0 +1,96 @@
package event
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move the content of this file to event.go? those file are pretty short and the are basically about the same thing.

@mthenw mthenw changed the title [WIP] HTTP CloudEvent coercion HTTP CloudEvent coercion May 24, 2018
@codecov
Copy link

codecov bot commented May 24, 2018

Codecov Report

Merging #421 into master will increase coverage by 0.01%.
The diff coverage is 80.32%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #421      +/-   ##
==========================================
+ Coverage   63.87%   63.89%   +0.01%     
==========================================
  Files          29       30       +1     
  Lines        1650     1681      +31     
==========================================
+ Hits         1054     1074      +20     
- Misses        553      563      +10     
- Partials       43       44       +1
Impacted Files Coverage Δ
event/errors.go 0% <0%> (ø)
router/event.go 100% <100%> (+19.56%) ⬆️
event/http.go 100% <100%> (+100%) ⬆️
router/router.go 52.14% <30%> (-5.81%) ⬇️
event/event.go 77.41% <91.48%> (+7.1%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 807d0e9...f0a134d. Read the comment docs.

Copy link
Contributor Author

@RaeesBhatti RaeesBhatti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

key = strings.TrimLeft(key, "Ce-X-")
// Make first character lowercase
runes := []rune(key)
runes[0] = unicode.ToLower(runes[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HTTP spec says that the header keys are case-insensitive. So, I think it will be better to do a strings.ToLower here for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's true, but in the same time, when you look at the example in HTTP binding spec you will see that desired behavior is to preserve case (testExtension).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants