Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b95f2ee
commit 855176b
Showing
7 changed files
with
159 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
|
||
var express = require('../') | ||
, res = express.response; | ||
var express = require('..'); | ||
var request = require('supertest'); | ||
|
||
describe('res', function(){ | ||
describe('.get(field)', function(){ | ||
it('should get the response header field', function(){ | ||
res.setHeader('Content-Type', 'text/x-foo'); | ||
res.get('Content-Type').should.equal('text/x-foo'); | ||
res.get('Content-type').should.equal('text/x-foo'); | ||
res.get('content-type').should.equal('text/x-foo'); | ||
it('should get the response header field', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.setHeader('Content-Type', 'text/x-foo'); | ||
res.send(res.get('Content-Type')); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect(200, 'text/x-foo', done); | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,46 @@ | ||
|
||
var express = require('../') | ||
, res = express.response; | ||
var express = require('..'); | ||
var request = require('supertest'); | ||
|
||
describe('res', function(){ | ||
|
||
beforeEach(function() { | ||
res.removeHeader('link'); | ||
}); | ||
|
||
describe('.links(obj)', function(){ | ||
it('should set Link header field', function(){ | ||
res.links({ | ||
next: 'http://api.example.com/users?page=2', | ||
last: 'http://api.example.com/users?page=5' | ||
it('should set Link header field', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.links({ | ||
next: 'http://api.example.com/users?page=2', | ||
last: 'http://api.example.com/users?page=5' | ||
}); | ||
res.end(); | ||
}); | ||
|
||
res.get('link') | ||
.should.equal( | ||
'<http://api.example.com/users?page=2>; rel="next", ' | ||
+ '<http://api.example.com/users?page=5>; rel="last"'); | ||
request(app) | ||
.get('/') | ||
.expect('Link', '<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last"') | ||
.expect(200, done); | ||
}) | ||
|
||
it('should set Link header field for multiple calls', function() { | ||
res.links({ | ||
next: 'http://api.example.com/users?page=2', | ||
last: 'http://api.example.com/users?page=5' | ||
}); | ||
it('should set Link header field for multiple calls', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.links({ | ||
next: 'http://api.example.com/users?page=2', | ||
last: 'http://api.example.com/users?page=5' | ||
}); | ||
|
||
res.links({ | ||
prev: 'http://api.example.com/users?page=1' | ||
}); | ||
|
||
res.links({ | ||
prev: 'http://api.example.com/users?page=1', | ||
res.end(); | ||
}); | ||
|
||
res.get('link') | ||
.should.equal( | ||
'<http://api.example.com/users?page=2>; rel="next", ' | ||
+ '<http://api.example.com/users?page=5>; rel="last", ' | ||
+ '<http://api.example.com/users?page=1>; rel="prev"'); | ||
request(app) | ||
.get('/') | ||
.expect('Link', '<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last", <http://api.example.com/users?page=1>; rel="prev"') | ||
.expect(200, done); | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,96 @@ | ||
|
||
var express = require('../') | ||
, should = require('should'); | ||
|
||
function response() { | ||
var res = Object.create(express.response); | ||
res._headers = {}; | ||
return res; | ||
} | ||
var assert = require('assert'); | ||
var express = require('..'); | ||
var request = require('supertest'); | ||
|
||
describe('res.vary()', function(){ | ||
describe('with no arguments', function(){ | ||
it('should not set Vary', function(){ | ||
var res = response(); | ||
res.vary(); | ||
should.not.exist(res.get('Vary')); | ||
it('should not set Vary', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.vary(); | ||
res.end(); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect(shouldNotHaveHeader('Vary')) | ||
.expect(200, done); | ||
}) | ||
}) | ||
|
||
describe('with an empty array', function(){ | ||
it('should not set Vary', function(){ | ||
var res = response(); | ||
res.vary([]); | ||
should.not.exist(res.get('Vary')); | ||
it('should not set Vary', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.vary([]); | ||
res.end(); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect(shouldNotHaveHeader('Vary')) | ||
.expect(200, done); | ||
}) | ||
}) | ||
|
||
describe('with an array', function(){ | ||
it('should set the values', function(){ | ||
var res = response(); | ||
res.vary(['Accept', 'Accept-Language', 'Accept-Encoding']); | ||
res.get('Vary').should.equal('Accept, Accept-Language, Accept-Encoding'); | ||
it('should set the values', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.vary(['Accept', 'Accept-Language', 'Accept-Encoding']); | ||
res.end(); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect('Vary', 'Accept, Accept-Language, Accept-Encoding') | ||
.expect(200, done); | ||
}) | ||
}) | ||
|
||
describe('with a string', function(){ | ||
it('should set the value', function(){ | ||
var res = response(); | ||
res.vary('Accept'); | ||
res.get('Vary').should.equal('Accept'); | ||
it('should set the value', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.vary('Accept'); | ||
res.end(); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect('Vary', 'Accept') | ||
.expect(200, done); | ||
}) | ||
}) | ||
|
||
describe('when the value is present', function(){ | ||
it('should not add it again', function(){ | ||
var res = response(); | ||
res.vary('Accept'); | ||
res.vary('Accept-Encoding'); | ||
res.vary('Accept-Encoding'); | ||
res.vary('Accept-Encoding'); | ||
res.vary('Accept'); | ||
res.get('Vary').should.equal('Accept, Accept-Encoding'); | ||
it('should not add it again', function (done) { | ||
var app = express(); | ||
|
||
app.use(function (req, res) { | ||
res.vary('Accept'); | ||
res.vary('Accept-Encoding'); | ||
res.vary('Accept-Encoding'); | ||
res.vary('Accept-Encoding'); | ||
res.vary('Accept'); | ||
res.end(); | ||
}); | ||
|
||
request(app) | ||
.get('/') | ||
.expect('Vary', 'Accept, Accept-Encoding') | ||
.expect(200, done); | ||
}) | ||
}) | ||
}) | ||
|
||
function shouldNotHaveHeader(header) { | ||
return function (res) { | ||
assert.ok(!(header.toLowerCase() in res.headers), 'should not have header ' + header); | ||
}; | ||
} |