index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2018 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. const assert = require('assert');
  15. const functions = require('firebase-functions');
  16. exports.dataTest = functions.https.onRequest((request, response) => {
  17. assert.deepEqual(request.body, {
  18. data: {
  19. bool: true,
  20. int: 2,
  21. long: {
  22. value: '9876543210',
  23. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  24. },
  25. string: 'four',
  26. array: [5, 6],
  27. 'null': null,
  28. }
  29. });
  30. response.send({
  31. data: {
  32. message: 'stub response',
  33. code: 42,
  34. long: {
  35. value: '420',
  36. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  37. },
  38. }
  39. });
  40. });
  41. exports.scalarTest = functions.https.onRequest((request, response) => {
  42. assert.deepEqual(request.body, { data: 17 });
  43. response.send({ data: 76 });
  44. });
  45. exports.tokenTest = functions.https.onRequest((request, response) => {
  46. assert.equal('Bearer token', request.get('Authorization'));
  47. assert.deepEqual(request.body, { data: {} });
  48. response.send({ data: {} });
  49. });
  50. exports.FCMTokenTest = functions.https.onRequest((request, response) => {
  51. assert.equal(request.get('Firebase-Instance-ID-Token'), 'fakeFCMToken');
  52. assert.deepEqual(request.body, { data: {} });
  53. response.send({ data: {} });
  54. });
  55. exports.nullTest = functions.https.onRequest((request, response) => {
  56. assert.deepEqual(request.body, { data: null });
  57. response.send({ data: null });
  58. });
  59. exports.missingResultTest = functions.https.onRequest((request, response) => {
  60. assert.deepEqual(request.body, { data: null });
  61. response.send({});
  62. });
  63. exports.unhandledErrorTest = functions.https.onRequest((request, response) => {
  64. // Fail in a way that the client shouldn't see.
  65. throw 'nope';
  66. });
  67. exports.unknownErrorTest = functions.https.onRequest((request, response) => {
  68. // Send an http error with a body with an explicit code.
  69. response.status(400).send({
  70. error: {
  71. status: 'THIS_IS_NOT_VALID',
  72. message: 'this should be ignored',
  73. },
  74. });
  75. });
  76. exports.explicitErrorTest = functions.https.onRequest((request, response) => {
  77. // Send an http error with a body with an explicit code.
  78. // Note that eventually the SDK will have a helper to automatically return
  79. // the appropriate http status code for an error.
  80. response.status(400).send({
  81. error: {
  82. status: 'OUT_OF_RANGE',
  83. message: 'explicit nope',
  84. details: {
  85. start: 10,
  86. end: 20,
  87. long: {
  88. value: '30',
  89. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  90. },
  91. },
  92. },
  93. });
  94. });
  95. exports.httpErrorTest = functions.https.onRequest((request, response) => {
  96. // Send an http error with no body.
  97. response.status(400).send();
  98. });
  99. exports.timeoutTest = functions.https.onRequest((request, response) => {
  100. // Wait for longer than 500ms.
  101. setTimeout(() => response.send({data: true}), 500);
  102. });