From acc5c562577a52cc0ee76a5037765e8cf04e457a Mon Sep 17 00:00:00 2001 From: Haili Zhang Date: Fri, 27 May 2022 17:50:22 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20refactor:=20polish=20http=20erro?= =?UTF-8?q?r=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Haili Zhang --- src/function_wrappers.ts | 2 +- src/types.ts | 2 +- test/integration/cloud_event.ts | 12 +++++++++--- test/integration/http.ts | 21 ++++++++++++++------- test/integration/http_binding.ts | 13 +++++++++++-- test/integration/legacy_event.ts | 12 +++++++++--- 6 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/function_wrappers.ts b/src/function_wrappers.ts index aaf95ff1..33e05c08 100644 --- a/src/function_wrappers.ts +++ b/src/function_wrappers.ts @@ -138,7 +138,7 @@ const wrapOpenFunction = ( Promise.resolve() .then(() => userFunction(ctx, req.body)) - .then(() => res.end()) + .then(result => callback(null, result)) .catch(err => callback(err, undefined)); }; diff --git a/src/types.ts b/src/types.ts index 44865026..84a32ffb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -14,7 +14,7 @@ // HTTP header field that is added to Worker response to signalize problems with // executing the client function. -export const FUNCTION_STATUS_HEADER_FIELD = 'X-Google-Status'; +export const FUNCTION_STATUS_HEADER_FIELD = 'X-OpenFunction-Status'; /** * List of function signature types that are supported by the framework. diff --git a/test/integration/cloud_event.ts b/test/integration/cloud_event.ts index ac239f14..c57fa782 100644 --- a/test/integration/cloud_event.ts +++ b/test/integration/cloud_event.ts @@ -13,10 +13,13 @@ // limitations under the License. import * as assert from 'assert'; -import * as functions from '../../src/index'; + +import * as supertest from 'supertest'; import * as sinon from 'sinon'; + +import * as functions from '../../src/index'; import {getTestServer} from '../../src/testing'; -import * as supertest from 'supertest'; +import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types'; // A structured CloudEvent const TEST_CLOUD_EVENT = { @@ -316,7 +319,10 @@ describe('CloudEvent Function', () => { .post('/') .send(TEST_CLOUD_EVENT) .expect(res => { - assert.deepStrictEqual(res.headers['x-google-status'], 'error'); + assert.deepStrictEqual( + res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()], + 'error' + ); assert.deepStrictEqual(res.body, {}); }) .expect(500); diff --git a/test/integration/http.ts b/test/integration/http.ts index f38c1dfb..6334410a 100644 --- a/test/integration/http.ts +++ b/test/integration/http.ts @@ -18,6 +18,7 @@ import * as supertest from 'supertest'; import * as functions from '../../src/index'; import {getTestServer} from '../../src/testing'; +import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types'; describe('HTTP Function', () => { let callCount = 0; @@ -99,13 +100,19 @@ describe('HTTP Function', () => { testData.forEach(test => { it(test.name, async () => { const st = supertest(getTestServer('testHttpFunction')); - await (test.httpVerb === 'GET' - ? st.get(test.path) - : st.post(test.path).send({text: 'hello'}) - ) - .set('Content-Type', 'application/json') - .expect(test.expectedBody) - .expect(test.expectedStatus); + try { + await (test.httpVerb === 'GET' + ? st.get(test.path) + : st.post(test.path).send({text: 'hello'}) + ) + .set('Content-Type', 'application/json') + .expect(test.expectedBody) + .expect(test.expectedStatus) + .expect(FUNCTION_STATUS_HEADER_FIELD, 'crash'); + } catch (err) { + test.expectedStatus === 500 && assert(err); + } + assert.strictEqual(callCount, test.expectedCallCount); }); }); diff --git a/test/integration/http_binding.ts b/test/integration/http_binding.ts index ad6f4a46..7c91aa2c 100644 --- a/test/integration/http_binding.ts +++ b/test/integration/http_binding.ts @@ -9,6 +9,7 @@ import {OpenFunctionContext} from '../../src/openfunction/function_context'; import {OpenFunctionRuntime} from '../../src/functions'; import {getServer} from '../../src/server'; +import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types'; const TEST_CONTEXT: OpenFunctionContext = { name: 'test-context', @@ -72,6 +73,7 @@ describe('OpenFunction - HTTP Binding', () => { {name: 'Save data', operation: 'create', listable: true}, {name: 'Get data', operation: 'get', listable: true}, {name: 'Delete data', operation: 'delete', listable: false}, + {name: 'Error data', operation: '', listable: false}, ]; testData.forEach(test => { @@ -83,6 +85,8 @@ describe('OpenFunction - HTTP Binding', () => { const server = getServer( async (ctx: OpenFunctionRuntime, data: {}) => { + if (!test.operation) throw new Error('I crashed'); + await ctx.send(data); ctx.res?.send(data); }, @@ -93,9 +97,14 @@ describe('OpenFunction - HTTP Binding', () => { await supertest(server) .post('/') .send(TEST_PAYLOAD) - .expect(200) + .expect(test.operation ? 200 : 500) .expect(res => { - deepStrictEqual(res.body, TEST_PAYLOAD); + !test.operation + ? deepStrictEqual( + res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()], + 'error' + ) + : deepStrictEqual(res.body, TEST_PAYLOAD); }); forEach(context.outputs, output => { diff --git a/test/integration/legacy_event.ts b/test/integration/legacy_event.ts index 89d3f3d3..f164b4f3 100644 --- a/test/integration/legacy_event.ts +++ b/test/integration/legacy_event.ts @@ -13,10 +13,13 @@ // limitations under the License. import * as assert from 'assert'; -import * as functions from '../../src/functions'; + +import * as supertest from 'supertest'; import * as sinon from 'sinon'; + +import * as functions from '../../src/index'; import {getServer} from '../../src/server'; -import * as supertest from 'supertest'; +import {FUNCTION_STATUS_HEADER_FIELD} from '../../src/types'; const TEST_CLOUD_EVENT = { specversion: '1.0', @@ -214,7 +217,10 @@ describe('Event Function', () => { }) .set({'Content-Type': 'application/json'}) .expect(res => { - assert.deepStrictEqual(res.headers['x-google-status'], 'error'); + assert.deepStrictEqual( + res.headers[FUNCTION_STATUS_HEADER_FIELD.toLowerCase()], + 'error' + ); assert.deepStrictEqual(res.body, {}); }) .expect(500);