Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug/conditionally check Response object properties and values in development and serve lifecycles #1131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/cli/src/lifecycles/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,20 @@ async function getHybridServer(compilation) {

ctx.body = Readable.from(response.body);
ctx.set('Content-Type', 'text/html');
// TODO should use status from response
// https://github.com/ProjectEvergreen/greenwood/issues/1048
ctx.status = 200;
} else if (isApiRoute) {
const apiRoute = manifest.apis.get(url.pathname);
const { handler } = await import(new URL(`.${apiRoute.path}`, outputDir));
const response = await handler(request);
const { body } = response;

// TODO should use status from response
// https://github.com/ProjectEvergreen/greenwood/issues/1048
ctx.body = body ? Readable.from(body) : null;
ctx.status = 200;
ctx.set('Content-Type', response.headers.get('Content-Type'));
ctx.body = Readable.from(response.body);
}
} catch (e) {
ctx.status = 500;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class NodeModulesResource extends ResourceInterface {
}

async shouldIntercept(url, request, response) {
return response.headers.get('Content-Type').indexOf('text/html') >= 0;
return response.headers.get('Content-Type')?.indexOf('text/html') >= 0;
}

async intercept(url, request, response) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/plugins/server/plugin-livereload.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class LiveReloadResource extends ResourceInterface {
async shouldIntercept(url, request, response) {
const contentType = response.headers.get('Content-Type');

return contentType.indexOf('text/html') >= 0 && process.env.__GWD_COMMAND__ === 'develop'; // eslint-disable-line no-underscore-dangle
return contentType?.indexOf('text/html') >= 0 && process.env.__GWD_COMMAND__ === 'develop'; // eslint-disable-line no-underscore-dangle
}

async intercept(url, request, response) {
Expand Down
23 changes: 23 additions & 0 deletions 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
* nothing.js
* assets/
* data.json
* favicon.ico
Expand Down Expand Up @@ -1348,6 +1349,28 @@ describe('Develop Greenwood With: ', function() {
done();
});
});

describe('Develop command with API specific behaviors with a minimal response', function() {
let response = {};

before(async function() {
return new Promise((resolve, reject) => {
request.get(`${hostname}:${port}/api/nothing`, (err, res) => {
if (err) {
reject();
}

response = res;
resolve();
});
});
});

it('should return a 200 status', function(done) {
expect(response.statusCode).to.equal(200);
done();
});
});
});

after(function() {
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/test/cases/develop.default/src/api/nothing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export async function handler() {
// TODO should support a non 200 status code
// https://github.com/ProjectEvergreen/greenwood/issues/1048
return new Response(undefined);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('Serve Greenwood With: ', function() {
this.context = {
hostname
};
runner = new Runner();
runner = new Runner(true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops :/

});

describe(LABEL, function() {
Expand Down Expand Up @@ -129,6 +129,28 @@ describe('Serve Greenwood With: ', function() {
});
});

describe('Serve command with API specific behaviors with a minimal response', function() {
let response = {};

before(async function() {
return new Promise((resolve, reject) => {
request.get(`${hostname}/api/nothing`, (err, res) => {
if (err) {
reject();
}

response = res;
resolve();
});
});
});

it('should return a 200 status', function(done) {
expect(response.statusCode).to.equal(200);
done();
});
});

describe('Serve command with API 404 not found behavior', function() {
let response = {};

Expand Down
3 changes: 3 additions & 0 deletions packages/cli/test/cases/serve.default.api/src/api/nothing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export async function handler() {
return new Response(undefined);
}
2 changes: 1 addition & 1 deletion packages/plugin-google-analytics/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GoogleAnalyticsResource extends ResourceInterface {
}

async shouldIntercept(url, request, response) {
return response.headers.get('Content-Type').indexOf(this.contentType) >= 0;
return response.headers.get('Content-Type')?.indexOf(this.contentType) >= 0;
}

async intercept(url, request, response) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-graphql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class GraphQLResource extends ResourceInterface {
}

async shouldIntercept(url, request, response) {
return response.headers.get('Content-Type').indexOf(this.contentType[1]) >= 0;
return response.headers.get('Content-Type')?.indexOf(this.contentType[1]) >= 0;
}

async intercept(url, request, response) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-include-html/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IncludeHtmlResource extends ResourceInterface {
}

async shouldIntercept(url, request, response) {
return response.headers.get('Content-Type').indexOf(this.contentType) >= 0;
return response.headers.get('Content-Type')?.indexOf(this.contentType) >= 0;
}

async intercept(url, request, response) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-polyfills/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PolyfillsResource extends ResourceInterface {

return isEnabled
&& protocol.startsWith('http')
&& response.headers.get('Content-Type').indexOf(this.contentType) >= 0;
&& response.headers.get('Content-Type')?.indexOf(this.contentType) >= 0;
}

async intercept(url, request, response) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-renderer-puppeteer/src/plugins/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PuppeteerResource extends ResourceInterface {

return process.env.__GWD_COMMAND__ === 'build' // eslint-disable-line no-underscore-dangle
&& protocol.startsWith('http')
&& response.headers.get('Content-Type').indexOf(this.contentType) >= 0;
&& response.headers.get('Content-Type')?.indexOf(this.contentType) >= 0;
}

async intercept(url, request, response) {
Expand Down
Loading