|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const assert = require('assert'); |
| 18 | +const cors = require('cors')({ origin: true }); |
| 19 | +const functions = require('firebase-functions'); |
| 20 | + |
| 21 | +/* |
| 22 | + * These backend test helpers are copied from the iOS and Android SDKs, but are |
| 23 | + * not all implemented as tests in the JS SDK yet. |
| 24 | + */ |
| 25 | + |
| 26 | +exports.dataTest = functions.https.onRequest((request, response) => { |
| 27 | + cors(request, response, () => { |
| 28 | + assert.deepEqual(request.body, { |
| 29 | + data: { |
| 30 | + bool: true, |
| 31 | + int: 2, |
| 32 | + // TODO(klimt): Should we add an API for encoding longs? |
| 33 | + /*long: { |
| 34 | + value: '3', |
| 35 | + '@type': 'type.googleapis.com/google.protobuf.Int64Value', |
| 36 | + },*/ |
| 37 | + string: 'four', |
| 38 | + array: [5, 6], |
| 39 | + null: null |
| 40 | + } |
| 41 | + }); |
| 42 | + response.send({ |
| 43 | + data: { |
| 44 | + message: 'stub response', |
| 45 | + code: 42, |
| 46 | + long: { |
| 47 | + value: '420', |
| 48 | + '@type': 'type.googleapis.com/google.protobuf.Int64Value' |
| 49 | + } |
| 50 | + } |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +exports.scalarTest = functions.https.onRequest((request, response) => { |
| 56 | + cors(request, response, () => { |
| 57 | + assert.deepEqual(request.body, { data: 17 }); |
| 58 | + response.send({ data: 76 }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +exports.tokenTest = functions.https.onRequest((request, response) => { |
| 63 | + cors(request, response, () => { |
| 64 | + assert.equal(request.get('Authorization'), 'Bearer token'); |
| 65 | + assert.deepEqual(request.body, { data: {} }); |
| 66 | + response.send({ data: {} }); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +exports.instanceIdTest = functions.https.onRequest((request, response) => { |
| 71 | + cors(request, response, () => { |
| 72 | + assert.equal(request.get('Firebase-Instance-ID-Token'), 'iid'); |
| 73 | + assert.deepEqual(request.body, { data: {} }); |
| 74 | + response.send({ data: {} }); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +exports.nullTest = functions.https.onRequest((request, response) => { |
| 79 | + cors(request, response, () => { |
| 80 | + assert.deepEqual(request.body, { data: null }); |
| 81 | + response.send({ data: null }); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +exports.missingResultTest = functions.https.onRequest((request, response) => { |
| 86 | + cors(request, response, () => { |
| 87 | + assert.deepEqual(request.body, { data: null }); |
| 88 | + response.send({}); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +exports.unhandledErrorTest = functions.https.onRequest((request, response) => { |
| 93 | + cors(request, response, () => { |
| 94 | + // Fail in a way that the client shouldn't see. |
| 95 | + throw 'nope'; |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +exports.unknownErrorTest = functions.https.onRequest((request, response) => { |
| 100 | + cors(request, response, () => { |
| 101 | + // Send an http error with a body with an explicit code. |
| 102 | + response.status(400).send({ |
| 103 | + error: { |
| 104 | + status: 'THIS_IS_NOT_VALID', |
| 105 | + message: 'this should be ignored' |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +exports.explicitErrorTest = functions.https.onRequest((request, response) => { |
| 112 | + cors(request, response, () => { |
| 113 | + // Send an http error with a body with an explicit code. |
| 114 | + response.status(400).send({ |
| 115 | + error: { |
| 116 | + status: 'OUT_OF_RANGE', |
| 117 | + message: 'explicit nope', |
| 118 | + details: { |
| 119 | + start: 10, |
| 120 | + end: 20, |
| 121 | + long: { |
| 122 | + value: '30', |
| 123 | + '@type': 'type.googleapis.com/google.protobuf.Int64Value' |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +exports.httpErrorTest = functions.https.onRequest((request, response) => { |
| 132 | + cors(request, response, () => { |
| 133 | + // Send an http error with no body. |
| 134 | + response.status(400).send(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments