Skip to content

Commit

Permalink
support merging Response status property
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Aug 2, 2023
1 parent 3d2f640 commit 0cce9f0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/cli/src/lib/resource-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lifecycles/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
30 changes: 29 additions & 1 deletion packages/cli/test/cases/develop.default/develop.default.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* src/
* api/
* greeting.js
* missing.js
* assets/
* data.json
* favicon.ico
Expand Down Expand Up @@ -74,7 +75,7 @@ describe('Develop Greenwood With: ', function() {
this.context = {
hostname: `${hostname}:${port}`
};
runner = new Runner();
runner = new Runner(true);
});

describe(LABEL, function() {
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/test/cases/develop.default/src/api/missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function handler() {
return new Response('Not Found', { status: 404 });
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/test/cases/serve.default.api/src/api/missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function handler() {
return new Response('Not Found', { status: 404 });
}

0 comments on commit 0cce9f0

Please sign in to comment.