adds a test for the protocol dumping of a request to the containert stdin.
there are a number of vectors to test for a cloud event, but since we're going
to change that behavior soon it's probably a waste of time to go about doing
so. in any event, this was pretty broken. my understanding of the cloud event
spec is deepening and the json stuff overall seems a little weird.

* fixes content type issue around json checking (since a string is also a json
value, we can just decode it, even though it's wasteful it's more easily
correct)
* doesn't force all json values to be map[string]interface{} and lets them be
whoever they want to be. maybe their dads are still proud.

closes #966
This commit is contained in:
Reed Allman
2018-05-07 22:22:53 -07:00
committed by GitHub
parent fe2c3daee3
commit 1f1624782b
2 changed files with 108 additions and 11 deletions

View File

@@ -21,11 +21,11 @@ type CloudEvent struct {
Source string `json:"source"`
EventType string `json:"eventType"`
EventTypeVersion string `json:"eventTypeVersion"`
EventTime time.Time `json:"eventTime"` // TODO: ensure rfc3339 format
EventTime time.Time `json:"eventTime"`
SchemaURL string `json:"schemaURL"`
ContentType string `json:"contentType"`
Extensions map[string]interface{} `json:"extensions"`
Data interface{} `json:"data"` // from docs: the payload is encoded into a media format which is specified by the contentType attribute (e.g. application/json)
Data interface{} `json:"data,omitempty"` // docs: the payload is encoded into a media format which is specified by the contentType attribute (e.g. application/json)
}
type cloudEventIn struct {
@@ -63,6 +63,8 @@ func (h *CloudEventProtocol) writeJSONToContainer(ci CallInfo) error {
return err
}
// TODO: handle binary
var in cloudEventIn
if ci.IsCloudEvent() {
// then it's already in the right format, let's parse it, then modify
@@ -80,18 +82,13 @@ func (h *CloudEventProtocol) writeJSONToContainer(ci CallInfo) error {
Source: ci.RequestURL(),
},
}
if buf.Len() == 0 {
// nada
// todo: should we leave as null, pass in empty string, omitempty or some default for the content type, eg: {} for json?
} else if ci.ContentType() == "application/json" {
d := map[string]interface{}{}
err = json.NewDecoder(buf).Decode(&d)
// NOTE: data is an optional field, we can leave it as nil
if buf.Len() > 0 {
// NOTE: if it's not contentType=application/json, then a string is a valid json value, so this will work.
err := json.NewDecoder(buf).Decode(&in.Data)
if err != nil {
return fmt.Errorf("Invalid json body with contentType 'application/json'. %v", err)
}
in.Data = d
} else {
in.Data = buf.String()
}
}
// todo: deal with the dual ID's, one from outside, one from inside