Skip to content

Commit

Permalink
refactor: move ce extensions to separate test case. support tracepare…
Browse files Browse the repository at this point in the history
…nt (#395)

Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
  • Loading branch information
grant committed Nov 23, 2021
1 parent 71eb43f commit e716d9b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
5 changes: 0 additions & 5 deletions src/cloud_events.ts
Expand Up @@ -68,10 +68,5 @@ export function getBinaryCloudEventContext(
context[attributeName] = req.header(name);
}
}

// Populate the traceparent header.
if (req.header('traceparent')) {
context.traceparent = req.header('traceparent');
}
return context;
}
4 changes: 4 additions & 0 deletions src/function_wrappers.ts
Expand Up @@ -66,6 +66,10 @@ const parseCloudEventRequest = (req: Request): CloudEventsContext => {
cloudEvent = getBinaryCloudEventContext(req);
cloudEvent.data = req.body;
}
// Populate the traceparent header.
if (req.header('traceparent')) {
cloudEvent.traceparent = req.header('traceparent');
}
return cloudEvent;
};

Expand Down
42 changes: 35 additions & 7 deletions test/integration/cloud_event.ts
Expand Up @@ -18,19 +18,22 @@ import * as sinon from 'sinon';
import {getServer} from '../../src/server';
import * as supertest from 'supertest';

// A structured CloudEvent
const TEST_CLOUD_EVENT = {
specversion: '1.0',
type: 'com.google.cloud.storage',
source: 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs',
subject: 'test-subject',
id: 'test-1234-1234',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
datacontenttype: 'application/json',
data: {
some: 'payload',
},
};
const TEST_EXTENSIONS = {
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
};

describe('CloudEvent Function', () => {
let clock: sinon.SinonFakeTimers;
Expand All @@ -55,13 +58,10 @@ describe('CloudEvent Function', () => {
},
{
name: 'CloudEvents v1.0 structured content request',
headers: {
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
headers: {},
body: TEST_CLOUD_EVENT,
expectedCloudEvent: {
...TEST_CLOUD_EVENT,
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
},
{
Expand All @@ -75,9 +75,10 @@ describe('CloudEvent Function', () => {
'ce-id': TEST_CLOUD_EVENT.id,
'ce-time': TEST_CLOUD_EVENT.time,
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
body: TEST_CLOUD_EVENT.data,
body: {
...TEST_CLOUD_EVENT.data,
},
expectedCloudEvent: TEST_CLOUD_EVENT,
},
{
Expand Down Expand Up @@ -230,6 +231,33 @@ describe('CloudEvent Function', () => {
},
},
},
{
name: 'CloudEvents v1.0 traceparent extension – structured',
headers: {
'Content-Type': 'application/cloudevents+json',
...TEST_EXTENSIONS,
},
body: TEST_CLOUD_EVENT,
expectedCloudEvent: {...TEST_CLOUD_EVENT, ...TEST_EXTENSIONS},
},
{
name: 'CloudEvents v1.0 traceparent extension – binary',
headers: {
'Content-Type': 'application/json',
'ce-specversion': TEST_CLOUD_EVENT.specversion,
'ce-type': TEST_CLOUD_EVENT.type,
'ce-source': TEST_CLOUD_EVENT.source,
'ce-subject': TEST_CLOUD_EVENT.subject,
'ce-id': TEST_CLOUD_EVENT.id,
'ce-time': TEST_CLOUD_EVENT.time,
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
...TEST_EXTENSIONS,
},
body: {
...TEST_CLOUD_EVENT.data,
},
expectedCloudEvent: {...TEST_CLOUD_EVENT, ...TEST_EXTENSIONS},
},
];
testData.forEach(test => {
it(`${test.name}`, async () => {
Expand Down
4 changes: 0 additions & 4 deletions test/integration/legacy_event.ts
Expand Up @@ -24,7 +24,6 @@ const TEST_CLOUD_EVENT = {
subject: 'test-subject',
id: 'test-1234-1234',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
datacontenttype: 'application/json',
data: {
some: 'payload',
Expand Down Expand Up @@ -112,7 +111,6 @@ describe('Event Function', () => {
specversion: '1.0',
subject: 'test-subject',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
type: 'com.google.cloud.storage',
},
},
Expand All @@ -127,7 +125,6 @@ describe('Event Function', () => {
'ce-id': TEST_CLOUD_EVENT.id,
'ce-time': TEST_CLOUD_EVENT.time,
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
},
body: TEST_CLOUD_EVENT.data,
expectedData: TEST_CLOUD_EVENT.data,
Expand All @@ -139,7 +136,6 @@ describe('Event Function', () => {
specversion: '1.0',
subject: 'test-subject',
time: '2020-05-13T01:23:45Z',
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
type: 'com.google.cloud.storage',
},
},
Expand Down

0 comments on commit e716d9b

Please sign in to comment.