-
Notifications
You must be signed in to change notification settings - Fork 20
feat: handle CloudEvent and Message responses from function invocation #68
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = function testFunc(context) { | ||
return context; | ||
return { ...context.query }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ const Spec = require('../lib/ce-constants.js').Spec; | |
const { existsSync, readdirSync } = require('fs'); | ||
const { execSync } = require('child_process'); | ||
const path = require('path'); | ||
const { CloudEvent } = require('cloudevents'); | ||
const { CloudEvent, HTTP } = require('cloudevents'); | ||
|
||
// Ensure fixture dependencies are installed | ||
const fixtureDir = path.join(__dirname, 'fixtures'); | ||
|
@@ -173,6 +173,62 @@ test('Responds to 1.0 structured cloud events', t => { | |
}, { log: false }); | ||
}); | ||
|
||
test('Handles 1.0 CloudEvent responses', t => { | ||
framework(_ => { | ||
return new CloudEvent({ | ||
source: 'test', | ||
type: 'test-type', | ||
data: 'some data', | ||
datacontenttype: 'text/plain' | ||
}); | ||
}, server => { | ||
request(server) | ||
.post('/') | ||
.send({ message: 'hello' }) | ||
.set(Spec.id, '1') | ||
.set(Spec.source, 'integration-test') | ||
.set(Spec.type, 'dev.knative.example') | ||
.set(Spec.version, '1.0') | ||
.expect(200) | ||
.expect('Content-Type', /text/) | ||
.end((err, res) => { | ||
t.error(err, 'No error'); | ||
t.equal(res.text, 'some data'); | ||
t.end(); | ||
server.close(); | ||
}); | ||
}, | ||
{ log: false }); | ||
}); | ||
|
||
test('Handles 1.0 CloudEvent Message responses', t => { | ||
framework(_ => { | ||
return HTTP.binary(new CloudEvent({ | ||
source: 'test', | ||
type: 'test-type', | ||
data: 'some data', | ||
datacontenttype: 'text/plain' | ||
})); | ||
}, server => { | ||
request(server) | ||
.post('/') | ||
.send({ message: 'hello' }) | ||
.set(Spec.id, '1') | ||
.set(Spec.source, 'integration-test') | ||
.set(Spec.type, 'dev.knative.example') | ||
.set(Spec.version, '1.0') | ||
.expect(200) | ||
.expect('Content-Type', /text/) | ||
.end((err, res) => { | ||
t.error(err, 'No error'); | ||
t.equal(res.text, 'some data'); | ||
t.end(); | ||
server.close(); | ||
}); | ||
}, | ||
{ log: false }); | ||
}); | ||
|
||
test('Extracts event data as the first parameter to a function', t => { | ||
const data = { | ||
lunch: "tacos" | ||
|
@@ -226,25 +282,6 @@ test('Successfully handles events with no data', t => { | |
}); | ||
}); | ||
|
||
test('Responds with error code (4xx or 5xx) to malformed cloud events', t => { | ||
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. Why deleting this? 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. I probably should have mentioned that in the description. I don't think we should fail if we can help it. If we can recover by creating a CE that is correct, in spite of the malformation, then I think we should do that. 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. @matejvasek I will elaborate a little more here. The rationale behind this change is rooted in changes that were made in the upstream But a bigger thing that it did was change how the receiver handles malformed events. A summary of the wider community discussion on why receiving a CloudEvent should provide loose validation is in this comment. Because of this change in the upstream validation behavior, an exception is never thrown on our end when receiving an event. In |
||
const func = require(`${__dirname}/fixtures/cloud-event/`); | ||
framework(func, server => { | ||
request(server) | ||
.post('/') | ||
.send({ message: 'hello' }) | ||
.set(Spec.id, '1') | ||
.set(Spec.source, 'integration-test') | ||
.set(Spec.version, '0.3') | ||
.set('ce-datacontenttype', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.end((err, res) => { | ||
t.assert(res.statusCode >= 400 && res.statusCode <= 599, 'Error code 4xx or 5xx expected.'); | ||
t.end(); | ||
server.close(); | ||
}); | ||
}, { log: false }); | ||
}); | ||
|
||
test('Responds with 406 Not Acceptable to unknown cloud event versions', t => { | ||
const func = require(`${__dirname}/fixtures/cloud-event/`); | ||
framework(func, server => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change, shouldn't it be case insensitive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right - it should be, and it is here where we merge user headers with event headers https://github.com/boson-project/faas-js-runtime/pull/68/files#diff-89ae878b1a78765add577244271796f156213cdd0ab6722f641ef96926f38674R54.
That change to lowercase is kind of immaterial since we do lowercase all headers later. It was left over from some debugging I was doing when I realized that I was not doing case-insensitive comparisons. But I thought it would be fine to keep. 🤷