Use let or const to avoid scope issues and hoisting
var assert = require('node:assert')
1'use strict'23var assert = require('node:assert')4const { Buffer } = require('node:buffer');5var express = require('..');6var methods = require('../lib/utils').methods;7var request = require('supertest');8var utils = require('./support/utils');910var shouldSkipQuery = require('./support/utils').shouldSkipQuery1112describe('res', function(){13 describe('.send()', function(){14 it('should set body to ""', function(done){15 var app = express();1617 app.use(function(req, res){18 res.send();19 });2021 request(app)22 .get('/')23 .expect(200, '', done);24 })25 })2627 describe('.send(null)', function(){28 it('should set body to ""', function(done){29 var app = express();3031 app.use(function(req, res){32 res.send(null);33 });3435 request(app)36 .get('/')37 .expect('Content-Length', '0')38 .expect(200, '', done);39 })40 })4142 describe('.send(undefined)', function(){43 it('should set body to ""', function(done){44 var app = express();4546 app.use(function(req, res){47 res.send(undefined);48 });4950 request(app)51 .get('/')52 .expect(200, '', done);53 })54 })5556 describe('.send(Number)', function(){57 it('should send as application/json', function(done){58 var app = express();5960 app.use(function(req, res){61 res.send(1000);62 });6364 request(app)65 .get('/')66 .expect('Content-Type', 'application/json; charset=utf-8')67 .expect(200, '1000', done)68 })69 })7071 describe('.send(String)', function(){72 it('should send as html', function(done){73 var app = express();7475 app.use(function(req, res){76 res.send('<p>hey</p>');77 });7879 request(app)80 .get('/')81 .expect('Content-Type', 'text/html; charset=utf-8')82 .expect(200, '<p>hey</p>', done);83 })8485 it('should set ETag', function (done) {86 var app = express();8788 app.use(function (req, res) {89 var str = Array(1000).join('-');90 res.send(str);91 });9293 request(app)94 .get('/')95 .expect('ETag', 'W/"3e7-qPnkJ3CVdVhFJQvUBfF10TmVA7g"')96 .expect(200, done);97 })9899 it('should not override Content-Type', function(done){100 var app = express();101102 app.use(function(req, res){103 res.set('Content-Type', 'text/plain').send('hey');104 });105106 request(app)107 .get('/')108 .expect('Content-Type', 'text/plain; charset=utf-8')109 .expect(200, 'hey', done);110 })111112 it('should override charset in Content-Type', function(done){113 var app = express();114115 app.use(function(req, res){116 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send('hey');117 });118119 request(app)120 .get('/')121 .expect('Content-Type', 'text/plain; charset=utf-8')122 .expect(200, 'hey', done);123 })124125 it('should keep charset in Content-Type for Buffers', function(done){126 var app = express();127128 app.use(function(req, res){129 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(Buffer.from('hi'))130 });131132 request(app)133 .get('/')134 .expect('Content-Type', 'text/plain; charset=iso-8859-1')135 .expect(200, 'hi', done);136 })137 })138139 describe('.send(Buffer)', function(){140 it('should send as octet-stream', function(done){141 var app = express();142143 app.use(function(req, res){144 res.send(Buffer.from('hello'))145 });146147 request(app)148 .get('/')149 .expect(200)150 .expect('Content-Type', 'application/octet-stream')151 .expect(utils.shouldHaveBody(Buffer.from('hello')))152 .end(done)153 })154155 it('should set ETag', function (done) {156 var app = express();157158 app.use(function (req, res) {159 res.send(Buffer.alloc(999, '-'))160 });161162 request(app)163 .get('/')164 .expect('ETag', 'W/"3e7-qPnkJ3CVdVhFJQvUBfF10TmVA7g"')165 .expect(200, done);166 })167168 it('should not override Content-Type', function(done){169 var app = express();170171 app.use(function(req, res){172 res.set('Content-Type', 'text/plain').send(Buffer.from('hey'))173 });174175 request(app)176 .get('/')177 .expect('Content-Type', 'text/plain; charset=utf-8')178 .expect(200, 'hey', done);179 })180181 it('should accept Uint8Array', function(done){182 var app = express();183 app.use(function(req, res){184 const encodedHey = new TextEncoder().encode("hey");185 res.set("Content-Type", "text/plain").send(encodedHey);186 })187188 request(app)189 .get("/")190 .expect("Content-Type", "text/plain; charset=utf-8")191 .expect(200, "hey", done);192 })193194 it('should not override ETag', function (done) {195 var app = express()196197 app.use(function (req, res) {198 res.type('text/plain').set('ETag', '"foo"').send(Buffer.from('hey'))199 })200201 request(app)202 .get('/')203 .expect('ETag', '"foo"')204 .expect(200, 'hey', done)205 })206 })207208 describe('.send(Object)', function(){209 it('should send as application/json', function(done){210 var app = express();211212 app.use(function(req, res){213 res.send({ name: 'tobi' });214 });215216 request(app)217 .get('/')218 .expect('Content-Type', 'application/json; charset=utf-8')219 .expect(200, '{"name":"tobi"}', done)220 })221 })222223 describe('when the request method is HEAD', function(){224 it('should ignore the body', function(done){225 var app = express();226227 app.use(function(req, res){228 res.send('yay');229 });230231 request(app)232 .head('/')233 .expect(200)234 .expect(utils.shouldNotHaveBody())235 .end(done)236 })237 })238239 describe('when .statusCode is 204', function(){240 it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){241 var app = express();242243 app.use(function(req, res){244 res.status(204).set('Transfer-Encoding', 'chunked').send('foo');245 });246247 request(app)248 .get('/')249 .expect(utils.shouldNotHaveHeader('Content-Type'))250 .expect(utils.shouldNotHaveHeader('Content-Length'))251 .expect(utils.shouldNotHaveHeader('Transfer-Encoding'))252 .expect(204, '', done);253 })254 })255256 describe('when .statusCode is 205', function () {257 it('should strip Transfer-Encoding field and body, set Content-Length', function (done) {258 var app = express()259260 app.use(function (req, res) {261 res.status(205).set('Transfer-Encoding', 'chunked').send('foo')262 })263264 request(app)265 .get('/')266 .expect(utils.shouldNotHaveHeader('Transfer-Encoding'))267 .expect('Content-Length', '0')268 .expect(205, '', done)269 })270 })271272 describe('when .statusCode is 304', function(){273 it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){274 var app = express();275276 app.use(function(req, res){277 res.status(304).set('Transfer-Encoding', 'chunked').send('foo');278 });279280 request(app)281 .get('/')282 .expect(utils.shouldNotHaveHeader('Content-Type'))283 .expect(utils.shouldNotHaveHeader('Content-Length'))284 .expect(utils.shouldNotHaveHeader('Transfer-Encoding'))285 .expect(304, '', done);286 })287 })288289 it('should always check regardless of length', function(done){290 var app = express();291 var etag = '"asdf"';292293 app.use(function(req, res, next){294 res.set('ETag', etag);295 res.send('hey');296 });297298 request(app)299 .get('/')300 .set('If-None-Match', etag)301 .expect(304, done);302 })303304 it('should respond with 304 Not Modified when fresh', function(done){305 var app = express();306 var etag = '"asdf"';307308 app.use(function(req, res){309 var str = Array(1000).join('-');310 res.set('ETag', etag);311 res.send(str);312 });313314 request(app)315 .get('/')316 .set('If-None-Match', etag)317 .expect(304, done);318 })319320 it('should not perform freshness check unless 2xx or 304', function(done){321 var app = express();322 var etag = '"asdf"';323324 app.use(function(req, res, next){325 res.status(500);326 res.set('ETag', etag);327 res.send('hey');328 });329330 request(app)331 .get('/')332 .set('If-None-Match', etag)333 .expect('hey')334 .expect(500, done);335 })336337 it('should not support jsonp callbacks', function(done){338 var app = express();339340 app.use(function(req, res){341 res.send({ foo: 'bar' });342 });343344 request(app)345 .get('/?callback=foo')346 .expect('{"foo":"bar"}', done);347 })348349 it('should be chainable', function (done) {350 var app = express()351352 app.use(function (req, res) {353 assert.equal(res.send('hey'), res)354 })355356 request(app)357 .get('/')358 .expect(200, 'hey', done)359 })360361 describe('"etag" setting', function () {362 describe('when enabled', function () {363 it('should send ETag', function (done) {364 var app = express();365366 app.use(function (req, res) {367 res.send('kajdslfkasdf');368 });369370 app.enable('etag');371372 request(app)373 .get('/')374 .expect('ETag', 'W/"c-IgR/L5SF7CJQff4wxKGF/vfPuZ0"')375 .expect(200, done);376 });377378 methods.forEach(function (method) {379 if (method === 'connect') return;380381 it('should send ETag in response to ' + method.toUpperCase() + ' request', function (done) {382 if (method === 'query' && shouldSkipQuery(process.versions.node)) {383 this.skip()384 }385 var app = express();386387 app[method]('/', function (req, res) {388 res.send('kajdslfkasdf');389 });390391 request(app)392 [method]('/')393 .expect('ETag', 'W/"c-IgR/L5SF7CJQff4wxKGF/vfPuZ0"')394 .expect(200, done);395 })396 });397398 it('should send ETag for empty string response', function (done) {399 var app = express();400401 app.use(function (req, res) {402 res.send('');403 });404405 app.enable('etag');406407 request(app)408 .get('/')409 .expect('ETag', 'W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"')410 .expect(200, done);411 })412413 it('should send ETag for long response', function (done) {414 var app = express();415416 app.use(function (req, res) {417 var str = Array(1000).join('-');418 res.send(str);419 });420421 app.enable('etag');422423 request(app)424 .get('/')425 .expect('ETag', 'W/"3e7-qPnkJ3CVdVhFJQvUBfF10TmVA7g"')426 .expect(200, done);427 });428429 it('should not override ETag when manually set', function (done) {430 var app = express();431432 app.use(function (req, res) {433 res.set('etag', '"asdf"');434 res.send('hello!');435 });436437 app.enable('etag');438439 request(app)440 .get('/')441 .expect('ETag', '"asdf"')442 .expect(200, done);443 });444445 it('should not send ETag for res.send()', function (done) {446 var app = express();447448 app.use(function (req, res) {449 res.send();450 });451452 app.enable('etag');453454 request(app)455 .get('/')456 .expect(utils.shouldNotHaveHeader('ETag'))457 .expect(200, done);458 })459 });460461 describe('when disabled', function () {462 it('should send no ETag', function (done) {463 var app = express();464465 app.use(function (req, res) {466 var str = Array(1000).join('-');467 res.send(str);468 });469470 app.disable('etag');471472 request(app)473 .get('/')474 .expect(utils.shouldNotHaveHeader('ETag'))475 .expect(200, done);476 });477478 it('should send ETag when manually set', function (done) {479 var app = express();480481 app.disable('etag');482483 app.use(function (req, res) {484 res.set('etag', '"asdf"');485 res.send('hello!');486 });487488 request(app)489 .get('/')490 .expect('ETag', '"asdf"')491 .expect(200, done);492 });493 });494495 describe('when "strong"', function () {496 it('should send strong ETag', function (done) {497 var app = express();498499 app.set('etag', 'strong');500501 app.use(function (req, res) {502 res.send('hello, world!');503 });504505 request(app)506 .get('/')507 .expect('ETag', '"d-HwnTDHB9U/PRbFMN1z1wps51lqk"')508 .expect(200, done);509 })510 })511512 describe('when "weak"', function () {513 it('should send weak ETag', function (done) {514 var app = express();515516 app.set('etag', 'weak');517518 app.use(function (req, res) {519 res.send('hello, world!');520 });521522 request(app)523 .get('/')524 .expect('ETag', 'W/"d-HwnTDHB9U/PRbFMN1z1wps51lqk"')525 .expect(200, done)526 })527 })528529 describe('when a function', function () {530 it('should send custom ETag', function (done) {531 var app = express();532533 app.set('etag', function (body, encoding) {534 var chunk = !Buffer.isBuffer(body)535 ? Buffer.from(body, encoding)536 : body;537 assert.strictEqual(chunk.toString(), 'hello, world!')538 return '"custom"';539 });540541 app.use(function (req, res) {542 res.send('hello, world!');543 });544545 request(app)546 .get('/')547 .expect('ETag', '"custom"')548 .expect(200, done);549 })550551 it('should not send falsy ETag', function (done) {552 var app = express();553554 app.set('etag', function (body, encoding) {555 return undefined;556 });557558 app.use(function (req, res) {559 res.send('hello, world!');560 });561562 request(app)563 .get('/')564 .expect(utils.shouldNotHaveHeader('ETag'))565 .expect(200, done);566 })567 })568 })569})
Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.