Skip to content

Commit 2cccbc1

Browse files
committedJun 23, 2014
add deprecation message to non-plural req.accepts*
1 parent 83bbf09 commit 2cccbc1

File tree

7 files changed

+159
-17
lines changed

7 files changed

+159
-17
lines changed
 

‎History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* add deprecation message to non-plural `req.accepts*`
45
* deprecate things with `depd` module
56
* fix behavior when handling request without routes
67
* fix handling when `route.all` is only route

‎lib/request.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
var accepts = require('accepts');
6+
var deprecate = require('depd')('express');
67
var typeis = require('type-is');
78
var http = require('http');
89
var fresh = require('fresh');
@@ -106,53 +107,55 @@ req.accepts = function(){
106107
};
107108

108109
/**
109-
* Check if the given `encoding` is accepted.
110+
* Check if the given `encoding`s are accepted.
110111
*
111-
* @param {String} encoding
112+
* @param {String} ...encoding
112113
* @return {Boolean}
113114
* @api public
114115
*/
115116

116-
req.acceptsEncoding = // backwards compatibility
117117
req.acceptsEncodings = function(){
118118
var accept = accepts(this);
119119
return accept.encodings.apply(accept, arguments);
120120
};
121121

122+
req.acceptsEncoding = deprecate.function(req.acceptsEncodings,
123+
'req.acceptsEncoding: Use acceptsEncodings instead');
124+
122125
/**
123-
* To do: update docs.
124-
*
125-
* Check if the given `charset` is acceptable,
126+
* Check if the given `charset`s are acceptable,
126127
* otherwise you should respond with 406 "Not Acceptable".
127128
*
128-
* @param {String} charset
129+
* @param {String} ...charset
129130
* @return {Boolean}
130131
* @api public
131132
*/
132133

133-
req.acceptsCharset = // backwards compatibility
134134
req.acceptsCharsets = function(){
135135
var accept = accepts(this);
136136
return accept.charsets.apply(accept, arguments);
137137
};
138138

139+
req.acceptsCharset = deprecate.function(req.acceptsCharsets,
140+
'req.acceptsCharset: Use acceptsCharsets instead');
141+
139142
/**
140-
* To do: update docs.
141-
*
142-
* Check if the given `lang` is acceptable,
143+
* Check if the given `lang`s are acceptable,
143144
* otherwise you should respond with 406 "Not Acceptable".
144145
*
145-
* @param {String} lang
146+
* @param {String} ...lang
146147
* @return {Boolean}
147148
* @api public
148149
*/
149150

150-
req.acceptsLanguage = // backwards compatibility
151151
req.acceptsLanguages = function(){
152152
var accept = accepts(this);
153153
return accept.languages.apply(accept, arguments);
154154
};
155155

