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

add Podium context "invisible" getters for convenience and consistency #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions __tests__/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ test('PodiumContext.middleware() - process a "rich" request - should put parsed
expect(result.context['podium-debug']).toEqual('false');
expect(result.context['podium-requested-by']).toEqual('foo');
expect(result.context['podium-public-pathname']).toBeInstanceOf(Function);
expect(result.context.mountOrigin).toEqual('http://localhost:3030');
expect(result.context.mountPathname).toEqual('/');
expect(result.context.deviceType).toEqual('mobile');
expect(result.context.locale).toEqual('en-US');
expect(result.context.debug).toEqual('false');
expect(result.context.requestedBy).toEqual('foo');
expect(result.context.publicPathname).toBeInstanceOf(Function);
});

test('PodiumContext.middleware() - process a "minimal" request - should put parsed values into res.locals.podium', async () => {
Expand All @@ -207,6 +214,30 @@ test('PodiumContext.middleware() - process a "minimal" request - should put pars
expect(result.context['podium-debug']).toEqual('false');
expect(result.context['podium-requested-by']).toEqual('foo');
expect(result.context['podium-public-pathname']).toBeInstanceOf(Function);
expect(result.context.mountOrigin).toEqual('http://localhost:3030');
expect(result.context.mountPathname).toEqual('/');
expect(result.context.deviceType).toEqual('desktop');
expect(result.context.locale).toEqual('en-US');
expect(result.context.debug).toEqual('false');
expect(result.context.requestedBy).toEqual('foo');
expect(result.context.publicPathname).toBeInstanceOf(Function);
});

test('PodiumContext.middleware() - context object getters not present when serializing', async () => {
const context = new Context({ name: 'foo' });
const incoming = new HttpIncoming();

const result = await context.process(incoming);

expect(Object.keys(result.context)).toEqual([
'podium-public-pathname',
'podium-mount-pathname',
'podium-mount-origin',
'podium-requested-by',
'podium-device-type',
'podium-locale',
'podium-debug',
]);
});

test('PodiumContext.middleware() - a parser throws - should emit "next()" with Boom Error Object', async () => {
Expand Down
8 changes: 8 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ const PodiumContext = class PodiumContext {
const key = `${PREFIX}-${parsers[index][0]}`;
// eslint-disable-next-line no-param-reassign
incoming.context[decamelize(key, '-')] = item;

Object.defineProperty(incoming.context, parsers[index][0], {
get() {
return incoming.context[decamelize(key, '-')];
},
enumerable: false,
configurable: false,
});
});

timer();
Expand Down