forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution_context.ts
112 lines (104 loc) · 2.86 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
executionContextMiddleware,
getCurrentContext,
ExeuctionContext,
} from '../src/execution_context';
import {Request, Response, NextFunction} from 'express';
import * as assert from 'assert';
describe('executionContextMiddleware', () => {
const createRequest = (
body: object | string,
headers?: {[key: string]: string}
) =>
({
body,
header: (h: string) => {
return headers === undefined ? '' : headers[h];
},
}) as Request;
const testSpanId = '123';
const testTrace = 'testtrace';
const cloudTraceContext = `${testTrace}/${testSpanId};o=1`;
const validExecutionId = 'xn1h9xdgv6zw';
function assertExecutionContext(exeuctionContext?: ExeuctionContext) {
assert(exeuctionContext);
assert.strictEqual(
(exeuctionContext as ExeuctionContext)['logging.googleapis.com/labels']
?.executionId.length,
12
);
assert.strictEqual(
exeuctionContext['logging.googleapis.com/spanId'],
testSpanId
);
assert.strictEqual(
exeuctionContext['logging.googleapis.com/trace'],
testTrace
);
}
it('uses execution ID in header', () => {
const request = createRequest(
{},
{
'X-Cloud-Trace-Context': cloudTraceContext,
'function-execution-id': validExecutionId,
}
);
let exeuctionContext;
const next = () => {
exeuctionContext = getCurrentContext() as ExeuctionContext;
assert(exeuctionContext);
assert.deepEqual(exeuctionContext['logging.googleapis.com/labels'], {
executionId: validExecutionId,
});
assert.strictEqual(
exeuctionContext['logging.googleapis.com/spanId'],
testSpanId
);
assert.strictEqual(
exeuctionContext['logging.googleapis.com/trace'],
testTrace
);
};
executionContextMiddleware(
request as Request,
{} as Response,
next as NextFunction
);
});
it('generates execution ID if not in header', () => {
const request = createRequest(
{},
{'X-Cloud-Trace-Context': cloudTraceContext}
);
let exeuctionContext;
const next = () => {
exeuctionContext = getCurrentContext() as ExeuctionContext;
assertExecutionContext(exeuctionContext);
};
executionContextMiddleware(
request as Request,
{} as Response,
next as NextFunction
);
});
it('generates execution ID if header is malformed', () => {
const request = createRequest(
{},
{
'X-Cloud-Trace-Context': cloudTraceContext,
'function-execution-id': 'abcde',
}
);
let exeuctionContext;
const next = () => {
exeuctionContext = getCurrentContext() as ExeuctionContext;
assertExecutionContext(exeuctionContext);
};
executionContextMiddleware(
request as Request,
{} as Response,
next as NextFunction
);
});
});