Skip to content

Commit

Permalink
Additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bosschaert committed Nov 29, 2023
1 parent f7e211a commit 14e652b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function main(req, ctx) {
const g = new GoogleLogger(req);
g.logRUM(cwv, id, weight, referer || referrer, generation, checkpoint, target, source, t);
} else {
const l = new ConsoleLogger(req);
const l = new ConsoleLogger(req, ctx?.altConsole);
l.logRUM(cwv, id, weight, referer || referrer, generation, checkpoint, target, source, t);
}
} catch (err) {
Expand Down
30 changes: 28 additions & 2 deletions test/console-logger.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('Test Console Logger', () => {
it('log to RUM', () => {
const headers = new Map();
headers.set('x-forwarded-host', 'www.foo.com');
headers.set('host', 'www.acme.com');
const url = new URL('http://www.foo.com/testing123');

const req = { headers, url };
Expand All @@ -25,10 +26,10 @@ describe('Test Console Logger', () => {
const testLogger = {
log: (...args) => logged.push(args),
};
const gl = new ConsoleLogger(req, testLogger);
const cl = new ConsoleLogger(req, testLogger);

const myJSON = { foo: ['b', 'ar'] };
gl.logRUM(
cl.logRUM(
myJSON,
'someid',
5,
Expand All @@ -53,4 +54,29 @@ describe('Test Console Logger', () => {
assert.equal(ld.id, 'someid');
assert.deepEqual(ld.foo, ['b', 'ar']);
});

it('log to RUM 2', () => {
const headers = new Map();
headers.set('host', 'www.acme.com');
const url = new URL('http://www.foo.com/testing123');

const req = { headers, url };

const logged = [];
const testLogger = {
log: (...args) => logged.push(args),
};
const cl = new ConsoleLogger(req, testLogger);

cl.logRUM({}, 'id123', 2, undefined, 42, 1);
assert.equal(logged.length, 1);
const ld = JSON.parse(logged[0]);

assert.equal(ld.host, 'www.acme.com');
assert.equal(ld.url.toString(), 'http://www.foo.com/testing123');
assert.equal(ld.weight, 2);
assert.equal(ld.generation, 42);
assert.equal(ld.checkpoint, 1);
assert.equal(ld.id, 'id123');
});
});
28 changes: 27 additions & 1 deletion test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('Test index', () => {
assert.equal(800, logged.TTFB);
});

it('Info request', async () => {
it('info request', async () => {
const req = {
method: 'GET',
url: 'http://test.org/info.json?key=val',
Expand All @@ -275,4 +275,30 @@ describe('Test index', () => {
assert.equal('my-platform', res.platform);
assert.equal('1.2.ZZ', res.version);
});

it('console logger', async () => {
const logged = [];
const capturedConsole = {
log: (...args) => logged.push(args),
};

const req = {};
req.headers = new Map();
req.method = 'POST';
req.url = 'http://www.acme.org';
req.json = () => ({
id: 'xyz123',
});

const ctx = { altConsole: capturedConsole };
const resp = await methods.main(req, ctx);

assert.equal(201, resp.status);
assert.equal(logged.length, 1);

const ld = JSON.parse(logged[0]);
assert.equal(ld.url, 'http://www.acme.org');
assert.equal(ld.weight, 1);
assert.equal(ld.id, 'xyz123');
});
});

0 comments on commit 14e652b

Please sign in to comment.