From 0cce9f0b110f98fc19f4398c68363b6eadeebf51 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Tue, 1 Aug 2023 22:07:01 -0400 Subject: [PATCH] support merging Response status property --- packages/cli/src/lib/resource-utils.js | 4 ++- packages/cli/src/lifecycles/serve.js | 2 +- .../develop.default/develop.default.spec.js | 30 ++++++++++++++++- .../cases/develop.default/src/api/missing.js | 3 ++ .../serve.default.api.spec.js | 33 +++++++++++++++++++ .../serve.default.api/src/api/missing.js | 3 ++ 6 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 packages/cli/test/cases/develop.default/src/api/missing.js create mode 100644 packages/cli/test/cases/serve.default.api/src/api/missing.js diff --git a/packages/cli/src/lib/resource-utils.js b/packages/cli/src/lib/resource-utils.js index 6f84e415e..969c2adf7 100644 --- a/packages/cli/src/lib/resource-utils.js +++ b/packages/cli/src/lib/resource-utils.js @@ -36,6 +36,7 @@ async function modelResource(context, type, src = undefined, contents = undefine function mergeResponse(destination, source) { const headers = destination.headers || new Headers(); + const status = source.status || destination.status; source.headers.forEach((value, key) => { // TODO better way to handle Response automatically setting content-type @@ -50,7 +51,8 @@ function mergeResponse(destination, source) { // TODO handle merging in state (aborted, type, status, etc) // https://github.com/ProjectEvergreen/greenwood/issues/1048 return new Response(source.body, { - headers + headers, + status }); } diff --git a/packages/cli/src/lifecycles/serve.js b/packages/cli/src/lifecycles/serve.js index 06ca8fb9b..dda7a599f 100644 --- a/packages/cli/src/lifecycles/serve.js +++ b/packages/cli/src/lifecycles/serve.js @@ -313,7 +313,7 @@ async function getHybridServer(compilation) { const { handler } = await import(new URL(`.${apiRoute.path}`, outputDir)); const response = await handler(request); - ctx.status = 200; + ctx.status = response.status; ctx.set('Content-Type', response.headers.get('Content-Type')); ctx.body = Readable.from(response.body); } diff --git a/packages/cli/test/cases/develop.default/develop.default.spec.js b/packages/cli/test/cases/develop.default/develop.default.spec.js index 9735f6fd3..e39e1f490 100644 --- a/packages/cli/test/cases/develop.default/develop.default.spec.js +++ b/packages/cli/test/cases/develop.default/develop.default.spec.js @@ -19,6 +19,7 @@ * src/ * api/ * greeting.js + * missing.js * assets/ * data.json * favicon.ico @@ -74,7 +75,7 @@ describe('Develop Greenwood With: ', function() { this.context = { hostname: `${hostname}:${port}` }; - runner = new Runner(); + runner = new Runner(true); }); describe(LABEL, function() { @@ -1348,6 +1349,33 @@ describe('Develop Greenwood With: ', function() { done(); }); }); + + describe('Develop command with API specific behaviors with a custom response', function() { + let response = {}; + + before(async function() { + return new Promise((resolve, reject) => { + request.get(`${hostname}:${port}/api/missing`, (err, res) => { + if (err) { + reject(); + } + + response = res; + resolve(); + }); + }); + }); + + it('should return a 404 status', function(done) { + expect(response.statusCode).to.equal(404); + done(); + }); + + it('should return a body of not found', function(done) { + expect(response.body).to.equal('Not Found'); + done(); + }); + }); }); after(function() { diff --git a/packages/cli/test/cases/develop.default/src/api/missing.js b/packages/cli/test/cases/develop.default/src/api/missing.js new file mode 100644 index 000000000..fe2c58c32 --- /dev/null +++ b/packages/cli/test/cases/develop.default/src/api/missing.js @@ -0,0 +1,3 @@ +export async function handler() { + return new Response('Not Found', { status: 404 }); +} \ No newline at end of file diff --git a/packages/cli/test/cases/serve.default.api/serve.default.api.spec.js b/packages/cli/test/cases/serve.default.api/serve.default.api.spec.js index 9144881e5..72939ab91 100644 --- a/packages/cli/test/cases/serve.default.api/serve.default.api.spec.js +++ b/packages/cli/test/cases/serve.default.api/serve.default.api.spec.js @@ -14,7 +14,11 @@ * User Workspace * src/ * api/ + * fragment.js * greeting.js + * missing.js + * components/ + * card.js */ import chai from 'chai'; import path from 'path'; @@ -152,6 +156,35 @@ describe('Serve Greenwood With: ', function() { done(); }); }); + + describe('Serve command with API specific behaviors with a custom response', function() { + let response = {}; + + before(async function() { + return new Promise((resolve, reject) => { + request.get(`${hostname}/api/missing`, (err, res, body) => { + if (err) { + reject(); + } + + response = res; + response.body = body; + + resolve(); + }); + }); + }); + + it('should return a 404 status', function(done) { + expect(response.statusCode).to.equal(404); + done(); + }); + + it('should return a body of not found', function(done) { + expect(response.body).to.equal('Not Found'); + done(); + }); + }); }); after(function() { diff --git a/packages/cli/test/cases/serve.default.api/src/api/missing.js b/packages/cli/test/cases/serve.default.api/src/api/missing.js new file mode 100644 index 000000000..fe2c58c32 --- /dev/null +++ b/packages/cli/test/cases/serve.default.api/src/api/missing.js @@ -0,0 +1,3 @@ +export async function handler() { + return new Response('Not Found', { status: 404 }); +} \ No newline at end of file