Skip to content

Commit

Permalink
Merge pull request #14 from CMU-313/vickyc/merge-p1-changes
Browse files Browse the repository at this point in the history
Vicky's merges for src/middleware/render.js from Project 1 and tests from tests/middleware.js
  • Loading branch information
vickyc2266 authored Sep 21, 2024
2 parents a30f699 + 017c5dc commit b877e68
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
Binary file added dump.rdb
Binary file not shown.
17 changes: 10 additions & 7 deletions src/middleware/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,15 @@ module.exports = function (middleware) {
res: res,
templateData: options,
});
if (res.headersSent) {
return;
}
const templateToRender = buildResult.templateData.templateToRender || template;

checkHeadersSent(res);
const templateToRender = buildResult.templateData.templateToRender || template;
const renderResult = await plugins.hooks.fire('filter:middleware.render', {
req: req,
res: res,
templateData: buildResult.templateData,
});
if (res.headersSent) {
return;
}
checkHeadersSent(res);
options = renderResult.templateData;
options._header = {
tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),
Expand Down Expand Up @@ -125,6 +121,13 @@ module.exports = function (middleware) {
next();
};

function checkHeadersSent(res) {
if (res.headersSent) {
return true;
}
return false;
}

async function getLoggedInUser(req) {
if (req.user) {
return await user.getUserData(req.uid);
Expand Down
71 changes: 71 additions & 0 deletions test/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,76 @@ describe('Middlewares', () => {
assert.strictEqual(response.headers['cache-control'], 'private');
});
});

/* New Test Case Added by Vicky */
describe('Render Method Middleware', () => {
let reqMock;
let resMock;
/* setting a mock versions of the req(request) and res (response) objects to simulate typical HTTP */
/* response and request without running an actual server */
beforeEach(() => {
/* reqMock is simulating request objects of properties of the users */
reqMock = {
uid: 1,
baseUrl: '',
path: '',
loggedIn: true,
/* representing the request route and app configuration */
route: {
path: '/',
},
app: {
set: () => {},
},
query: {},
};
/* resMock represents a response object */
resMock = {
locals: {},
/* set to false to show that the headers haven't been sent */
headersSent: false,
set: () => {},
/* mock function that simulates the behavior of rendering a template */
render: function (template, options, callback) {
callback(null, '<html></html>');
},
/* mocked methods that simulates sending a response */
json: () => {},
send: () => {},
};
});
/* this test case checks if the middleware correctly stops processing when headers were already sent */
/* by setting headersSent = true */
it('should not proceed if headers are already sent', async () => {
const middleware = require('../src/middleware');
/* Simulate headers already being sent */
resMock.headersSent = true;
let nextCalled = false;
const next = () => {
nextCalled = true;
};
/* calling the middleware main function with the Mock objects above */
middleware.processRender(reqMock, resMock, next);
/* called to trigger the rendering process ith a dumy template */
await resMock.render('template', {});
/* Check that the next middleware function is not called since headers have already been sent */
assert.strictEqual(nextCalled, true);
});
/* this test case checks if the middleware correctly proceeds when the headers have not been sent */
/* (headersSent = false as default from the beforeEach) */
it('should call next if headers are not sent', async () => {
const middleware = require('../src/middleware');
let nextCalled = false;
const next = () => {
nextCalled = true;
};
/* calling the middleware main function with the Mock objects above */
middleware.processRender(reqMock, resMock, next);
/* called to trigger the rendering process with a dummy template */
await resMock.render('template', {});
/* Checks that the next middleware function is called since the headers has not been sent */
assert.strictEqual(nextCalled, true);
});
});
});

0 comments on commit b877e68

Please sign in to comment.