fix(buffer): use node:buffer instead of safe-buffer (#6071)
[express.git] / test / support / utils.js
blob50350943d415bc5215aaf8dd04eb12de23f9d330
2 /**
3 * Module dependencies.
4 * @private
5 */
7 var assert = require('assert');
8 var Buffer = require('node:buffer').Buffer
10 /**
11 * Module exports.
12 * @public
15 exports.shouldHaveBody = shouldHaveBody
16 exports.shouldHaveHeader = shouldHaveHeader
17 exports.shouldNotHaveBody = shouldNotHaveBody
18 exports.shouldNotHaveHeader = shouldNotHaveHeader;
19 exports.shouldSkipQuery = shouldSkipQuery
21 /**
22 * Assert that a supertest response has a specific body.
24 * @param {Buffer} buf
25 * @returns {function}
28 function shouldHaveBody (buf) {
29 return function (res) {
30 var body = !Buffer.isBuffer(res.body)
31 ? Buffer.from(res.text)
32 : res.body
33 assert.ok(body, 'response has body')
34 assert.strictEqual(body.toString('hex'), buf.toString('hex'))
38 /**
39 * Assert that a supertest response does have a header.
41 * @param {string} header Header name to check
42 * @returns {function}
45 function shouldHaveHeader (header) {
46 return function (res) {
47 assert.ok((header.toLowerCase() in res.headers), 'should have header ' + header)
51 /**
52 * Assert that a supertest response does not have a body.
54 * @returns {function}
57 function shouldNotHaveBody () {
58 return function (res) {
59 assert.ok(res.text === '' || res.text === undefined)
63 /**
64 * Assert that a supertest response does not have a header.
66 * @param {string} header Header name to check
67 * @returns {function}
69 function shouldNotHaveHeader(header) {
70 return function (res) {
71 assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header);
75 function getMajorVersion(versionString) {
76 return versionString.split('.')[0];
79 function shouldSkipQuery(versionString) {
80 // Skipping HTTP QUERY tests on Node 21, it is reported in http.METHODS on 21.7.2 but not supported
81 // update this implementation to run on supported versions of 21 once they exist
82 // upstream tracking https://github.com/nodejs/node/issues/51562
83 // express tracking issue: https://github.com/expressjs/express/issues/5615
84 return Number(getMajorVersion(versionString)) === 21