Skip to content

Commit e716d9b

Browse files
authored
refactor: move ce extensions to separate test case. support traceparent (#395)
Signed-off-by: Grant Timmerman <[email protected]>
1 parent 71eb43f commit e716d9b

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

src/cloud_events.ts

-5
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,5 @@ export function getBinaryCloudEventContext(
6868
context[attributeName] = req.header(name);
6969
}
7070
}
71-
72-
// Populate the traceparent header.
73-
if (req.header('traceparent')) {
74-
context.traceparent = req.header('traceparent');
75-
}
7671
return context;
7772
}

src/function_wrappers.ts

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ const parseCloudEventRequest = (req: Request): CloudEventsContext => {
6666
cloudEvent = getBinaryCloudEventContext(req);
6767
cloudEvent.data = req.body;
6868
}
69+
// Populate the traceparent header.
70+
if (req.header('traceparent')) {
71+
cloudEvent.traceparent = req.header('traceparent');
72+
}
6973
return cloudEvent;
7074
};
7175

test/integration/cloud_event.ts

+35-7
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ import * as sinon from 'sinon';
1818
import {getServer} from '../../src/server';
1919
import * as supertest from 'supertest';
2020

21+
// A structured CloudEvent
2122
const TEST_CLOUD_EVENT = {
2223
specversion: '1.0',
2324
type: 'com.google.cloud.storage',
2425
source: 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs',
2526
subject: 'test-subject',
2627
id: 'test-1234-1234',
2728
time: '2020-05-13T01:23:45Z',
28-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
2929
datacontenttype: 'application/json',
3030
data: {
3131
some: 'payload',
3232
},
3333
};
34+
const TEST_EXTENSIONS = {
35+
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
36+
};
3437

3538
describe('CloudEvent Function', () => {
3639
let clock: sinon.SinonFakeTimers;
@@ -55,13 +58,10 @@ describe('CloudEvent Function', () => {
5558
},
5659
{
5760
name: 'CloudEvents v1.0 structured content request',
58-
headers: {
59-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
60-
},
61+
headers: {},
6162
body: TEST_CLOUD_EVENT,
6263
expectedCloudEvent: {
6364
...TEST_CLOUD_EVENT,
64-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
6565
},
6666
},
6767
{
@@ -75,9 +75,10 @@ describe('CloudEvent Function', () => {
7575
'ce-id': TEST_CLOUD_EVENT.id,
7676
'ce-time': TEST_CLOUD_EVENT.time,
7777
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
78-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
7978
},
80-
body: TEST_CLOUD_EVENT.data,
79+
body: {
80+
...TEST_CLOUD_EVENT.data,
81+
},
8182
expectedCloudEvent: TEST_CLOUD_EVENT,
8283
},
8384
{
@@ -230,6 +231,33 @@ describe('CloudEvent Function', () => {
230231
},
231232
},
232233
},
234+
{
235+
name: 'CloudEvents v1.0 traceparent extension – structured',
236+
headers: {
237+
'Content-Type': 'application/cloudevents+json',
238+
...TEST_EXTENSIONS,
239+
},
240+
body: TEST_CLOUD_EVENT,
241+
expectedCloudEvent: {...TEST_CLOUD_EVENT, ...TEST_EXTENSIONS},
242+
},
243+
{
244+
name: 'CloudEvents v1.0 traceparent extension – binary',
245+
headers: {
246+
'Content-Type': 'application/json',
247+
'ce-specversion': TEST_CLOUD_EVENT.specversion,
248+
'ce-type': TEST_CLOUD_EVENT.type,
249+
'ce-source': TEST_CLOUD_EVENT.source,
250+
'ce-subject': TEST_CLOUD_EVENT.subject,
251+
'ce-id': TEST_CLOUD_EVENT.id,
252+
'ce-time': TEST_CLOUD_EVENT.time,
253+
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
254+
...TEST_EXTENSIONS,
255+
},
256+
body: {
257+
...TEST_CLOUD_EVENT.data,
258+
},
259+
expectedCloudEvent: {...TEST_CLOUD_EVENT, ...TEST_EXTENSIONS},
260+
},
233261
];
234262
testData.forEach(test => {
235263
it(`${test.name}`, async () => {

test/integration/legacy_event.ts

-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const TEST_CLOUD_EVENT = {
2424
subject: 'test-subject',
2525
id: 'test-1234-1234',
2626
time: '2020-05-13T01:23:45Z',
27-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
2827
datacontenttype: 'application/json',
2928
data: {
3029
some: 'payload',
@@ -112,7 +111,6 @@ describe('Event Function', () => {
112111
specversion: '1.0',
113112
subject: 'test-subject',
114113
time: '2020-05-13T01:23:45Z',
115-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
116114
type: 'com.google.cloud.storage',
117115
},
118116
},
@@ -127,7 +125,6 @@ describe('Event Function', () => {
127125
'ce-id': TEST_CLOUD_EVENT.id,
128126
'ce-time': TEST_CLOUD_EVENT.time,
129127
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
130-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
131128
},
132129
body: TEST_CLOUD_EVENT.data,
133130
expectedData: TEST_CLOUD_EVENT.data,
@@ -139,7 +136,6 @@ describe('Event Function', () => {
139136
specversion: '1.0',
140137
subject: 'test-subject',
141138
time: '2020-05-13T01:23:45Z',
142-
traceparent: '00-65088630f09e0a5359677a7429456db7-97f23477fb2bf5ec-01',
143139
type: 'com.google.cloud.storage',
144140
},
145141
},

0 commit comments

Comments
 (0)