Skip to content

Commit

Permalink
add tests for node-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Neville Mehta authored and Neville Mehta committed Jul 22, 2024
1 parent 5c07a8b commit fdc526b
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/integrations/node/src/standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ export default function standalone(app: NodeApp, options: Options) {
const server = createServer(handler, host, port);

// optionally add standalone middleware (if passed)
console.log(`options >>> `, options);
if (options.standaloneMiddleware) {
import(options.standaloneMiddleware)
.then((middleware) => {
return (req: http.IncomingMessage, res: http.ServerResponse) => {
middleware.default(req, res, () => handler);
};
server.updateServerListener((req: http.IncomingMessage, res: http.ServerResponse) => {
middleware.default(req, res, () => {
handler(req, res);
});
});
})
.catch(() => {});
.catch((err) => console.log('Error loading standalone middleware:', err));
}

server.server.listen(port, host);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const combineMiddleware = (...middlewares) => {
return (req, res, next) => {
let index = 0;

const run = (err) => {
if (err) {
return next(err);
}
if (index >= middlewares.length) {
return next();
}
const middleware = middlewares[index++];
try {
middleware(req, res, run);
} catch (error) {
next(error);
}
};

run();
};
};

const middleware_one = (req, res, next) => {
if (req.url === '/middleware-one') {
res.end('This is middleware one');
} else {
next();
}
};

const middleware_two = (req, res, next) => {
if (req.url === '/middleware-two') {
res.end('This is middleware two');
} else {
next();
}
};

export default combineMiddleware(middleware_one, middleware_two);
123 changes: 123 additions & 0 deletions packages/integrations/node/test/node-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,126 @@ describe('behavior from middleware, middleware', () => {
assert.equal(body.text().includes("Here's a random number"), true);
});
});

describe('behavior from middleware, standalone `mode` passing `standaloneMiddleware` path', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let server;

before(async () => {
process.env.PRERENDER = false;
fixture = await loadFixture({
root: './fixtures/node-middleware/',
output: 'server',
adapter: nodejs(
{
mode: 'standalone',
standaloneMiddleware: new URL('./fixtures/node-middleware/src/node-middleware.mjs', import.meta.url).toString(),
}
),
});
await fixture.build();
const { startServer } = await fixture.loadAdapterEntryModule();
let res = await startServer();
server = res.server;
await waitServerListen(server.server);
});

after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});

describe('middleware-one', async () => {
it('when mode is standalone, calling middleware-one', async () => {
const res = await fetch(`http://${server.host}:${server.port}/middleware-one`);

assert.equal(res.status, 200);

const html = await res.text();
const $ = cheerio.load(html);

const body = $('body');
assert.equal(body.text().includes('This is middleware one'), true);
});
});

describe('middleware-two', async () => {
it('when mode is standalone, calling middleware-two', async () => {
const res = await fetch(`http://${server.host}:${server.port}/middleware-two`);

assert.equal(res.status, 200);

const html = await res.text();
const $ = cheerio.load(html);

const body = $('body');
assert.equal(body.text().includes('This is middleware two'), true);
});
});
});

describe('behavior from middleware, standalone `mode` dont pass `standaloneMiddleware` option', () => {
/** @type {import('./test-utils').Fixture} */

it('when mode is `middleware` and `standaloneMiddleware` is passed.', async () => {
try {
await loadFixture({
root: './fixtures/node-middleware/',
output: 'server',
adapter: nodejs({ mode: 'middleware', standaloneMiddleware: new URL('./fixtures/node-middleware/src/node-middleware.mjs', import.meta.url).toString() }),
});
assert.fail('should have thrown an error');
} catch (err) {
assert.equal(err.message, `'standaloneMiddleware' option is only available in 'standalone' mode.`);
}
});
});

describe('behavior from middleware, standalone mode with incorrect standaloneMiddleware path', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let server;

before(async () => {
process.env.PRERENDER = false;
fixture = await loadFixture({
root: './fixtures/node-middleware/',
output: 'server',
adapter: nodejs({ mode: 'standalone', standaloneMiddleware: `wrong-path/node-middleware.mjs` }),
});
await fixture.build();
const { startServer } = await fixture.loadAdapterEntryModule();
let res = startServer();
server = res.server;
await waitServerListen(server.server);
});

after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});

describe('404', async () => {
it('when mode is standalone and wrong standaloneMiddleware path, 404 page', async () => {
const res = await fetch(`http://${server.host}:${server.port}/error-page`);

assert.equal(res.status, 404);

const html = await res.text();
const $ = cheerio.load(html);

const body = $('body');
assert.equal(body.text().includes('Page does not exist'), true);
});
});

describe('200', async () => {
it('when mode is standalone and wrong standaloneMiddleware path, 200 page', async () => {
const res = await fetch(`http://${server.host}:${server.port}/`);
assert.equal(res.status, 200);
});
});
});

0 comments on commit fdc526b

Please sign in to comment.