Skip to content
Myr

res.send.js

This file

Path

test/res.send.js

530 lines · 11.9 KiB · JavaScript

Last changed by

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

History of this file

Take it

12var express = require('../')3  , request = require('supertest')4  , assert = require('assert');56describe('res', function(){7  describe('.send(null)', function(){8    it('should set body to ""', function(done){9      var app = express();1011      app.use(function(req, res){12        res.send(null);13      });1415      request(app)16      .get('/')17      .expect('', done);18    })19  })2021  describe('.send(undefined)', function(){22    it('should set body to ""', function(done){23      var app = express();2425      app.use(function(req, res){26        res.send(undefined);27      });2829      request(app)30      .get('/')31      .expect('', done);32    })33  })3435  describe('.send(code)', function(){36    it('should set .statusCode', function(done){37      var app = express();3839      app.use(function(req, res){40        res.send(201).should.equal(res);41      });4243      request(app)44      .get('/')45      .expect('Created')46      .expect(201, done);47    })48  })4950  describe('.send(code, body)', function(){51    it('should set .statusCode and body', function(done){52      var app = express();5354      app.use(function(req, res){55        res.send(201, 'Created :)');56      });5758      request(app)59      .get('/')60      .expect('Created :)')61      .expect(201, done);62    })63  })6465  describe('.send(body, code)', function(){66    it('should be supported for backwards compat', function(done){67      var app = express();6869      app.use(function(req, res){70        res.send('Bad!', 400);71      });7273      request(app)74      .get('/')75      .expect('Bad!')76      .expect(400, done);77    })78  })7980  describe('.send(String)', function(){81    it('should send as html', function(done){82      var app = express();8384      app.use(function(req, res){85        res.send('<p>hey</p>');86      });8788      request(app)89      .get('/')90      .end(function(err, res){91        res.headers.should.have.property('content-type', 'text/html; charset=utf-8');92        res.text.should.equal('<p>hey</p>');93        res.statusCode.should.equal(200);94        done();95      })96    })9798    it('should set ETag', function(done){99      var app = express();100101      app.use(function(req, res){102        var str = Array(1024 * 2).join('-');103        res.send(str);104      });105106      request(app)107      .get('/')108      .expect('ETag', 'W/"7ff-2796319984"')109      .end(done);110    })111112    it('should not set ETag for non-GET/HEAD', function(done){113      var app = express();114115      app.use(function(req, res){116        var str = Array(1024 * 2).join('-');117        res.send(str);118      });119120      request(app)121      .post('/')122      .end(function(err, res){123        if (err) return done(err);124        assert(!res.header.etag, 'has an ETag');125        done();126      });127    })128129    it('should not override Content-Type', function(done){130      var app = express();131132      app.use(function(req, res){133        res.set('Content-Type', 'text/plain').send('hey');134      });135136      request(app)137      .get('/')138      .expect('Content-Type', 'text/plain; charset=utf-8')139      .expect(200, 'hey', done);140    })141142    it('should override charset in Content-Type', function(done){143      var app = express();144145      app.use(function(req, res){146        res.set('Content-Type', 'text/plain; charset=iso-8859-1').send('hey');147      });148149      request(app)150      .get('/')151      .expect('Content-Type', 'text/plain; charset=utf-8')152      .expect(200, 'hey', done);153    })154155    it('should keep charset in Content-Type for Buffers', function(done){156      var app = express();157158      app.use(function(req, res){159        res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(new Buffer('hi'));160      });161162      request(app)163      .get('/')164      .expect('Content-Type', 'text/plain; charset=iso-8859-1')165      .expect(200, 'hi', done);166    })167  })168169  describe('.send(Buffer)', function(){170    it('should send as octet-stream', function(done){171      var app = express();172173      app.use(function(req, res){174        res.send(new Buffer('hello'));175      });176177      request(app)178      .get('/')179      .end(function(err, res){180        res.headers.should.have.property('content-type', 'application/octet-stream');181        res.text.should.equal('hello');182        res.statusCode.should.equal(200);183        done();184      })185    })186187    it('should set ETag', function(done){188      var app = express();189190      app.use(function(req, res){191        var str = Array(1024 * 2).join('-');192        res.send(new Buffer(str));193      });194195      request(app)196      .get('/')197      .expect('ETag', 'W/"7ff-2796319984"')198      .end(done);199    })200201    it('should not override Content-Type', function(done){202      var app = express();203204      app.use(function(req, res){205        res.set('Content-Type', 'text/plain').send(new Buffer('hey'));206      });207208      request(app)209      .get('/')210      .end(function(err, res){211        res.headers.should.have.property('content-type', 'text/plain');212        res.text.should.equal('hey');213        res.statusCode.should.equal(200);214        done();215      })216    })217  })218219  describe('.send(Object)', function(){220    it('should send as application/json', function(done){221      var app = express();222223      app.use(function(req, res){224        res.send({ name: 'tobi' });225      });226227      request(app)228      .get('/')229      .expect('Content-Type', 'application/json; charset=utf-8')230      .expect(200, '{"name":"tobi"}', done)231    })232  })233234  describe('when the request method is HEAD', function(){235    it('should ignore the body', function(done){236      var app = express();237238      app.use(function(req, res){239        res.send('yay');240      });241242      request(app)243      .head('/')244      .expect('', done);245    })246  })247248  describe('when .statusCode is 204', function(){249    it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){250      var app = express();251252      app.use(function(req, res){253        res.status(204).set('Transfer-Encoding', 'chunked').send('foo');254      });255256      request(app)257      .get('/')258      .end(function(err, res){259        res.headers.should.not.have.property('content-type');260        res.headers.should.not.have.property('content-length');261        res.headers.should.not.have.property('transfer-encoding');262        res.text.should.equal('');263        done();264      })265    })266  })267268  describe('when .statusCode is 304', function(){269    it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){270      var app = express();271272      app.use(function(req, res){273        res.status(304).set('Transfer-Encoding', 'chunked').send('foo');274      });275276      request(app)277      .get('/')278      .end(function(err, res){279        res.headers.should.not.have.property('content-type');280        res.headers.should.not.have.property('content-length');281        res.headers.should.not.have.property('transfer-encoding');282        res.text.should.equal('');283        done();284      })285    })286  })287288  it('should always check regardless of length', function(done){289    var app = express();290    var etag = '"asdf"';291292    app.use(function(req, res, next){293      res.set('ETag', etag);294      res.send('hey');295    });296297    request(app)298    .get('/')299    .set('If-None-Match', etag)300    .expect(304, done);301  })302303  it('should respond with 304 Not Modified when fresh', function(done){304    var app = express();305306    app.use(function(req, res){307      var str = Array(1024 * 2).join('-');308      res.send(str);309    });310311    request(app)312    .get('/')313    .set('If-None-Match', 'W/"7ff-2796319984"')314    .expect(304, done);315  })316317  it('should not perform freshness check unless 2xx or 304', function(done){318    var app = express();319    var etag = '"asdf"';320321    app.use(function(req, res, next){322      res.status(500);323      res.set('ETag', etag);324      res.send('hey');325    });326327    request(app)328    .get('/')329    .set('If-None-Match', etag)330    .expect('hey')331    .expect(500, done);332  })333334  it('should not support jsonp callbacks', function(done){335    var app = express();336337    app.use(function(req, res){338      res.send({ foo: 'bar' });339    });340341    request(app)342    .get('/?callback=foo')343    .expect('{"foo":"bar"}', done);344  })345346  describe('"etag" setting', function(){347    describe('when enabled', function(){348      it('should send ETag', function(done){349        var app = express();350351        app.use(function(req, res){352          res.send('kajdslfkasdf');353        });354355        app.enable('etag');356357        request(app)358        .get('/')359        .expect('etag', 'W/"c-1525560792"', done)360      })361362      it('should send ETag for empty string response', function(done){363        var app = express()364365        app.use(function(req, res){366          res.send('')367        });368369        app.enable('etag')370371        request(app)372        .get('/')373        .expect('etag', 'W/"0-0"', done)374      })375376      it('should send ETag for long response', function(done){377        var app = express();378379        app.use(function(req, res){380          var str = Array(1024 * 2).join('-');381          res.send(str);382        });383384        app.enable('etag');385386        request(app)387        .get('/')388        .expect('etag', 'W/"7ff-2796319984"', done)389      });390391      it('should not override ETag when manually set', function(done){392        var app = express();393394        app.use(function(req, res){395          res.set('etag', '"asdf"');396          res.send(200);397        });398399        app.enable('etag');400401        request(app)402        .get('/')403        .expect('etag', '"asdf"', done)404      });405406      it('should not send ETag for res.send()', function(done){407        var app = express()408409        app.use(function(req, res){410          res.send()411        });412413        app.enable('etag')414415        request(app)416        .get('/')417        .end(function(err, res){418          res.headers.should.not.have.property('etag');419          done();420        })421      })422    });423424    describe('when disabled', function(){425      it('should send no ETag', function(done){426        var app = express();427428        app.use(function(req, res){429          var str = Array(1024 * 2).join('-');430          res.send(str);431        });432433        app.disable('etag');434435        request(app)436        .get('/')437        .end(function(err, res){438          res.headers.should.not.have.property('etag');439          done();440        });441      });442443      it('should send ETag when manually set', function(done){444        var app = express();445446        app.disable('etag');447448        app.use(function(req, res){449          res.set('etag', '"asdf"');450          res.send(200);451        });452453        request(app)454        .get('/')455        .expect('etag', '"asdf"', done)456      });457    });458459    describe('when "strong"', function(){460      it('should send strong ETag', function(done){461        var app = express()462463        app.set('etag', 'strong');464465        app.use(function(req, res){466          res.send('hello, world!');467        });468469        request(app)470        .get('/')471        .expect('etag', '"Otu60XkfuuPskIiUxJY4cA=="', done)472      })473    })474475    describe('when "weak"', function(){476      it('should send weak ETag', function(done){477        var app = express()478479        app.set('etag', 'weak');480481        app.use(function(req, res){482          res.send('hello, world!');483        });484485        request(app)486        .get('/')487        .expect('etag', 'W/"d-1486392595"', done)488      })489    })490491    describe('when a function', function(){492      it('should send custom ETag', function(done){493        var app = express()494495        app.set('etag', function(body, encoding){496          body.should.equal('hello, world!')497          encoding.should.equal('utf8')498          return '"custom"'499        });500501        app.use(function(req, res){502          res.send('hello, world!');503        });504505        request(app)506        .get('/')507        .expect('etag', '"custom"', done)508      })509510      it('should not send falsy ETag', function(done){511        var app = express()512513        app.set('etag', function(body, encoding){514          return undefined515        });516517        app.use(function(req, res){518          res.send('hello, world!');519        });520521        request(app)522        .get('/')523        .end(function(err, res){524          res.headers.should.not.have.property('etag')525          done();526        })527      })528    })529  })530})