Permalink
Browse files

Added `req.is()` tests

  • Loading branch information...
1 parent 144a88b commit 014fb464492848d5892e6f77432c0a3be8404ca9 @tj tj committed Dec 30, 2011
Showing with 140 additions and 1 deletion.
  1. +25 −0 client.js
  2. +1 −1 lib/request.js
  3. +13 −0 test.js
  4. +101 −0 test/req.is.js
View
@@ -0,0 +1,25 @@
+
+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();
+}
View
@@ -310,7 +310,7 @@ req.is = function(type){
var fn = this.app.is(type);
if (fn) return fn(this);
var contentType = this.headers['content-type'];
- if (!contentType) return;
+ if (!contentType) return false;
if (!~type.indexOf('/')) type = mime.lookup(type);
if (~type.indexOf('*')) {
type = type.split('/');
View
13 test.js
@@ -0,0 +1,13 @@
+
+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');
View
@@ -0,0 +1,101 @@
+
+var express = require('../')
+ , request = require('./support/http');
+
+function req(ct) {
+ var req = {
+ headers: { 'content-type': ct }
+ , app: express()
+ , __proto__: express.request
+ };
+
+ return req;
+}
+
+describe('req.is()', function(){
+ it('should ignore charset', function(){
+ req('application/json; charset=utf-8')
+ .is('json')
+ .should.be.true;
+ })
+
+ describe('when content-type is not present', function(){
+ it('should return false', function(){
+ req('')
+ .is('json')
+ .should.be.false;
+ })
+ })
+
+ describe('when given an extension', function(){
+ it('should lookup the mime type', function(){
+ req('application/json')
+ .is('json')
+ .should.be.true;
+
+ req('text/html')
+ .is('json')
+ .should.be.false;
+ })
+ })
+
+ describe('when given a mime type', function(){
+ it('should match', function(){
+ req('application/json')
+ .is('application/json')
+ .should.be.true;
+
+ req('image/jpeg')
+ .is('application/json')
+ .should.be.false;
+ })
+ })
+
+ describe('when given */subtype', function(){
+ it('should match', function(){
+ req('application/json')
+ .is('*/json')
+ .should.be.true;
+
+ req('image/jpeg')
+ .is('*/json')
+ .should.be.false;
+ })
+
+ describe('with a charset', function(){
+ it('should match', function(){
+ req('text/html; charset=utf-8')
+ .is('*/html')
+ .should.be.true;
+
+ req('text/plain; charset=utf-8')
+ .is('*/html')
+ .should.be.false;
+ })
+ })
+ })
+
+ describe('when given type/*', function(){
+ it('should match', function(){
+ req('image/png')
+ .is('image/*')
+ .should.be.true;
+
+ req('text/html')
+ .is('image/*')
+ .should.be.false;
+ })
+
+ describe('with a charset', function(){
+ it('should match', function(){
+ req('text/html; charset=utf-8')
+ .is('text/*')
+ .should.be.true;
+
+ req('something/html; charset=utf-8')
+ .is('text/*')
+ .should.be.false;
+ })
+ })
+ })
+})

0 comments on commit 014fb46

Please sign in to comment.