index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const assert = require('assert');
  2. const functions = require('firebase-functions');
  3. exports.dataTest = functions.https.onRequest((request, response) => {
  4. assert.deepEqual(request.body, {
  5. data: {
  6. bool: true,
  7. int: 2,
  8. long: {
  9. value: '3',
  10. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  11. },
  12. string: 'four',
  13. array: [5, 6],
  14. 'null': null,
  15. }
  16. });
  17. response.send({
  18. data: {
  19. message: 'stub response',
  20. code: 42,
  21. long: {
  22. value: '420',
  23. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  24. },
  25. }
  26. });
  27. });
  28. exports.scalarTest = functions.https.onRequest((request, response) => {
  29. assert.deepEqual(request.body, { data: 17 });
  30. response.send({ data: 76 });
  31. });
  32. exports.tokenTest = functions.https.onRequest((request, response) => {
  33. assert.equal('Bearer token', request.get('Authorization'));
  34. assert.deepEqual(request.body, { data: {} });
  35. response.send({ data: {} });
  36. });
  37. exports.instanceIdTest = functions.https.onRequest((request, response) => {
  38. assert.equal(request.get('Firebase-Instance-ID-Token'), 'iid');
  39. assert.deepEqual(request.body, { data: {} });
  40. response.send({ data: {} });
  41. });
  42. exports.nullTest = functions.https.onRequest((request, response) => {
  43. assert.deepEqual(request.body, { data: null });
  44. response.send({ data: null });
  45. });
  46. exports.missingResultTest = functions.https.onRequest((request, response) => {
  47. assert.deepEqual(request.body, { data: null });
  48. response.send({});
  49. });
  50. exports.unhandledErrorTest = functions.https.onRequest((request, response) => {
  51. // Fail in a way that the client shouldn't see.
  52. throw 'nope';
  53. });
  54. exports.unknownErrorTest = functions.https.onRequest((request, response) => {
  55. // Send an http error with a body with an explicit code.
  56. response.status(400).send({
  57. error: {
  58. status: 'THIS_IS_NOT_VALID',
  59. message: 'this should be ignored',
  60. },
  61. });
  62. });
  63. exports.explicitErrorTest = functions.https.onRequest((request, response) => {
  64. // Send an http error with a body with an explicit code.
  65. // Note that eventually the SDK will have a helper to automatically return
  66. // the appropriate http status code for an error.
  67. response.status(400).send({
  68. error: {
  69. status: 'OUT_OF_RANGE',
  70. message: 'explicit nope',
  71. details: {
  72. start: 10,
  73. end: 20,
  74. long: {
  75. value: '30',
  76. '@type': 'type.googleapis.com/google.protobuf.Int64Value',
  77. },
  78. },
  79. },
  80. });
  81. });
  82. exports.httpErrorTest = functions.https.onRequest((request, response) => {
  83. // Send an http error with no body.
  84. response.status(400).send();
  85. });