cleanup: remove unnecessary require for global Buffer
[express.git] / test / support / utils.js
blobc74f8efe0c2cade23fc93642e59f2a5c6c2d0dd7
2 /**
3 * Module dependencies.
4 * @private
5 */
7 var assert = require('assert');
9 /**
10 * Module exports.
11 * @public
14 exports.shouldHaveBody = shouldHaveBody
15 exports.shouldHaveHeader = shouldHaveHeader
16 exports.shouldNotHaveBody = shouldNotHaveBody
17 exports.shouldNotHaveHeader = shouldNotHaveHeader;
18 exports.shouldSkipQuery = shouldSkipQuery
20 /**
21 * Assert that a supertest response has a specific body.
23 * @param {Buffer} buf
24 * @returns {function}
27 function shouldHaveBody (buf) {
28 return function (res) {
29 var body = !Buffer.isBuffer(res.body)
30 ? Buffer.from(res.text)
31 : res.body
32 assert.ok(body, 'response has body')
33 assert.strictEqual(body.toString('hex'), buf.toString('hex'))
37 /**
38 * Assert that a supertest response does have a header.
40 * @param {string} header Header name to check
41 * @returns {function}
44 function shouldHaveHeader (header) {
45 return function (res) {
46 assert.ok((header.toLowerCase() in res.headers), 'should have header ' + header)
50 /**
51 * Assert that a supertest response does not have a body.
53 * @returns {function}
56 function shouldNotHaveBody () {
57 return function (res) {
58 assert.ok(res.text === '' || res.text === undefined)
62 /**
63 * Assert that a supertest response does not have a header.
65 * @param {string} header Header name to check
66 * @returns {function}
68 function shouldNotHaveHeader(header) {
69 return function (res) {
70 assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);
74 function getMajorVersion(versionString) {
75 return versionString.split('.')[0];
78 function shouldSkipQuery(versionString) {
79 // Skipping HTTP QUERY tests on Node 21, it is reported in http.METHODS on 21.7.2 but not supported
80 // update this implementation to run on supported versions of 21 once they exist
81 // upstream tracking https://github.com/nodejs/node/issues/51562
82 // express tracking issue: https://github.com/expressjs/express/issues/5615
83 return Number(getMajorVersion(versionString)) === 21