Skip to content

Use CloudEvents v1.0 in CloudEventsContext and tests #139

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

Merged
merged 3 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cloudevents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CloudEventsContext } from './functions';
* Checks whether the incoming request is a CloudEvents event in binary content
* mode. This is verified by checking the presence of required headers.
*
* @link https://github.com/cloudevents/spec/blob/master/http-transport-binding.md#31-binary-content-mode
* @link https://github.com/cloudevents/spec/blob/master/http-protocol-binding.md#3-http-message-mapping
*
* @param req Express request object.
* @return True if the request is a CloudEvents event in binary content mode,
Expand Down
4 changes: 2 additions & 2 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export interface CloudFunctionsContext {
}

/**
* The CloudEvents v0.2 context object for the event.
* The CloudEvents v1.0 context object for the event.
*
* @link https://github.com/cloudevents/spec/blob/v0.2/spec.md#context-attributes
* @link https://github.com/cloudevents/spec/blob/master/spec.md#context-attributes
*/
export interface CloudEventsContext {
/**
Expand Down
69 changes: 39 additions & 30 deletions test/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,38 +156,47 @@ describe('CloudEvents request to event function', () => {
headers: { [key: string]: string };
body: {};
}

const specversion = '1.0';
const type = 'com.google.cloud.storage';
const source =
'https://github.com/GoogleCloudPlatform/functions-framework-nodejs';
const subject = 'test-subject';
const id = 'test-1234-1234';
const time = '2020-05-13T01:23:45Z';
const datacontenttype = 'application/json';
const data = {
some: 'payload',
};

const testData: TestData[] = [
{
name: 'CloudEvents v0.2 structured content mode',
name: 'CloudEvents v1.0 structured content mode',
headers: { 'Content-Type': 'application/cloudevents+json' },
body: {
type: 'testType',
specversion: 'testSpecversion',
source: 'testSource',
id: 'testId',
time: 'testTime',
schemaurl: 'testSchemaurl',
contenttype: 'testContenttype',
data: {
some: 'payload',
},
specversion,
type,
source,
subject,
id,
time,
datacontenttype,
data,
},
},
{
name: 'CloudEvents v0.2 binary content mode',
name: 'CloudEvents v1.0 binary content mode',
headers: {
'Content-Type': 'application/json',
'ce-type': 'testType',
'ce-specversion': 'testSpecversion',
'ce-source': 'testSource',
'ce-id': 'testId',
'ce-time': 'testTime',
'ce-schemaurl': 'testSchemaurl',
'ce-contenttype': 'testContenttype',
},
body: {
some: 'payload',
'ce-specversion': specversion,
'ce-type': type,
'ce-source': source,
'ce-subject': subject,
'ce-id': id,
'ce-time': time,
'ce-datacontenttype': datacontenttype,
},
body: data,
},
];
testData.forEach(test => {
Expand All @@ -206,15 +215,15 @@ describe('CloudEvents request to event function', () => {
.set(test.headers)
.send(test.body)
.expect(204);
assert.deepStrictEqual(receivedData, { some: 'payload' });
assert.deepStrictEqual(receivedData, data);
assert.notStrictEqual(receivedContext, null);
assert.strictEqual(receivedContext!.type, 'testType');
assert.strictEqual(receivedContext!.specversion, 'testSpecversion');
assert.strictEqual(receivedContext!.source, 'testSource');
assert.strictEqual(receivedContext!.id, 'testId');
assert.strictEqual(receivedContext!.time, 'testTime');
assert.strictEqual(receivedContext!.schemaurl, 'testSchemaurl');
assert.strictEqual(receivedContext!.contenttype, 'testContenttype');
assert.strictEqual(receivedContext!.specversion, specversion);
assert.strictEqual(receivedContext!.type, type);
assert.strictEqual(receivedContext!.source, source);
assert.strictEqual(receivedContext!.subject, subject);
assert.strictEqual(receivedContext!.id, id);
assert.strictEqual(receivedContext!.time, time);
assert.strictEqual(receivedContext!.datacontenttype, datacontenttype);
});
});
});