|
| 1 | +// Copyright 2019 Google LLC |
| 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 | + |
| 15 | +import * as assert from 'assert'; |
| 16 | +import * as functions from '../../src/functions'; |
| 17 | +import {getServer} from '../../src/server'; |
| 18 | +import {SignatureType} from '../../src/types'; |
| 19 | +import * as supertest from 'supertest'; |
| 20 | + |
| 21 | +const TEST_CLOUD_EVENT = { |
| 22 | + specversion: '1.0', |
| 23 | + type: 'com.google.cloud.storage', |
| 24 | + source: 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs', |
| 25 | + subject: 'test-subject', |
| 26 | + id: 'test-1234-1234', |
| 27 | + time: '2020-05-13T01:23:45Z', |
| 28 | + datacontenttype: 'application/json', |
| 29 | + data: { |
| 30 | + some: 'payload', |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +describe('Event Function', () => { |
| 35 | + const testData = [ |
| 36 | + { |
| 37 | + name: 'GCF event', |
| 38 | + headers: {}, |
| 39 | + body: { |
| 40 | + context: { |
| 41 | + eventId: 'testEventId', |
| 42 | + timestamp: 'testTimestamp', |
| 43 | + eventType: 'testEventType', |
| 44 | + resource: 'testResource', |
| 45 | + }, |
| 46 | + data: {some: 'payload'}, |
| 47 | + }, |
| 48 | + expectedResource: { |
| 49 | + eventId: 'testEventId', |
| 50 | + eventType: 'testEventType', |
| 51 | + resource: 'testResource', |
| 52 | + timestamp: 'testTimestamp', |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'GCF legacy event', |
| 57 | + headers: {}, |
| 58 | + body: { |
| 59 | + eventId: 'testEventId', |
| 60 | + timestamp: 'testTimestamp', |
| 61 | + eventType: 'testEventType', |
| 62 | + resource: 'testResource', |
| 63 | + data: {some: 'payload'}, |
| 64 | + }, |
| 65 | + expectedResource: { |
| 66 | + eventId: 'testEventId', |
| 67 | + eventType: 'testEventType', |
| 68 | + resource: 'testResource', |
| 69 | + timestamp: 'testTimestamp', |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: 'GCF event with resource JSON', |
| 74 | + headers: {}, |
| 75 | + body: { |
| 76 | + context: { |
| 77 | + eventId: 'testEventId', |
| 78 | + timestamp: 'testTimestamp', |
| 79 | + eventType: 'testEventType', |
| 80 | + resource: { |
| 81 | + service: 'testService', |
| 82 | + name: 'testName', |
| 83 | + type: 'testType', |
| 84 | + }, |
| 85 | + }, |
| 86 | + data: {some: 'payload'}, |
| 87 | + }, |
| 88 | + expectedResource: { |
| 89 | + eventId: 'testEventId', |
| 90 | + eventType: 'testEventType', |
| 91 | + resource: { |
| 92 | + name: 'testName', |
| 93 | + service: 'testService', |
| 94 | + type: 'testType', |
| 95 | + }, |
| 96 | + timestamp: 'testTimestamp', |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: 'CloudEvents v1.0 structured content request', |
| 101 | + headers: {'Content-Type': 'application/cloudevents+json'}, |
| 102 | + body: TEST_CLOUD_EVENT, |
| 103 | + expectedResource: { |
| 104 | + datacontenttype: 'application/json', |
| 105 | + id: 'test-1234-1234', |
| 106 | + source: |
| 107 | + 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs', |
| 108 | + specversion: '1.0', |
| 109 | + subject: 'test-subject', |
| 110 | + time: '2020-05-13T01:23:45Z', |
| 111 | + type: 'com.google.cloud.storage', |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: 'CloudEvents v1.0 binary content request', |
| 116 | + headers: { |
| 117 | + 'Content-Type': 'application/cloudevents+json', |
| 118 | + 'ce-specversion': TEST_CLOUD_EVENT.specversion, |
| 119 | + 'ce-type': TEST_CLOUD_EVENT.type, |
| 120 | + 'ce-source': TEST_CLOUD_EVENT.source, |
| 121 | + 'ce-subject': TEST_CLOUD_EVENT.subject, |
| 122 | + 'ce-id': TEST_CLOUD_EVENT.id, |
| 123 | + 'ce-time': TEST_CLOUD_EVENT.time, |
| 124 | + 'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype, |
| 125 | + }, |
| 126 | + body: TEST_CLOUD_EVENT.data, |
| 127 | + expectedResource: { |
| 128 | + datacontenttype: 'application/json', |
| 129 | + id: 'test-1234-1234', |
| 130 | + source: |
| 131 | + 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs', |
| 132 | + specversion: '1.0', |
| 133 | + subject: 'test-subject', |
| 134 | + time: '2020-05-13T01:23:45Z', |
| 135 | + type: 'com.google.cloud.storage', |
| 136 | + }, |
| 137 | + }, |
| 138 | + ]; |
| 139 | + testData.forEach(test => { |
| 140 | + it(test.name, async () => { |
| 141 | + let receivedData: {} | null = null; |
| 142 | + let receivedContext: functions.CloudFunctionsContext | null = null; |
| 143 | + const server = getServer((data: {}, context: functions.Context) => { |
| 144 | + receivedData = data; |
| 145 | + receivedContext = context as functions.CloudFunctionsContext; |
| 146 | + }, SignatureType.EVENT); |
| 147 | + const requestHeaders = { |
| 148 | + 'Content-Type': 'application/json', |
| 149 | + ...test.headers, |
| 150 | + }; |
| 151 | + await supertest(server) |
| 152 | + .post('/') |
| 153 | + .send(test.body) |
| 154 | + .set(requestHeaders) |
| 155 | + .expect(204); |
| 156 | + assert.deepStrictEqual(receivedData, {some: 'payload'}); |
| 157 | + assert.deepStrictEqual(receivedContext, test.expectedResource); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments