Skip to content

Commit 39766f0

Browse files
authored
v2 Callable now takes a single parameter (#929)
* v2 Callable now takes a single parameter * Fix https tests
1 parent 6d16d52 commit 39766f0

File tree

4 files changed

+275
-137
lines changed

4 files changed

+275
-137
lines changed

spec/common/providers/https.spec.ts

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ interface CallTest {
3838
// The function to execute with the request.
3939
callableFunction: (data: any, context: https.CallableContext) => any;
4040

41+
callableFunction2: (request: https.CallableRequest<any>) => any;
42+
4143
// The expected shape of the http response returned to the callable SDK.
4244
expectedHttpResponse: RunHandlerResult;
4345
}
@@ -92,16 +94,27 @@ function runHandler(
9294
// Runs a CallTest test.
9395
async function runTest(test: CallTest): Promise<any> {
9496
const opts = { origin: true, methods: 'POST' };
95-
const callableFunction = https.onCallHandler(opts, (data, context) => {
97+
const callableFunctionV1 = https.onCallHandler(opts, (data, context) => {
9698
expect(data).to.deep.equal(test.expectedData);
9799
return test.callableFunction(data, context);
98100
});
99101

100-
const response = await runHandler(callableFunction, test.httpRequest);
102+
const responseV1 = await runHandler(callableFunctionV1, test.httpRequest);
103+
104+
expect(responseV1.body).to.deep.equal(test.expectedHttpResponse.body);
105+
expect(responseV1.headers).to.deep.equal(test.expectedHttpResponse.headers);
106+
expect(responseV1.status).to.equal(test.expectedHttpResponse.status);
107+
108+
const callableFunctionV2 = https.onCallHandler(opts, (request) => {
109+
expect(request.data).to.deep.equal(test.expectedData);
110+
return test.callableFunction2(request);
111+
});
112+
113+
const responseV2 = await runHandler(callableFunctionV2, test.httpRequest);
101114

102-
expect(response.body).to.deep.equal(test.expectedHttpResponse.body);
103-
expect(response.headers).to.deep.equal(test.expectedHttpResponse.headers);
104-
expect(response.status).to.equal(test.expectedHttpResponse.status);
115+
expect(responseV2.body).to.deep.equal(test.expectedHttpResponse.body);
116+
expect(responseV2.headers).to.deep.equal(test.expectedHttpResponse.headers);
117+
expect(responseV2.status).to.equal(test.expectedHttpResponse.status);
105118
}
106119

107120
describe('onCallHandler', () => {
@@ -138,6 +151,7 @@ describe('onCallHandler', () => {
138151
httpRequest: mockRequest({ foo: 'bar' }),
139152
expectedData: { foo: 'bar' },
140153
callableFunction: (data, context) => ({ baz: 'qux' }),
154+
callableFunction2: (request) => ({ baz: 'qux' }),
141155
expectedHttpResponse: {
142156
status: 200,
143157
headers: expectedResponseHeaders,
@@ -151,6 +165,7 @@ describe('onCallHandler', () => {
151165
httpRequest: mockRequest(null),
152166
expectedData: null,
153167
callableFunction: (data, context) => null,
168+
callableFunction2: (request) => null,
154169
expectedHttpResponse: {
155170
status: 200,
156171
headers: expectedResponseHeaders,
@@ -166,6 +181,9 @@ describe('onCallHandler', () => {
166181
callableFunction: (data, context) => {
167182
return;
168183
},
184+
callableFunction2: (request) => {
185+
return;
186+
},
169187
expectedHttpResponse: {
170188
status: 200,
171189
headers: expectedResponseHeaders,
@@ -183,6 +201,9 @@ describe('onCallHandler', () => {
183201
callableFunction: (data, context) => {
184202
return;
185203
},
204+
callableFunction2: (request) => {
205+
return;
206+
},
186207
expectedHttpResponse: {
187208
status: 400,
188209
headers: expectedResponseHeaders,
@@ -200,6 +221,9 @@ describe('onCallHandler', () => {
200221
callableFunction: (data, context) => {
201222
return;
202223
},
224+
callableFunction2: (request) => {
225+
return;
226+
},
203227
expectedHttpResponse: {
204228
status: 200,
205229
headers: expectedResponseHeaders,
@@ -215,6 +239,9 @@ describe('onCallHandler', () => {
215239
callableFunction: (data, context) => {
216240
return;
217241
},
242+
callableFunction2: (request) => {
243+
return;
244+
},
218245
expectedHttpResponse: {
219246
status: 400,
220247
headers: expectedResponseHeaders,
@@ -234,6 +261,9 @@ describe('onCallHandler', () => {
234261
callableFunction: (data, context) => {
235262
return;
236263
},
264+
callableFunction2: (request) => {
265+
return;
266+
},
237267
expectedHttpResponse: {
238268
status: 400,
239269
headers: expectedResponseHeaders,
@@ -251,6 +281,9 @@ describe('onCallHandler', () => {
251281
callableFunction: (data, context) => {
252282
throw new Error(`ceci n'est pas une error`);
253283
},
284+
callableFunction2: (request) => {
285+
throw new Error(`cece n'est pas une error`);
286+
},
254287
expectedHttpResponse: {
255288
status: 500,
256289
headers: expectedResponseHeaders,
@@ -266,6 +299,9 @@ describe('onCallHandler', () => {
266299
callableFunction: (data, context) => {
267300
throw new https.HttpsError('THIS_IS_NOT_VALID' as any, 'nope');
268301
},
302+
callableFunction2: (request) => {
303+
throw new https.HttpsError('THIS_IS_NOT_VALID' as any, 'nope');
304+
},
269305
expectedHttpResponse: {
270306
status: 500,
271307
headers: expectedResponseHeaders,
@@ -281,6 +317,9 @@ describe('onCallHandler', () => {
281317
callableFunction: (data, context) => {
282318
throw new https.HttpsError('not-found', 'i am error');
283319
},
320+
callableFunction2: (request) => {
321+
throw new https.HttpsError('not-found', 'i am error');
322+
},
284323
expectedHttpResponse: {
285324
status: 404,
286325
headers: expectedResponseHeaders,
@@ -308,6 +347,16 @@ describe('onCallHandler', () => {
308347
expect(context.instanceIdToken).to.be.undefined;
309348
return null;
310349
},
350+
callableFunction2: (request) => {
351+
expect(request.auth).to.not.be.undefined;
352+
expect(request.auth).to.not.be.null;
353+
expect(request.auth.uid).to.equal(mocks.user_id);
354+
expect(request.auth.token.uid).to.equal(mocks.user_id);
355+
expect(request.auth.token.sub).to.equal(mocks.user_id);
356+
expect(request.auth.token.aud).to.equal(projectId);
357+
expect(request.instanceIdToken).to.be.undefined;
358+
return null;
359+
},
311360
expectedHttpResponse: {
312361
status: 200,
313362
headers: expectedResponseHeaders,
@@ -326,6 +375,9 @@ describe('onCallHandler', () => {
326375
callableFunction: (data, context) => {
327376
return;
328377
},
378+
callableFunction2: (request) => {
379+
return;
380+
},
329381
expectedHttpResponse: {
330382
status: 401,
331383
headers: expectedResponseHeaders,
@@ -360,6 +412,19 @@ describe('onCallHandler', () => {
360412
expect(context.instanceIdToken).to.be.undefined;
361413
return null;
362414
},
415+
callableFunction2: (request) => {
416+
expect(request.app).to.not.be.undefined;
417+
expect(request.app).to.not.be.null;
418+
expect(request.app.appId).to.equal(appId);
419+
expect(request.app.token.app_id).to.be.equal(appId);
420+
expect(request.app.token.sub).to.be.equal(appId);
421+
expect(request.app.token.aud).to.be.deep.equal([
422+
`projects/${projectId}`,
423+
]);
424+
expect(request.auth).to.be.undefined;
425+
expect(request.instanceIdToken).to.be.undefined;
426+
return null;
427+
},
363428
expectedHttpResponse: {
364429
status: 200,
365430
headers: expectedResponseHeaders,
@@ -378,6 +443,9 @@ describe('onCallHandler', () => {
378443
callableFunction: (data, context) => {
379444
return;
380445
},
446+
callableFunction2: (request) => {
447+
return;
448+
},
381449
expectedHttpResponse: {
382450
status: 401,
383451
headers: expectedResponseHeaders,
@@ -402,6 +470,11 @@ describe('onCallHandler', () => {
402470
expect(context.instanceIdToken).to.equal('iid-token');
403471
return null;
404472
},
473+
callableFunction2: (request) => {
474+
expect(request.auth).to.be.undefined;
475+
expect(request.instanceIdToken).to.equal('iid-token');
476+
return null;
477+
},
405478
expectedHttpResponse: {
406479
status: 200,
407480
headers: expectedResponseHeaders,
@@ -420,6 +493,11 @@ describe('onCallHandler', () => {
420493
expect(context.rawRequest).to.equal(mockReq);
421494
return null;
422495
},
496+
callableFunction2: (request) => {
497+
expect(request.rawRequest).to.not.be.undefined;
498+
expect(request.rawRequest).to.equal(mockReq);
499+
return null;
500+
},
423501
expectedHttpResponse: {
424502
status: 200,
425503
headers: expectedResponseHeaders,
@@ -477,7 +555,6 @@ describe('encoding/decoding', () => {
477555
it('encodes double', () => {
478556
expect(https.encode(1.2)).to.equal(1.2);
479557
});
480-
481558
it('decodes double', () => {
482559
expect(https.decode(1.2)).to.equal(1.2);
483560
});

spec/v2/providers/https.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe('onCall', () => {
194194
});
195195

196196
it('should return a minimal trigger with appropriate values', () => {
197-
const result = https.onCall((data, context) => 42);
197+
const result = https.onCall((request) => 42);
198198
expect(result.__trigger).to.deep.equal({
199199
apiVersion: 2,
200200
platform: 'gcfv2',
@@ -208,7 +208,7 @@ describe('onCall', () => {
208208
});
209209

210210
it('should create a complex trigger with appropriate values', () => {
211-
const result = https.onCall(FULL_OPTIONS, (data, context) => 42);
211+
const result = https.onCall(FULL_OPTIONS, (request) => 42);
212212
expect(result.__trigger).to.deep.equal({
213213
...FULL_TRIGGER,
214214
httpsTrigger: {
@@ -233,7 +233,7 @@ describe('onCall', () => {
233233
region: ['us-west1', 'us-central1'],
234234
minInstances: 3,
235235
},
236-
(data, context) => 42
236+
(request) => 42
237237
);
238238

239239
expect(result.__trigger).to.deep.equal({
@@ -252,23 +252,23 @@ describe('onCall', () => {
252252
});
253253

254254
it('has a .run method', () => {
255-
const cf = https.onCall((d, c) => {
256-
return { data: d, context: c };
255+
const cf = https.onCall((request) => {
256+
return request;
257257
});
258258

259-
const data = 'data';
260-
const context: any = {
259+
const request: any = {
260+
data: 'data',
261261
instanceIdToken: 'token',
262262
auth: {
263263
uid: 'abc',
264264
token: 'token',
265265
},
266266
};
267-
expect(cf.run(data, context)).to.deep.equal({ data, context });
267+
expect(cf.run(request)).to.deep.equal(request);
268268
});
269269

270270
it('should be an express handler', async () => {
271-
const func = https.onCall((data, context) => 42);
271+
const func = https.onCall((request) => 42);
272272

273273
const req = new MockRequest(
274274
{
@@ -286,7 +286,7 @@ describe('onCall', () => {
286286
});
287287

288288
it('should enforce CORS options', async () => {
289-
const func = https.onCall({ cors: 'example.com' }, (req, res) => {
289+
const func = https.onCall({ cors: 'example.com' }, (request) => {
290290
throw new Error('Should not reach here for OPTIONS preflight');
291291
});
292292

@@ -314,7 +314,7 @@ describe('onCall', () => {
314314
});
315315

316316
it('adds CORS headers', async () => {
317-
const func = https.onCall((data, context) => 42);
317+
const func = https.onCall((request) => 42);
318318
const req = new MockRequest(
319319
{
320320
data: {},

0 commit comments

Comments
 (0)