Skip to content
Myr

res.send.js

This file

Path

test/res.send.js

534 lines · 12.1 KiB · JavaScript

Last changed by

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

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('Content-Length', '0')18      .expect('', done);19    })20  })2122  describe('.send(undefined)', function(){23    it('should set body to ""', function(done){24      var app = express();2526      app.use(function(req, res){27        res.send(undefined);28      });2930      request(app)31      .get('/')32      .expect('', function(req, res){33        res.header.should.not.have.property('content-length');34        done();35      });36    })37  })3839  describe('.send(code)', function(){40    it('should set .statusCode', function(done){41      var app = express();4243      app.use(function(req, res){44        res.send(201).should.equal(res);45      });4647      request(app)48      .get('/')49      .expect('Created')50      .expect(201, done);51    })52  })5354  describe('.send(code, body)', function(){55    it('should set .statusCode and body', function(done){56      var app = express();5758      app.use(function(req, res){59        res.send(201, 'Created :)');60      });6162      request(app)63      .get('/')64      .expect('Created :)')65      .expect(201, done);66    })67  })6869  describe('.send(body, code)', function(){70    it('should be supported for backwards compat', function(done){71      var app = express();7273      app.use(function(req, res){74        res.send('Bad!', 400);75      });7677      request(app)78      .get('/')79      .expect('Bad!')80      .expect(400, done);81    })82  })8384  describe('.send(String)', function(){85    it('should send as html', function(done){86      var app = express();8788      app.use(function(req, res){89        res.send('<p>hey</p>');90      });9192      request(app)93      .get('/')94      .end(function(err, res){95        res.headers.should.have.property('content-type', 'text/html; charset=utf-8');96        res.text.should.equal('<p>hey</p>');97        res.statusCode.should.equal(200);98        done();99      })100    })101102    it('should set ETag', function(done){103      var app = express();104105      app.use(function(req, res){106        var str = Array(1024 * 2).join('-');107        res.send(str);108      });109110      request(app)111      .get('/')112      .expect('ETag', 'W/"7ff-2796319984"')113      .end(done);114    })115116    it('should not set ETag for non-GET/HEAD', function(done){117      var app = express();118119      app.use(function(req, res){120        var str = Array(1024 * 2).join('-');121        res.send(str);122      });123124      request(app)125      .post('/')126      .end(function(err, res){127        if (err) return done(err);128        assert(!res.header.etag, 'has an ETag');129        done();130      });131    })132133    it('should not override Content-Type', function(done){134      var app = express();135136      app.use(function(req, res){137        res.set('Content-Type', 'text/plain').send('hey');138      });139140      request(app)141      .get('/')142      .expect('Content-Type', 'text/plain; charset=utf-8')143      .expect(200, 'hey', done);144    })145146    it('should override charset in Content-Type', function(done){147      var app = express();148149      app.use(function(req, res){150        res.set('Content-Type', 'text/plain; charset=iso-8859-1').send('hey');151      });152153      request(app)154      .get('/')155      .expect('Content-Type', 'text/plain; charset=utf-8')156      .expect(200, 'hey', done);157    })158159    it('should keep charset in Content-Type for Buffers', function(done){160      var app = express();161162      app.use(function(req, res){163        res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(new Buffer('hi'));164      });165166      request(app)167      .get('/')168      .expect('Content-Type', 'text/plain; charset=iso-8859-1')169      .expect(200, 'hi', done);170    })171  })172173  describe('.send(Buffer)', function(){174    it('should send as octet-stream', function(done){175      var app = express();176177      app.use(function(req, res){178        res.send(new Buffer('hello'));179      });180181      request(app)182      .get('/')183      .end(function(err, res){184        res.headers.should.have.property('content-type', 'application/octet-stream');185        res.text.should.equal('hello');186        res.statusCode.should.equal(200);187        done();188      })189    })190191    it('should set ETag', function(done){192      var app = express();193194      app.use(function(req, res){195        var str = Array(1024 * 2).join('-');196        res.send(new Buffer(str));197      });198199      request(app)200      .get('/')201      .expect('ETag', 'W/"7ff-2796319984"')202      .end(done);203    })204205    it('should not override Content-Type', function(done){206      var app = express();207208      app.use(function(req, res){209        res.set('Content-Type', 'text/plain').send(new Buffer('hey'));210      });211212      request(app)213      .get('/')214      .end(function(err, res){215        res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');216        res.text.should.equal('hey');217        res.statusCode.should.equal(200);218        done();219      })220    })221  })222223  describe('.send(Object)', function(){224    it('should send as application/json', function(done){225      var app = express();226227      app.use(function(req, res){228        res.send({ name: 'tobi' });229      });230231      request(app)232      .get('/')233      .expect('Content-Type', 'application/json; charset=utf-8')234      .expect(200, '{"name":"tobi"}', done)235    })236  })237238  describe('when the request method is HEAD', function(){239    it('should ignore the body', function(done){240      var app = express();241242      app.use(function(req, res){243        res.send('yay');244      });245246      request(app)247      .head('/')248      .expect('', done);249    })250  })251252  describe('when .statusCode is 204', function(){253    it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){254      var app = express();255256      app.use(function(req, res){257        res.status(204).set('Transfer-Encoding', 'chunked').send('foo');258      });259260      request(app)261      .get('/')262      .end(function(err, res){263        res.headers.should.not.have.property('content-type');264        res.headers.should.not.have.property('content-length');265        res.headers.should.not.have.property('transfer-encoding');266        res.text.should.equal('');267        done();268      })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      .end(function(err, res){283        res.headers.should.not.have.property('content-type');284        res.headers.should.not.have.property('content-length');285        res.headers.should.not.have.property('transfer-encoding');286        res.text.should.equal('');287        done();288      })289    })290  })291292  it('should always check regardless of length', function(done){293    var app = express();294    var etag = '"asdf"';295296    app.use(function(req, res, next){297      res.set('ETag', etag);298      res.send('hey');299    });300301    request(app)302    .get('/')303    .set('If-None-Match', etag)304    .expect(304, done);305  })306307  it('should respond with 304 Not Modified when fresh', function(done){308    var app = express();309310    app.use(function(req, res){311      var str = Array(1024 * 2).join('-');312      res.send(str);313    });314315    request(app)316    .get('/')317    .set('If-None-Match', 'W/"7ff-2796319984"')318    .expect(304, done);319  })320321  it('should not perform freshness check unless 2xx or 304', function(done){322    var app = express();323    var etag = '"asdf"';324325    app.use(function(req, res, next){326      res.status(500);327      res.set('ETag', etag);328      res.send('hey');329    });330331    request(app)332    .get('/')333    .set('If-None-Match', etag)334    .expect('hey')335    .expect(500, done);336  })337338  it('should not support jsonp callbacks', function(done){339    var app = express();340341    app.use(function(req, res){342      res.send({ foo: 'bar' });343    });344345    request(app)346    .get('/?callback=foo')347    .expect('{"foo":"bar"}', done);348  })349350  describe('"etag" setting', function(){351    describe('when enabled', function(){352      it('should send ETag', function(done){353        var app = express();354355        app.use(function(req, res){356          res.send('kajdslfkasdf');357        });358359        app.enable('etag');360361        request(app)362        .get('/')363        .expect('etag', 'W/"c-1525560792"', done)364      })365366      it('should send ETag for empty string response', function(done){367        var app = express()368369        app.use(function(req, res){370          res.send('')371        });372373        app.enable('etag')374375        request(app)376        .get('/')377        .expect('etag', 'W/"0-0"', done)378      })379380      it('should send ETag for long response', function(done){381        var app = express();382383        app.use(function(req, res){384          var str = Array(1024 * 2).join('-');385          res.send(str);386        });387388        app.enable('etag');389390        request(app)391        .get('/')392        .expect('etag', 'W/"7ff-2796319984"', done)393      });394395      it('should not override ETag when manually set', function(done){396        var app = express();397398        app.use(function(req, res){399          res.set('etag', '"asdf"');400          res.send(200);401        });402403        app.enable('etag');404405        request(app)406        .get('/')407        .expect('etag', '"asdf"', done)408      });409410      it('should not send ETag for res.send()', function(done){411        var app = express()412413        app.use(function(req, res){414          res.send()415        });416417        app.enable('etag')418419        request(app)420        .get('/')421        .end(function(err, res){422          res.headers.should.not.have.property('etag');423          done();424        })425      })426    });427428    describe('when disabled', function(){429      it('should send no ETag', function(done){430        var app = express();431432        app.use(function(req, res){433          var str = Array(1024 * 2).join('-');434          res.send(str);435        });436437        app.disable('etag');438439        request(app)440        .get('/')441        .end(function(err, res){442          res.headers.should.not.have.property('etag');443          done();444        });445      });446447      it('should send ETag when manually set', function(done){448        var app = express();449450        app.disable('etag');451452        app.use(function(req, res){453          res.set('etag', '"asdf"');454          res.send(200);455        });456457        request(app)458        .get('/')459        .expect('etag', '"asdf"', done)460      });461    });462463    describe('when "strong"', function(){464      it('should send strong ETag', function(done){465        var app = express()466467        app.set('etag', 'strong');468469        app.use(function(req, res){470          res.send('hello, world!');471        });472473        request(app)474        .get('/')475        .expect('etag', '"Otu60XkfuuPskIiUxJY4cA=="', done)476      })477    })478479    describe('when "weak"', function(){480      it('should send weak ETag', function(done){481        var app = express()482483        app.set('etag', 'weak');484485        app.use(function(req, res){486          res.send('hello, world!');487        });488489        request(app)490        .get('/')491        .expect('etag', 'W/"d-1486392595"', done)492      })493    })494495    describe('when a function', function(){496      it('should send custom ETag', function(done){497        var app = express()498499        app.set('etag', function(body, encoding){500          body.should.equal('hello, world!')501          encoding.should.equal('utf8')502          return '"custom"'503        });504505        app.use(function(req, res){506          res.send('hello, world!');507        });508509        request(app)510        .get('/')511        .expect('etag', '"custom"', done)512      })513514      it('should not send falsy ETag', function(done){515        var app = express()516517        app.set('etag', function(body, encoding){518          return undefined519        });520521        app.use(function(req, res){522          res.send('hello, world!');523        });524525        request(app)526        .get('/')527        .end(function(err, res){528          res.headers.should.not.have.property('etag')529          done();530        })531      })532    })533  })534})