-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathindex.ts
More file actions
171 lines (151 loc) · 3.95 KB
/
index.ts
File metadata and controls
171 lines (151 loc) · 3.95 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as assert from 'assert';
import * as functions from 'firebase-functions';
// For example app.
// noinspection JSUnusedGlobalSymbols
export const listFruit = functions.https.onCall(() => {
return ['Apple', 'Banana', 'Cherry', 'Date', 'Fig', 'Grapes'];
});
// For e2e testing a custom region.
// noinspection JSUnusedGlobalSymbols
export const testFunctionCustomRegion = functions.region('europe-west1').https.onCall(() => 'europe-west1');
// For e2e testing timeouts.
export const testFunctionTimeout = functions.https.onCall((data) => {
console.log(JSON.stringify({ data }));
return new Promise((resolve, reject) => {
if (data && data.testTimeout) {
setTimeout(() => resolve({ timeLimit: 'exceeded' }), parseInt(data.testTimeout, 10));
} else {
reject(new functions.https.HttpsError('invalid-argument', 'testTimeout must be provided.'));
}
});
});
export const foo = functions.https.onCall((data) => {
return data
});
// For e2e testing that the caller is authorized
export const testFunctionAuthorized = functions.https.onCall(async (data, context) => {
console.log("testFunctionAuthorized called", data, context.auth);
if (!context.auth) {
throw new functions.https.HttpsError(
"unauthenticated",
"testFunctionAuthorized must be called while authenticated."
);
}
try {
return 'authorized';
} catch (e) {
throw new functions.https.HttpsError("internal", (e as any).message, (e as any).details);
}
});
// For e2e testing that the caller is authorized
export const testExceptions = functions.https.onCall(async (data, context) => {
if (data == 'bad-status') {
throw new functions.https.HttpsError("invalid-argument", "", "");
}
return {};
});
// For e2e testing errors & return values.
// noinspection JSUnusedGlobalSymbols
export const testFunctionDefaultRegion = functions.https.onCall((data) => {
console.log(JSON.stringify({ data }));
if (typeof data === 'undefined') {
return 'undefined';
}
if (typeof data === 'string') {
return 'string';
}
if (typeof data === 'number') {
return 'number';
}
if (typeof data === 'boolean') {
return 'boolean';
}
if (data === null) {
return 'null';
}
if (Array.isArray(data)) {
return 'array';
}
if (data.type === 'rawData') {
return data;
}
const sampleData: {
[key: string]: any;
} = {
number: 1234,
string: 'acde',
boolean: true,
null: null,
map: {
number: 1234,
string: 'acde',
boolean: true,
null: null,
},
list: [1234, 'acde', true, null],
deepMap: {
number: 123,
string: 'foo',
booleanTrue: true,
booleanFalse: false,
null: null,
list: ['1', 2, true, false],
map: {
number: 123,
string: 'foo',
booleanTrue: true,
booleanFalse: false,
null: null,
},
},
deepList: [
'1',
2,
true,
false,
['1', 2, true, false],
{
number: 123,
string: 'foo',
booleanTrue: true,
booleanFalse: false,
null: null,
},
],
};
const {
type,
asError,
inputData,
}: {
type: string;
asError?: boolean;
inputData?: any;
} = data;
if (!Object.hasOwnProperty.call(sampleData, type)) {
throw new functions.https.HttpsError('invalid-argument', 'Invalid test requested.');
}
const outputData = sampleData[type];
try {
assert.deepEqual(outputData, inputData);
} catch (e) {
console.error(e);
throw new functions.https.HttpsError(
'invalid-argument',
'Input and Output types did not match.',
(e as any).message
);
}
// all good
if (asError) {
throw new functions.https.HttpsError(
'cancelled',
'Response data was requested to be sent as part of an Error payload, so here we are!',
outputData
);
}
return outputData;
});
export const testMapConvertType = functions.https.onCall((data) => ({
foo: 'bar',
}));