-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtestFunctionDefaultRegion.ts
More file actions
74 lines (61 loc) · 1.8 KB
/
Copy pathtestFunctionDefaultRegion.ts
File metadata and controls
74 lines (61 loc) · 1.8 KB
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
/*
*
* Testing tools for invertase/react-native-firebase use only.
*
* Copyright (C) 2018-present Invertase Limited <oss@invertase.io>
*
* See License file for more information.
*/
import { deepEqual } from 'assert';
import { FirebaseError } from 'firebase-admin';
import { CallableRequest, onCall, HttpsError } from 'firebase-functions/v2/https';
import { E2E_TEST_FUNCTION_TIMEOUT_SECONDS } from './e2eCallOptions';
import SAMPLE_DATA from './sample-data';
export const testFunctionDefaultRegionV2 = onCall(
{ timeoutSeconds: E2E_TEST_FUNCTION_TIMEOUT_SECONDS },
(req: CallableRequest<{ type: string; asError: boolean; inputData: any }>) => {
console.log(Date.now(), req.data);
if (typeof req.data === 'undefined') {
return 'undefined';
}
if (typeof req.data === 'string') {
return 'string';
}
if (typeof req.data === 'number') {
return 'number';
}
if (typeof req.data === 'boolean') {
return 'boolean';
}
if (req.data === null) {
return 'null';
}
if (Array.isArray(req.data)) {
return 'array';
}
const { type, asError, inputData } = req.data;
if (!Object.hasOwnProperty.call(SAMPLE_DATA, type)) {
throw new HttpsError('invalid-argument', 'Invalid test requested.');
}
const outputData = SAMPLE_DATA[type];
try {
deepEqual(outputData, inputData);
} catch (e) {
console.error(e);
throw new HttpsError(
'invalid-argument',
'Input and Output types did not match.',
(e as FirebaseError).message,
);
}
// all good
if (asError) {
throw new HttpsError(
'cancelled',
'Response data was requested to be sent as part of an Error payload, so here we are!',
outputData,
);
}
return outputData;
},
);