From 18f782bba97157ab2e0ab754b404388e524915fd Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Wed, 23 Feb 2022 00:18:36 -0500 Subject: [PATCH] tests: remove duplicate utils --- test/res.download.js | 36 ++++++++++-------------------------- test/res.redirect.js | 33 +++++++++++++-------------------- test/res.send.js | 34 +++++++++------------------------- test/res.sendFile.js | 26 ++++++++------------------ 4 files changed, 40 insertions(+), 89 deletions(-) diff --git a/test/res.download.js b/test/res.download.js index ce0ee088..29a687a4 100644 --- a/test/res.download.js +++ b/test/res.download.js @@ -1,10 +1,10 @@ 'use strict' var after = require('after'); -var assert = require('assert'); var Buffer = require('safe-buffer').Buffer var express = require('..'); var request = require('supertest'); +var utils = require('./support/utils') describe('res', function(){ describe('.download(path)', function(){ @@ -129,12 +129,12 @@ describe('res', function(){ }) request(app) - .get('/') - .expect(200) - .expect('Content-Disposition', 'attachment; filename="document"') - .expect('Cache-Control', 'public, max-age=14400') - .expect(shouldHaveBody(Buffer.from('tobi'))) - .end(done) + .get('/') + .expect(200) + .expect('Content-Disposition', 'attachment; filename="document"') + .expect('Cache-Control', 'public, max-age=14400') + .expect(utils.shouldHaveBody(Buffer.from('tobi'))) + .end(done) }) describe('when options.headers contains Content-Disposition', function () { @@ -207,25 +207,9 @@ describe('res', function(){ }); request(app) - .get('/') - .expect(shouldNotHaveHeader('Content-Disposition')) - .expect(200, 'failed', done); + .get('/') + .expect(utils.shouldNotHaveHeader('Content-Disposition')) + .expect(200, 'failed', done) }) }) }) - -function shouldHaveBody (buf) { - return function (res) { - var body = !Buffer.isBuffer(res.body) - ? Buffer.from(res.text) - : res.body - assert.ok(body, 'response has body') - assert.strictEqual(body.toString('hex'), buf.toString('hex')) - } -} - -function shouldNotHaveHeader(header) { - return function (res) { - assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header); - }; -} diff --git a/test/res.redirect.js b/test/res.redirect.js index be7c773b..5ffc7e48 100644 --- a/test/res.redirect.js +++ b/test/res.redirect.js @@ -1,6 +1,5 @@ 'use strict' -var assert = require('assert') var express = require('..'); var request = require('supertest'); var utils = require('./support/utils'); @@ -86,11 +85,11 @@ describe('res', function(){ }); request(app) - .head('/') - .expect(302) - .expect('Location', 'http://google.com') - .expect(shouldNotHaveBody()) - .end(done) + .head('/') + .expect(302) + .expect('Location', 'http://google.com') + .expect(utils.shouldNotHaveBody()) + .end(done) }) }) @@ -199,20 +198,14 @@ describe('res', function(){ }); request(app) - .get('/') - .set('Accept', 'application/octet-stream') - .expect(302) - .expect('location', 'http://google.com') - .expect('content-length', '0') - .expect(utils.shouldNotHaveHeader('Content-Type')) - .expect(shouldNotHaveBody()) - .end(done) + .get('/') + .set('Accept', 'application/octet-stream') + .expect(302) + .expect('location', 'http://google.com') + .expect('content-length', '0') + .expect(utils.shouldNotHaveHeader('Content-Type')) + .expect(utils.shouldNotHaveBody()) + .end(done) }) }) }) - -function shouldNotHaveBody () { - return function (res) { - assert.ok(res.text === '' || res.text === undefined) - } -} diff --git a/test/res.send.js b/test/res.send.js index 8f849f80..6ba55422 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -188,11 +188,11 @@ describe('res', function(){ }); request(app) - .get('/') - .expect(200) - .expect('Content-Type', 'application/octet-stream') - .expect(shouldHaveBody(Buffer.from('hello'))) - .end(done) + .get('/') + .expect(200) + .expect('Content-Type', 'application/octet-stream') + .expect(utils.shouldHaveBody(Buffer.from('hello'))) + .end(done) }) it('should set ETag', function (done) { @@ -259,10 +259,10 @@ describe('res', function(){ }); request(app) - .head('/') - .expect(200) - .expect(shouldNotHaveBody()) - .end(done) + .head('/') + .expect(200) + .expect(utils.shouldNotHaveBody()) + .end(done) }) }) @@ -578,19 +578,3 @@ describe('res', function(){ }) }) }) - -function shouldHaveBody (buf) { - return function (res) { - var body = !Buffer.isBuffer(res.body) - ? Buffer.from(res.text) - : res.body - assert.ok(body, 'response has body') - assert.strictEqual(body.toString('hex'), buf.toString('hex')) - } -} - -function shouldNotHaveBody () { - return function (res) { - assert.ok(res.text === '' || res.text === undefined) - } -} diff --git a/test/res.sendFile.js b/test/res.sendFile.js index c676fc41..538d6b9d 100644 --- a/test/res.sendFile.js +++ b/test/res.sendFile.js @@ -165,10 +165,10 @@ describe('res', function(){ var app = createApp(path.resolve(__dirname, 'fixtures/.name'), { dotfiles: 'allow' }); request(app) - .get('/') - .expect(200) - .expect(shouldHaveBody(Buffer.from('tobi'))) - .end(done) + .get('/') + .expect(200) + .expect(utils.shouldHaveBody(Buffer.from('tobi'))) + .end(done) }); }); @@ -570,10 +570,10 @@ describe('res', function(){ }); request(app) - .get('/') - .expect(200) - .expect(shouldHaveBody(Buffer.from('tobi'))) - .end(done) + .get('/') + .expect(200) + .expect(utils.shouldHaveBody(Buffer.from('tobi'))) + .end(done) }) it('should accept headers option', function(done){ @@ -828,13 +828,3 @@ function createApp(path, options, fn) { return app; } - -function shouldHaveBody (buf) { - return function (res) { - var body = !Buffer.isBuffer(res.body) - ? Buffer.from(res.text) - : res.body - assert.ok(body, 'response has body') - assert.strictEqual(body.toString('hex'), buf.toString('hex')) - } -} -- 2.11.4.GIT