Skip to content
Myr

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

4 files+140 added−1 removed

modified:lib/request.js+1 added-1 removed
308 unmodified lines
309req.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 lines
added:test/req.is.js+101 added
12var express = require('../')3 , request = require('./support/http');45function req(ct) {6 var req = {7 headers: { 'content-type': ct }8 , app: express()9 , __proto__: express.request10 };1112 return req;13}1415describe('req.is()', function(){16 it('should ignore charset', function(){17 req('application/json; charset=utf-8')18 .is('json')19 .should.be.true;20 })2122 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 })2930 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;3536 req('text/html')37 .is('json')38 .should.be.false;39 })40 })4142 describe('when given a mime type', function(){43 it('should match', function(){44 req('application/json')45 .is('application/json')46 .should.be.true;4748 req('image/jpeg')49 .is('application/json')50 .should.be.false;51 })52 })5354 describe('when given */subtype', function(){55 it('should match', function(){56 req('application/json')57 .is('*/json')58 .should.be.true;5960 req('image/jpeg')61 .is('*/json')62 .should.be.false;63 })6465 describe('with a charset', function(){66 it('should match', function(){67 req('text/html; charset=utf-8')68 .is('*/html')69 .should.be.true;7071 req('text/plain; charset=utf-8')72 .is('*/html')73 .should.be.false;74 })75 })76 })7778 describe('when given type/*', function(){79 it('should match', function(){80 req('image/png')81 .is('image/*')82 .should.be.true;8384 req('text/html')85 .is('image/*')86 .should.be.false;87 })8889 describe('with a charset', function(){90 it('should match', function(){91 req('text/html; charset=utf-8')92 .is('text/*')93 .should.be.true;9495 req('something/html; charset=utf-8')96 .is('text/*')97 .should.be.false;98 })99 })100 })101})
added:client.js+25 added
added:test.js+13 added