-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathexecution_context.ts
58 lines (47 loc) · 1.55 KB
/
execution_context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {executionContextMiddleware} from '../src/execution_context';
import {Request, Response} from '../src/functions';
import * as assert from 'assert';
import * as sinon from 'sinon';
describe('executionContextMiddleware', () => {
const createRequest = (
body: object | string,
headers?: {[key: string]: string}
) =>
({
body,
header: (h: string) => {
return headers === undefined ? '' : headers[h];
},
}) as Request;
const next = sinon.spy();
const testSpanId = '123';
const testTrace = 'testtrace';
const validExecutionId = 'xn1h9xdgv6zw';
it('uses execution ID in header', () => {
const req = createRequest(
{},
{
'X-Cloud-Trace-Context': `${testTrace}/${testSpanId};o=1`,
'function-execution-id': validExecutionId,
}
);
executionContextMiddleware(req as Request, {} as Response, next);
assert.strictEqual(req.executionId, validExecutionId);
assert.strictEqual(req.spanId, testSpanId);
});
it('generates execution ID if not in header', () => {
const req = createRequest(
{},
{'X-Cloud-Trace-Context': `${testTrace}/${testSpanId}`}
);
executionContextMiddleware(req as Request, {} as Response, next);
assert(req.executionId);
assert.strictEqual(req.spanId, testSpanId);
});
it('req trace undefined if not in header', () => {
const req = createRequest({}, {});
executionContextMiddleware(req as Request, {} as Response, next);
assert(req.executionId);
assert.strictEqual(req.spanId, undefined);
});
});