index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 functionsV1 = require('firebase-functions/v1');
  16. const functionsV2 = require('firebase-functions/v2');
  17. exports.dataTest = functionsV1.https.onRequest((request, response) => {
  18. assert.deepEqual(request.body, {
  19. data: {
  20. bool: true,
  21. int: 2,
  22. long: {
  23. value: '9876543210',
  24. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  25. },
  26. string: 'four',
  27. array: [5, 6],
  28. 'null': null,
  29. }
  30. });
  31. response.send({
  32. data: {
  33. message: 'stub response',
  34. code: 42,
  35. long: {
  36. value: '420',
  37. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  38. },
  39. }
  40. });
  41. });
  42. exports.scalarTest = functionsV1.https.onRequest((request, response) => {
  43. assert.deepEqual(request.body, { data: 17 });
  44. response.send({ data: 76 });
  45. });
  46. exports.tokenTest = functionsV1.https.onRequest((request, response) => {
  47. assert.equal('Bearer token', request.get('Authorization'));
  48. assert.deepEqual(request.body, { data: {} });
  49. response.send({ data: {} });
  50. });
  51. exports.FCMTokenTest = functionsV1.https.onRequest((request, response) => {
  52. assert.equal(request.get('Firebase-Instance-ID-Token'), 'fakeFCMToken');
  53. assert.deepEqual(request.body, { data: {} });
  54. response.send({ data: {} });
  55. });
  56. exports.nullTest = functionsV1.https.onRequest((request, response) => {
  57. assert.deepEqual(request.body, { data: null });
  58. response.send({ data: null });
  59. });
  60. exports.missingResultTest = functionsV1.https.onRequest((request, response) => {
  61. assert.deepEqual(request.body, { data: null });
  62. response.send({});
  63. });
  64. exports.unhandledErrorTest = functionsV1.https.onRequest((request, response) => {
  65. // Fail in a way that the client shouldn't see.
  66. throw 'nope';
  67. });
  68. exports.unknownErrorTest = functionsV1.https.onRequest((request, response) => {
  69. // Send an http error with a body with an explicit code.
  70. response.status(400).send({
  71. error: {
  72. status: 'THIS_IS_NOT_VALID',
  73. message: 'this should be ignored',
  74. },
  75. });
  76. });
  77. exports.explicitErrorTest = functionsV1.https.onRequest((request, response) => {
  78. // Send an http error with a body with an explicit code.
  79. // Note that eventually the SDK will have a helper to automatically return
  80. // the appropriate http status code for an error.
  81. response.status(400).send({
  82. error: {
  83. status: 'OUT_OF_RANGE',
  84. message: 'explicit nope',
  85. details: {
  86. start: 10,
  87. end: 20,
  88. long: {
  89. value: '30',
  90. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  91. },
  92. },
  93. },
  94. });
  95. });
  96. exports.httpErrorTest = functionsV1.https.onRequest((request, response) => {
  97. // Send an http error with no body.
  98. response.status(400).send();
  99. });
  100. // Regression test for https://github.com/firebase/firebase-ios-sdk/issues/9855
  101. exports.throwTest = functionsV1.https.onCall((data) => {
  102. throw new functionsV1.https.HttpsError('invalid-argument', 'Invalid test requested.');
  103. });
  104. exports.timeoutTest = functionsV1.https.onRequest((request, response) => {
  105. // Wait for longer than 500ms.
  106. setTimeout(() => response.send({ data: true }), 500);
  107. });
  108. const streamData = ["hello", "world", "this", "is", "cool"]
  109. function sleep(ms) {
  110. return new Promise(resolve => setTimeout(resolve, ms));
  111. };
  112. async function* generateText() {
  113. for (const chunk of streamData) {
  114. yield chunk;
  115. await sleep(1000);
  116. }
  117. };
  118. exports.genStream = functionsV2.https.onCall(
  119. async (request, response) => {
  120. if (request.acceptsStreaming) {
  121. for await (const chunk of generateText()) {
  122. response.sendChunk({ chunk });
  123. }
  124. }
  125. return data.join(" ");
  126. }
  127. );
  128. exports.genStreamError = functionsV2.https.onCall(
  129. async (request, response) => {
  130. if (request.acceptsStreaming) {
  131. for await (const chunk of generateText()) {
  132. response.write({ chunk });
  133. }
  134. throw Error("BOOM")
  135. }
  136. }
  137. );