Added `req.is()` tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 144a88b · Browse files · 4 files·+140 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+140 added−1 removed
- tests:+101 added−0 removed
- other:+39 added−1 removed
Provenance
- Commit
014fb46- Tree
165d135- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
144a88b
4 files changed+140 added−1 removed
modified:lib/request.js+1 added-1 removed
308 unmodified lines309 req.is = function(type){310 var fn = this.app.is(type);311 if (fn) return fn(this);312 var contentType = this.headers['content-type'];313- if (!contentType) return;313+ if (!contentType) return false;314 if (!~type.indexOf('/')) type = mime.lookup(type);315 if (~type.indexOf('*')) {316 type = type.split('/');317 contentType = contentType.split('/');64 unmodified linesadded:test/req.is.js+101 added
1+2+var express = require('../')3+ , request = require('./support/http');4+5+function req(ct) {6+ var req = {7+ headers: { 'content-type': ct }8+ , app: express()9+ , __proto__: express.request10+ };11+12+ return req;13+}14+15+describe('req.is()', function(){16+ it('should ignore charset', function(){17+ req('application/json; charset=utf-8')18+ .is('json')19+ .should.be.true;20+ })21+22+ describe('when content-type is not present', function(){23+ it('should return false', function(){24+ req('')25+ .is('json')26+ .should.be.false;27+ })28+ })29+30+ describe('when given an extension', function(){31+ it('should lookup the mime type', function(){32+ req('application/json')33+ .is('json')34+ .should.be.true;35+36+ req('text/html')37+ .is('json')38+ .should.be.false;39+ })40+ })41+42+ describe('when given a mime type', function(){43+ it('should match', function(){44+ req('application/json')45+ .is('application/json')46+ .should.be.true;47+48+ req('image/jpeg')49+ .is('application/json')50+ .should.be.false;51+ })52+ })53+54+ describe('when given */subtype', function(){55+ it('should match', function(){56+ req('application/json')57+ .is('*/json')58+ .should.be.true;59+60+ req('image/jpeg')61+ .is('*/json')62+ .should.be.false;63+ })64+65+ describe('with a charset', function(){66+ it('should match', function(){67+ req('text/html; charset=utf-8')68+ .is('*/html')69+ .should.be.true;70+71+ req('text/plain; charset=utf-8')72+ .is('*/html')73+ .should.be.false;74+ })75+ })76+ })77+78+ describe('when given type/*', function(){79+ it('should match', function(){80+ req('image/png')81+ .is('image/*')82+ .should.be.true;83+84+ req('text/html')85+ .is('image/*')86+ .should.be.false;87+ })88+89+ describe('with a charset', function(){90+ it('should match', function(){91+ req('text/html; charset=utf-8')92+ .is('text/*')93+ .should.be.true;94+95+ req('something/html; charset=utf-8')96+ .is('text/*')97+ .should.be.false;98+ })99+ })100+ })101+})added:client.js+25 added
12345678910111213141516171819202122232425
var http = require('http');
var times = 50;
while (times--) { var req = http.request({ port: 3000 , method: 'POST' , headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
req.on('response', function(res){ console.log(res.statusCode); });
var n = 500000; while (n--) { req.write('foo=bar&bar=baz&'); }
req.write('foo=bar&bar=baz');
req.end();}No newline at end of fileadded:test.js+13 added
12345678910111213
var express = require('./');var app = express();
app.use(express.logger('dev'));app.use(express.bodyParser());
app.post('/', function(req, res){ res.send('done\n');});
app.listen(3000);console.log('listening on port 3000');No newline at end of file