From d29634bf56a2c640fd202561dc9beb9595183adb Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Thu, 15 Sep 2016 08:41:12 +0200 Subject: [PATCH] Simplify the new tests using a custom assertion. --- test/expressExtractHeaders.js | 182 ++++++++-------------------------- 1 file changed, 41 insertions(+), 141 deletions(-) diff --git a/test/expressExtractHeaders.js b/test/expressExtractHeaders.js index 40f78c0..1d3cb89 100644 --- a/test/expressExtractHeaders.js +++ b/test/expressExtractHeaders.js @@ -64,199 +64,99 @@ describe('expressExtractHeaders', function () { ); }); - it('should extract ', function () { - var responseHtml = - '\nfoo'; + expect.addAssertion(' to result in a Link header of ', function (expect, subject, value) { return expect( express() .use(expressExtractHeaders()) .use(function (req, res) { - res.send(responseHtml); + res.send('\n' + subject + 'foo'); }), 'to yield exchange', { request: '/', response: { headers: { 'Content-Type': 'text/html; charset=utf-8', - Link: '; rel=preconnect' + Link: value } } } ); }); + it('should extract ', function () { + return expect( + '', + 'to result in a Link header of', + '; rel=preconnect' + ); + }); + it('should extract two link elements', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: [ - '; rel=preconnect', - '; rel=prefetch' - ] - } - } - } + '' + + '', + 'to result in a Link header of', [ + '; rel=preconnect', + '; rel=prefetch' + ] ); }); it('should extract three link elements', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: [ - '; rel=prefetch; as=image', - '; rel=preconnect', - '; rel=prefetch' - ] - } - } - } + '' + + '' + + '', + 'to result in a Link header of', [ + '; rel=prefetch; as=image', + '; rel=preconnect', + '; rel=prefetch' + ] ); }); it('should ignore unsupported link elements based on the rel attribute', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: undefined - } - } - } - ); + '', 'to result in a Link header of', undefined); }); it('should extract ', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: '; rel=preconnect' - } - } - } + '', + 'to result in a Link header of', + '; rel=preconnect' ); }); it('should extract the "as" attribute correctly', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: '; rel=prefetch; as=image' - } - } - } + '', + 'to result in a Link header of', + '; rel=prefetch; as=image' ); }); it('should extract the "pr" attribute correctly', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: '; rel=prerender; pr=0.75' - } - } - } + '', + 'to result in a Link header of', + '; rel=prerender; pr=0.75' ); }); it('should extract the "crossorigin" attribute when it has a value', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: '; rel=prefetch; as=html; crossorigin=use-credentials' - } - } - } + '', + 'to result in a Link header of', + '; rel=prefetch; as=html; crossorigin=use-credentials' ); }); it('should extract the "crossorigin" attribute when it has no value', function () { - var responseHtml = - '\nfoo'; return expect( - express() - .use(expressExtractHeaders()) - .use(function (req, res) { - res.send(responseHtml); - }), - 'to yield exchange', { - request: '/', - response: { - headers: { - 'Content-Type': 'text/html; charset=utf-8', - Link: '; rel=prefetch; as=html; crossorigin' - } - } - } + 'foo', + 'to result in a Link header of', + '; rel=prefetch; as=html; crossorigin' ); });