-
Notifications
You must be signed in to change notification settings - Fork 630
Asserts on event-sender #3431
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
Asserts on event-sender #3431
Changes from all commits
6172720
b196bb0
e65e71d
ff1d2a4
429bc9c
d3d5d62
18ab9cb
d20a74f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| Copyright 2020 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package sender | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| cloudevents "github.com/cloudevents/sdk-go/v2" | ||
| cehttp "github.com/cloudevents/sdk-go/v2/protocol/http" | ||
| ) | ||
|
|
||
| const ( | ||
| EventType = "sender.test.knative.dev" | ||
| ResponseStatusCodeExtension = "responsestatuscode" | ||
| ) | ||
|
|
||
| // NewSenderEvent creates a new sender event assertable with the matchers provided in this package | ||
| func NewSenderEvent(id string, source string, event *cloudevents.Event, result *cehttp.Result) cloudevents.Event { | ||
| ev := cloudevents.NewEvent() | ||
| ev.SetID(id) | ||
| ev.SetSource(source) | ||
| ev.SetType(EventType) | ||
| ev.SetTime(time.Now()) | ||
|
|
||
| if result != nil { | ||
| ev.SetExtension(ResponseStatusCodeExtension, result.StatusCode) | ||
| } | ||
|
|
||
| if event != nil { | ||
| _ = ev.SetData("application/json", event) | ||
| } | ||
|
|
||
| return ev | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| Copyright 2020 The Knative Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package sender | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| cloudevents "github.com/cloudevents/sdk-go/v2" | ||
| "github.com/cloudevents/sdk-go/v2/event" | ||
| cetest "github.com/cloudevents/sdk-go/v2/test" | ||
| cetypes "github.com/cloudevents/sdk-go/v2/types" | ||
| ) | ||
|
|
||
| // MatchStatusCode matches the response status code of the sent event | ||
| func MatchStatusCode(status int) cetest.EventMatcher { | ||
|
slinkydeveloper marked this conversation as resolved.
|
||
| return cetest.AllOf( | ||
| cetest.HasType(EventType), | ||
| cetest.AnyOf( | ||
| // Because extensions could lose type information during serialization | ||
| // (eg when they're transported as http headers) the assert should match or the string or the int | ||
| cetest.HasExtension(ResponseStatusCodeExtension, cetypes.FormatInteger(int32(status))), | ||
| cetest.HasExtension(ResponseStatusCodeExtension, status), | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| // MatchInnerEvent matches the response event of the sent event | ||
| func MatchInnerEvent(matchers ...cetest.EventMatcher) cetest.EventMatcher { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this function used anywhere?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet, but i suppose we'll need it for broker data-plane tests and other tests when sender actually expects an event back
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lberk might want to take a look too ? |
||
| return cetest.AllOf(cetest.HasType(EventType), func(have event.Event) error { | ||
| if have.Data() != nil { | ||
| innerEvent := cloudevents.Event{} | ||
| err := have.DataAs(&innerEvent) | ||
| if err != nil { | ||
| return fmt.Errorf("error while trying to parse inner event %w", err) | ||
| } | ||
|
|
||
| return cetest.AllOf(matchers...)(innerEvent) | ||
| } | ||
| return errors.New("event doesn't contain an inner event") | ||
| }) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.