From 9e4ec058bbc0c967da35224b01f41ececc0fff5f Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 28 Oct 2019 23:03:09 -0700 Subject: [PATCH] fix(polka): handle non-Polka multi-mount; - Finishes #107 --- packages/polka/index.js | 8 ++--- packages/polka/test/index.js | 67 +++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/packages/polka/index.js b/packages/polka/index.js index d36a488..57508af 100644 --- a/packages/polka/index.js +++ b/packages/polka/index.js @@ -21,12 +21,10 @@ class Polka extends Router { } use(base, ...fns) { - if (typeof base === 'function') { - super.use('/', base, fns); - } else if (base === '/') { + if (base === '/') { super.use(base, fns.map(mount)); - } else if (base instanceof Polka) { - super.use('/', [base.attach, ...fns.map(mount)]); + } else if (typeof base === 'function' || base instanceof Polka) { + super.use('/', [base, ...fns].map(mount)); } else { super.use(base, (req, _, next) => { diff --git a/packages/polka/test/index.js b/packages/polka/test/index.js index 7f7dfe8..6c7b501 100644 --- a/packages/polka/test/index.js +++ b/packages/polka/test/index.js @@ -340,6 +340,71 @@ test('(polka) middleware - use(subapp)', async t => { next(); } + const sub = ( + polka() + .use(foo, bar) + .use((req, res, next) => { + t.is(req.main, true, '~> SUB ran after MAIN'); + t.is(req.foo, true, '~> SUB ran after FOO'); + t.is(req.bar, true, '~> SUB ran after BAR'); + req.sub = true; + next(); + }) + .get('/item', (req, res) => { + t.pass('~> HANDLER for sub("/item") ran'); + t.is(req.main, true, '~> HANDLER ran after MAIN'); + t.is(req.foo, true, '~> HANDLER ran after FOO'); + t.is(req.bar, true, '~> HANDLER ran after BAR'); + t.is(req.sub, true, '~> HANDLER ran after SUB'); + res.end('item'); + }) + ); + + // Construct the main application + const main = ( + polka() + .use((req, res, next) => { + req.main = true; + next(); + }) + .use(sub) + ); + + let uri = listen(main); + + console.log('GET "/item"'); + // `sub` has the "/item" route + let res = await get(uri + '/item'); // +10 + t.is(res.statusCode, 200, '~> received 200 status'); + t.is(res.data, 'item', '~> received "item" response'); + + console.log('GET "/unknown"'); + // 404 from `sub` application, no route + await get(uri + '/unknown').catch(err => { // +6 + t.is(err.statusCode, 404, '~> received 404 status'); + t.is(err.data, 'Not Found', '~> received "Not Found" response'); + }); + + main.server.close(); +}); + + +test('(polka) middleware - use(mware, subapp)', async t => { + t.plan(21); + + function foo(req, res, next) { + t.is(req.main, true, '~> FOO ran after MAIN'); + req.foo = true; + next(); + } + + function bar(req, res, next) { + t.is(req.main, true, '~> BAR ran after MAIN'); + t.is(req.foo, true, '~> BAR ran after FOO'); + req.bar = true; + next(); + } + const sub = ( polka() .use(bar) @@ -367,7 +432,7 @@ test('(polka) middleware - use(subapp)', async t => { req.main = true; next(); }) - .use('/', foo, sub) + .use(foo, sub) ); let uri = listen(main);