|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import { expect } from 'chai'; |
| 18 | +import { FirebaseApp } from '@firebase/app-types'; |
| 19 | +import { FunctionsErrorCode } from '@firebase/functions-types-exp'; |
| 20 | +import { createTestService } from '../test/utils'; |
| 21 | +import { firebase } from '@firebase/app-compat'; |
| 22 | + |
| 23 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
| 24 | +export const TEST_PROJECT = require('../../../config/project.json'); |
| 25 | + |
| 26 | +// Chai doesn't handle Error comparisons in a useful way. |
| 27 | +// https://github.com/chaijs/chai/issues/608 |
| 28 | +async function expectError( |
| 29 | + promise: Promise<any>, |
| 30 | + code: FunctionsErrorCode, |
| 31 | + message: string, |
| 32 | + details?: any |
| 33 | +): Promise<void> { |
| 34 | + let failed = false; |
| 35 | + try { |
| 36 | + await promise; |
| 37 | + } catch (e) { |
| 38 | + failed = true; |
| 39 | + // Errors coming from callable functions usually have the functions-exp |
| 40 | + // code in the message since it's thrown inside functions-exp. |
| 41 | + expect(e.code).to.match(new RegExp(`functions.*/${code}`)); |
| 42 | + expect(e.message).to.equal(message); |
| 43 | + expect(e.details).to.deep.equal(details); |
| 44 | + } |
| 45 | + if (!failed) { |
| 46 | + expect(false, 'Promise should have failed.').to.be.true; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +describe('Firebase Functions > Call', () => { |
| 51 | + let app: FirebaseApp; |
| 52 | + const region = 'us-central1'; |
| 53 | + |
| 54 | + before(() => { |
| 55 | + const useEmulator = !!process.env.FIREBASE_FUNCTIONS_EMULATOR_ORIGIN; |
| 56 | + const projectId = useEmulator |
| 57 | + ? 'functions-integration-test' |
| 58 | + : TEST_PROJECT.projectId; |
| 59 | + const messagingSenderId = 'messaging-sender-id'; |
| 60 | + |
| 61 | + app = firebase.initializeApp({ projectId, messagingSenderId }); |
| 62 | + }); |
| 63 | + |
| 64 | + after(async () => { |
| 65 | + await app.delete(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('simple data', async () => { |
| 69 | + const functions = createTestService(app, region); |
| 70 | + // TODO(klimt): Should we add an API to create a "long" in JS? |
| 71 | + const data = { |
| 72 | + bool: true, |
| 73 | + int: 2, |
| 74 | + str: 'four', |
| 75 | + array: [5, 6], |
| 76 | + null: null |
| 77 | + }; |
| 78 | + |
| 79 | + const func = functions.httpsCallable('dataTest'); |
| 80 | + const result = await func(data); |
| 81 | + |
| 82 | + expect(result.data).to.deep.equal({ |
| 83 | + message: 'stub response', |
| 84 | + code: 42, |
| 85 | + long: 420 |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('scalars', async () => { |
| 90 | + const functions = createTestService(app, region); |
| 91 | + const func = functions.httpsCallable('scalarTest'); |
| 92 | + const result = await func(17); |
| 93 | + expect(result.data).to.equal(76); |
| 94 | + }); |
| 95 | + |
| 96 | + it('null', async () => { |
| 97 | + const functions = createTestService(app, region); |
| 98 | + const func = functions.httpsCallable('nullTest'); |
| 99 | + let result = await func(null); |
| 100 | + expect(result.data).to.be.null; |
| 101 | + |
| 102 | + // Test with void arguments version. |
| 103 | + result = await func(); |
| 104 | + expect(result.data).to.be.null; |
| 105 | + }); |
| 106 | + |
| 107 | + it('missing result', async () => { |
| 108 | + const functions = createTestService(app, region); |
| 109 | + const func = functions.httpsCallable('missingResultTest'); |
| 110 | + await expectError(func(), 'internal', 'Response is missing data field.'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('unhandled error', async () => { |
| 114 | + const functions = createTestService(app, region); |
| 115 | + const func = functions.httpsCallable('unhandledErrorTest'); |
| 116 | + await expectError(func(), 'internal', 'internal'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('unknown error', async () => { |
| 120 | + const functions = createTestService(app, region); |
| 121 | + const func = functions.httpsCallable('unknownErrorTest'); |
| 122 | + await expectError(func(), 'internal', 'internal'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('explicit error', async () => { |
| 126 | + const functions = createTestService(app, region); |
| 127 | + const func = functions.httpsCallable('explicitErrorTest'); |
| 128 | + await expectError(func(), 'out-of-range', 'explicit nope', { |
| 129 | + start: 10, |
| 130 | + end: 20, |
| 131 | + long: 30 |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('http error', async () => { |
| 136 | + const functions = createTestService(app, region); |
| 137 | + const func = functions.httpsCallable('httpErrorTest'); |
| 138 | + await expectError(func(), 'invalid-argument', 'invalid-argument'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('timeout', async () => { |
| 142 | + const functions = createTestService(app, region); |
| 143 | + const func = functions.httpsCallable('timeoutTest', { timeout: 10 }); |
| 144 | + await expectError(func(), 'deadline-exceeded', 'deadline-exceeded'); |
| 145 | + }); |
| 146 | +}); |
0 commit comments