tests: update res.append to verify separate header lines
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 7df0c84 · Browse files · 1 file·+41 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+41 added−30 removed
- tests:+41 added−30 removed
Provenance
- Commit
9967ffb- Tree
f13032a- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
7df0c84
1 file changed+41 added−30 removed
modified:test/res.append.js+41 added-30 removed
1234556789101189101112171314151617221819202122272823242526272829303 unmodified lines343536374041424344383940414243444546475152484950515253575455565758626359606162636465666768706970717273757475767778808179808182838484858687888990919293939495969798989910010110299100101102103104105106107108109110111112113114115116'use strict'
var assert = require('assert')var express = require('..')var request = require('supertest')var should = require('should')
describe('res', function () { // note about these tests: "Link" and "X-*" are chosen because // the common node.js versions white list which _incoming_ // headers can appear multiple times; there is no such white list // for outgoing, though describe('.append(field, val)', function () { it('should append multiple headers', function (done) { var app = express()
app.use(function (req, res, next) { res.append('Link', '<http://localhost/>') res.append('Set-Cookie', 'foo=bar') next() })
app.use(function (req, res) { res.append('Link', '<http://localhost:80/>') res.append('Set-Cookie', 'fizz=buzz') res.end() })
request(app) .get('/') .expect('Link', '<http://localhost/>, <http://localhost:80/>', done) .get('/') .expect(200) .expect(shouldHaveHeaderValues('Set-Cookie', ['foo=bar', 'fizz=buzz'])) .end(done) })
it('should accept array of values', function (done) { var app = express()3 unmodified lines res.end() })
request(app) .get('/') .expect(function (res) { should(res.headers['set-cookie']).eql(['foo=bar', 'fizz=buzz']) }) .expect(200, done) .get('/') .expect(200) .expect(shouldHaveHeaderValues('Set-Cookie', ['foo=bar', 'fizz=buzz'])) .end(done) })
it('should get reset by res.set(field, val)', function (done) { var app = express()
app.use(function (req, res, next) { res.append('Link', '<http://localhost/>') res.append('Link', '<http://localhost:80/>') res.append('Set-Cookie', 'foo=bar') res.append('Set-Cookie', 'fizz=buzz') next() })
app.use(function (req, res) { res.set('Link', '<http://127.0.0.1/>') res.set('Set-Cookie', 'pet=tobi') res.end() });
request(app) .get('/') .expect('Link', '<http://127.0.0.1/>', done) .get('/') .expect(200) .expect(shouldHaveHeaderValues('Set-Cookie', ['pet=tobi'])) .end(done) })
it('should work with res.set(field, val) first', function (done) { var app = express()
app.use(function (req, res, next) { res.set('Link', '<http://localhost/>') res.set('Set-Cookie', 'foo=bar') next() })
app.use(function(req, res){ res.append('Link', '<http://localhost:80/>') res.append('Set-Cookie', 'fizz=buzz') res.end() })
request(app) .get('/') .expect('Link', '<http://localhost/>, <http://localhost:80/>', done) .get('/') .expect(200) .expect(shouldHaveHeaderValues('Set-Cookie', ['foo=bar', 'fizz=buzz'])) .end(done) })
it('should work with cookies', function (done) { it('should work together with res.cookie', function (done) { var app = express()
app.use(function (req, res, next) { res.cookie('foo', 'bar') next() })
app.use(function (req, res) { res.append('Set-Cookie', 'bar=baz') res.append('Set-Cookie', 'fizz=buzz') res.end() })
request(app) .get('/') .expect(function (res) { should(res.headers['set-cookie']).eql(['foo=bar; Path=/', 'bar=baz']) }) .expect(200, done) .get('/') .expect(200) .expect(shouldHaveHeaderValues('Set-Cookie', ['foo=bar; Path=/', 'fizz=buzz'])) .end(done) }) })})
function shouldHaveHeaderValues (key, values) { return function (res) { var headers = res.headers[key.toLowerCase()] assert.ok(headers, 'should have header "' + key + '"') assert.strictEqual(headers.length, values.length, 'should have ' + values.length + ' occurances of "' + key + '"') for (var i = 0; i < values.length; i++) { assert.strictEqual(headers[i], values[i]) } }}