156+
req.acceptsLanguage = deprecate.function(req.acceptsLanguages,
157+
'req.acceptsLanguage: Use acceptsLanguages instead');
158+
156159
/**
157160
* Parse Range header field,
158161
* capping to the given `size`.

‎test/req.acceptsCharset.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('req', function(){
99
var app = express();
1010

1111
app.use(function(req, res, next){
12-
res.end(req.acceptsCharsets('utf-8') ? 'yes' : 'no');
12+
res.end(req.acceptsCharset('utf-8') ? 'yes' : 'no');
1313
});
1414

1515
request(app)
@@ -23,7 +23,7 @@ describe('req', function(){
2323
var app = express();
2424

2525
app.use(function(req, res, next){
26-
res.end(req.acceptsCharsets('utf-8') ? 'yes' : 'no');
26+
res.end(req.acceptsCharset('utf-8') ? 'yes' : 'no');
2727
});
2828

2929
request(app)
@@ -36,7 +36,7 @@ describe('req', function(){
3636
var app = express();
3737

3838
app.use(function(req, res, next){
39-
res.end(req.acceptsCharsets('utf-8') ? 'yes' : 'no');
39+
res.end(req.acceptsCharset('utf-8') ? 'yes' : 'no');
4040
});
4141

4242
request(app)

‎test/req.acceptsCharsets.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
var express = require('../')
3+
, request = require('supertest');
4+
5+
describe('req', function(){
6+
describe('.acceptsCharsets(type)', function(){
7+
describe('when Accept-Charset is not present', function(){
8+
it('should return true', function(done){
9+
var app = express();
10+
11+
app.use(function(req, res, next){
12+
res.end(req.acceptsCharsets('utf-8') ? 'yes' : 'no');
13+
});
14+
15+
request(app)
16+
.get('/')
17+
.expect('yes', done);
18+
})
19+
})
20+
21+
describe('when Accept-Charset is not present', function(){
22+
it('should return true when present', function(done){
23+
var app = express();
24+
25+
app.use(function(req, res, next){
26+
res.end(req.acceptsCharsets('utf-8') ? 'yes' : 'no');
27+
});
28+
29+
request(app)
30+
.get('/')
31+
.set('Accept-Charset', 'foo, bar, utf-8')
32+
.expect('yes', done);
33+
})
34+
35+
it('should return false otherwise', function(done){
36+
var app = express();
37+
38+
app.use(function(req, res, next){
39+
res.end(req.acceptsCharsets('utf-8') ? 'yes' : 'no');
40+
});
41+
42+
request(app)
43+
.get('/')
44+
.set('Accept-Charset', 'foo, bar')
45+
.expect('no', done);
46+
})
47+
})
48+
})
49+
})

‎test/req.acceptsEncoding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var express = require('../')
33
, request = require('supertest');
44

55
describe('req', function(){
6-
describe('.acceptsEncodings', function(){
6+
describe('.acceptsEncoding', function(){
77
it('should be true if encoding accpeted', function(done){
88
var app = express();
99

‎test/req.acceptsEncodings.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
var express = require('../')
3+
, request = require('supertest');
4+
5+
describe('req', function(){
6+
describe('.acceptsEncodingss', function(){
7+
it('should be true if encoding accpeted', function(done){
8+
var app = express();
9+
10+
app.use(function(req, res){
11+
req.acceptsEncodings('gzip').should.be.ok;
12+
req.acceptsEncodings('deflate').should.be.ok;
13+
res.end();
14+
});
15+
16+
request(app)
17+
.get('/')
18+
.set('Accept-Encoding', ' gzip, deflate')
19+
.expect(200, done);
20+
})
21+
22+
it('should be false if encoding not accpeted', function(done){
23+
var app = express();
24+
25+
app.use(function(req, res){
26+
req.acceptsEncodings('bogus').should.not.be.ok;
27+
res.end();
28+
});
29+
30+
request(app)
31+
.get('/')
32+
.set('Accept-Encoding', ' gzip, deflate')
33+
.expect(200, done);
34+
})
35+
})
36+
})

‎test/req.acceptsLanguages.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
var express = require('../')
3+
, request = require('supertest');
4+
5+
describe('req', function(){
6+
describe('.acceptsLanguages', function(){
7+
it('should be true if language accpeted', function(done){
8+
var app = express();
9+
10+
app.use(function(req, res){
11+
req.acceptsLanguages('en-us').should.be.ok;
12+
req.acceptsLanguages('en').should.be.ok;
13+
res.end();
14+
});
15+
16+
request(app)
17+
.get('/')
18+
.set('Accept-Language', 'en;q=.5, en-us')
19+
.expect(200, done);
20+
})
21+
22+
it('should be false if language not accpeted', function(done){
23+
var app = express();
24+
25+
app.use(function(req, res){
26+
req.acceptsLanguages('es').should.not.be.ok;
27+
res.end();
28+
});
29+
30+
request(app)
31+
.get('/')
32+
.set('Accept-Language', 'en;q=.5, en-us')
33+
.expect(200, done);
34+
})
35+
36+
describe('when Accept-Language is not present', function(){
37+
it('should always return true', function(done){
38+
var app = express();
39+
40+
app.use(function(req, res){
41+
req.acceptsLanguages('en').should.be.ok;
42+
req.acceptsLanguages('es').should.be.ok;
43+
req.acceptsLanguages('jp').should.be.ok;
44+
res.end();
45+
});
46+
47+
request(app)
48+
.get('/')
49+
.expect(200, done);
50+
})
51+
})
52+
})
53+
})

0 commit comments

Comments
 (0)