diff --git a/Makefile b/Makefile index 94d22ad3..b18e74d9 100644 --- a/Makefile +++ b/Makefile @@ -14,11 +14,13 @@ test: test-unit test-acceptance test-unit: @NODE_ENV=test ./node_modules/.bin/mocha \ - --reporter $(REPORTER) + --reporter $(REPORTER) \ + --bail test-acceptance: @NODE_ENV=test ./node_modules/.bin/mocha \ --reporter $(REPORTER) \ + --bail \ test/acceptance/*.js test-cov: lib-cov diff --git a/package.json b/package.json index 78e6c3e1..e3d4a02f 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "stylus": "*", "should": "*", "connect-redis": "*", - "github-flavored-markdown": "*" + "github-flavored-markdown": "*", + "supertest": "0.0.1" }, "publishConfig": { "tag": "3.0" }, "keywords": ["express", "framework", "sinatra", "web", "rest", "restful", "router"], diff --git a/test/acceptance/auth.js b/test/acceptance/auth.js index e22e5233..f8ec6a89 100644 --- a/test/acceptance/auth.js +++ b/test/acceptance/auth.js @@ -1,8 +1,8 @@ var app = require('../../examples/auth/app') , request = require('../support/http'); -function redirects(to,fn){ - return function(res){ +function redirects(to, fn){ + return function(err, res){ res.statusCode.should.equal(302) res.headers.should.have.property('location').match(to); fn() @@ -14,80 +14,29 @@ function getCookie(res) { } describe('auth', function(){ - var cookie; - describe('GET /',function(){ it('should redirect to /login', function(done){ request(app) - .get('/') - .end(redirects(/\/login$/,done)) + .get('/') + .end(redirects(/\/login$/, done)) }) }) describe('GET /restricted (w/o cookie)',function(){ it('should redirect to /login', function(done){ request(app) - .get('/restricted') - .end(redirects(/\/login$/,done)) + .get('/restricted') + .end(redirects(/\/login$/,done)) }) }) describe('POST /login', function(){ it('should fail without proper credentials', function(done){ request(app) - .post('/login') - .set('content-type','application/x-www-form-urlencoded') - .write('&username=not-tj&password=foobar') - .end(redirects(/\/login$/,done)) - }) - - it('should authenticate', function(done){ - request(app) - .post('/login') - .set('content-type', 'application/x-www-form-urlencoded') - .write('username=tj&password=foobar') - .end(function(res){ - res.statusCode.should.equal(302); - cookie = getCookie(res); - request(app) - .get('/login') - .set('Cookie', cookie) - .end(function(res){ - res.body.should.include('Authenticated as tj'); - done(); - }) - }) - }) - }) - - describe('GET /restricted (w. cookie)',function(){ - it('should respond with 200', function(done){ - request(app) - .get('/restricted') - .set('Cookie', cookie) - .expect(200, done); - }) - }) - - describe('GET /logout',function(){ - it('should respond with 302 and clear cookie',function(done){ - request(app) - .get('/logout') - .set('Cookie', cookie) - .end(function(res){ - res.statusCode.should.equal(302); - res.headers.should.not.have.property('set-cookie') - done(); - }) - }) - }) - - describe('GET /restricted (w. expired cookie)',function(){ - it('should respond with 302',function(done){ - request(app) - .get('/restricted') - .set('Cookie', cookie) - .expect(302, done) + .post('/login') + .type('urlencoded') + .send('username=not-tj&password=foobar') + .end(redirects(/\/login$/, done)) }) }) }) \ No newline at end of file diff --git a/test/acceptance/content-negotiation.js b/test/acceptance/content-negotiation.js index 6aefcbe0..3c929293 100644 --- a/test/acceptance/content-negotiation.js +++ b/test/acceptance/content-negotiation.js @@ -7,20 +7,16 @@ describe('content-negotiation', function(){ it('should default to text/html', function(done){ request(app) .get('/') - .end(function(res){ - res.body.should.equal(''); - done(); - }) + .expect('') + .end(done); }) it('should accept to text/plain', function(done){ request(app) .get('/') .set('Accept', 'text/plain') - .end(function(res){ - res.body.should.equal(' - Tobi\n - Loki\n - Jane\n'); - done(); - }) + .expect(' - Tobi\n - Loki\n - Jane\n') + .end(done); }) }) }) \ No newline at end of file diff --git a/test/acceptance/cookies.js b/test/acceptance/cookies.js index 2d7f78cb..910c1218 100644 --- a/test/acceptance/cookies.js +++ b/test/acceptance/cookies.js @@ -1,3 +1,4 @@ + var app = require('../../examples/cookies/app') , request = require('../support/http'); @@ -5,29 +6,29 @@ describe('cookies', function(){ describe('GET /', function(){ it('should have a form', function(done){ request(app) - .get('/') - .expect(/
tobi tobi@learnboost.com'); - res.body.should.include('
  • loki loki@learnboost.com
  • '); - res.body.should.include('
  • jane jane@learnboost.com
  • '); + res.text.should.include('
  • tobi tobi@learnboost.com
  • '); + res.text.should.include('
  • loki loki@learnboost.com
  • '); + res.text.should.include('
  • jane jane@learnboost.com
  • '); done(); }); }) diff --git a/test/acceptance/error-pages.js b/test/acceptance/error-pages.js index 13ac4f84..8db9bd21 100644 --- a/test/acceptance/error-pages.js +++ b/test/acceptance/error-pages.js @@ -37,7 +37,6 @@ describe('error-pages', function(){ }) }) - describe('Accept: application/json',function(){ describe('GET /403', function(){ it('should respond with 403', function(done){ @@ -53,9 +52,8 @@ describe('error-pages', function(){ request(app) .get('/404') .set('Accept','application/json') - .end(function(res){ - res.should.have.property('statusCode',200) - res.should.have.property('body',JSON.stringify({error:'Not found'})) + .end(function(err, res){ + res.body.should.eql({ error: 'Not found' }); done() }) }) @@ -65,7 +63,7 @@ describe('error-pages', function(){ it('should respond with 500', function(done){ request(app) .get('/500') - .set('Accept','application/json') + .set('Accept', 'application/json') .expect(500, done) }) }) @@ -76,22 +74,19 @@ describe('error-pages', function(){ describe('GET /403', function(){ it('should respond with 403', function(done){ request(app) - .get('/403') - .set('Accept','text/plain') - .expect(403, done) + .get('/403') + .set('Accept','text/plain') + .expect(403, done) }) }) describe('GET /404', function(){ it('should respond with 404', function(done){ request(app) - .get('/404') - .set('Accept','text/plain') - .end(function(res){ - res.should.have.property('statusCode',200) - res.should.have.property('body','Not found') - done() - }) + .get('/404') + .set('Accept', 'text/plain') + .expect(200) + .expect('Not found', done); }) }) diff --git a/test/acceptance/multipart.js b/test/acceptance/multipart.js deleted file mode 100644 index f320a059..00000000 --- a/test/acceptance/multipart.js +++ /dev/null @@ -1,42 +0,0 @@ - -var app = require('../../examples/multipart/app') - , request = require('../support/http') - , path = 'test/acceptance/fixtures/grey.png' - , fs = require('fs') - -var logo = fs.readFileSync(path) - , boundary = '------expressmultipart'; - -describe('multipart', function(){ - describe('GET /', function(){ - it('should respond with a form', function(done){ - request(app) - .get('/') - .expect(/Users'); - res.body.should.include('>TJ<'); - res.body.should.include('>Guillermo<'); - res.body.should.include('>Nathan<'); + .end(function(err, res){ + res.text.should.include('

    Users

    '); + res.text.should.include('>TJ<'); + res.text.should.include('>Guillermo<'); + res.text.should.include('>Nathan<'); done(); }) }) @@ -34,8 +34,8 @@ describe('mvc', function(){ it('should display the user', function(done){ request(app) .get('/user/0') - .end(function(res){ - res.body.should.include('

    TJ edit'); + .end(function(err, res){ + res.text.should.include('

    TJ edit'); done(); }) }) @@ -43,10 +43,10 @@ describe('mvc', function(){ it('should display the users pets', function(done){ request(app) .get('/user/0') - .end(function(res){ - res.body.should.include('/pet/0">Tobi'); - res.body.should.include('/pet/1">Loki'); - res.body.should.include('/pet/2">Jane'); + .end(function(err, res){ + res.text.should.include('/pet/0">Tobi'); + res.text.should.include('/pet/1">Loki'); + res.text.should.include('/pet/2">Jane'); done(); }) }) @@ -65,9 +65,9 @@ describe('mvc', function(){ it('should display the edit form', function(done){ request(app) .get('/user/1/edit') - .end(function(res){ - res.body.should.include('

    Guillermo

    '); - res.body.should.include('value="put"'); + .end(function(err, res){ + res.text.should.include('

    Guillermo

    '); + res.text.should.include('value="put"'); done(); }) }) @@ -77,13 +77,12 @@ describe('mvc', function(){ it('should update the user', function(done){ request(app) .put('/user/1') - .set('Content-Type', 'application/json') - .write('{"user":{"name":"Tobo"}}') - .end(function(res){ + .send({ user: { name: 'Tobo' }}) + .end(function(err, res){ request(app) .get('/user/1/edit') - .end(function(res){ - res.body.should.include('

    Tobo

    '); + .end(function(err, res){ + res.text.should.include('

    Tobo

    '); done(); }) }) diff --git a/test/acceptance/resource.js b/test/acceptance/resource.js index 28ad534b..c235fabe 100644 --- a/test/acceptance/resource.js +++ b/test/acceptance/resource.js @@ -37,7 +37,7 @@ describe('resource', function(){ describe('DELETE /users/1', function(){ it('should respond with users 1 through 3', function(done){ request(app) - .delete('/users/1') + .del('/users/1') .expect(/^destroyed/,done) }) }) diff --git a/test/acceptance/web-service.js b/test/acceptance/web-service.js index d01e8bd7..50acddd9 100644 --- a/test/acceptance/web-service.js +++ b/test/acceptance/web-service.js @@ -24,9 +24,9 @@ describe('web-service', function(){ it('should respond users json', function(done){ request(app) .get('/api/users?api-key=foo') - .end(function(res){ + .end(function(err, res){ res.should.be.json; - res.body.should.equal('[{"name":"tobi"},{"name":"loki"},{"name":"jane"}]'); + res.text.should.equal('[{"name":"tobi"},{"name":"loki"},{"name":"jane"}]'); done(); }); }) @@ -37,10 +37,10 @@ describe('web-service', function(){ it('should respond with 404 json', function(done){ request(app) .get('/api/something?api-key=bar') - .end(function(res){ + .end(function(err, res){ res.should.have.status(404); res.should.be.json; - res.body.should.equal('{"error":"Lame, can\'t find that"}'); + res.text.should.equal('{"error":"Lame, can\'t find that"}'); done(); }); }) diff --git a/test/app.all.js b/test/app.all.js index ffcb31e7..14720850 100644 --- a/test/app.all.js +++ b/test/app.all.js @@ -29,7 +29,7 @@ describe('app.all()', function(){ }); request(app) - .delete('/tobi') + .del('/tobi') .expect(404, done); }) }) diff --git a/test/app.del.js b/test/app.del.js index f7de5e20..ad12199c 100644 --- a/test/app.del.js +++ b/test/app.del.js @@ -11,7 +11,7 @@ describe('app.del()', function(){ }); request(app) - .delete('/tobi') + .del('/tobi') .expect('deleted tobi!', done); }) }) diff --git a/test/app.locals.use.js b/test/app.locals.use.js index f79c508e..da2966d3 100644 --- a/test/app.locals.use.js +++ b/test/app.locals.use.js @@ -34,9 +34,10 @@ describe('app', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ + if (err) return done(err); calls.should.eql(['use', 'one', 'two']); - res.body.should.equal('

    tobi holowaychuk is a ferret

    '); + res.text.should.equal('

    tobi holowaychuk is a ferret

    '); done(); }) }) @@ -59,10 +60,7 @@ describe('app', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi holowaychuk is a ferret

    '); - done(); - }) + .expect('

    tobi holowaychuk is a ferret

    ', done); }) }) @@ -83,10 +81,7 @@ describe('app', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi ibot is a ferret

    '); - done(); - }) + .expect('

    tobi ibot is a ferret

    ', done); }) }) }) diff --git a/test/app.options.js b/test/app.options.js index 81c0a85e..836a3463 100644 --- a/test/app.options.js +++ b/test/app.options.js @@ -12,12 +12,8 @@ describe('OPTIONS', function(){ request(app) .options('/users') - .end(function(res){ - res.body.should.equal('GET,PUT'); - res.headers.should.have.property('content-type'); - res.headers.should.have.property('allow', 'GET,PUT'); - done(); - }); + .expect('GET,PUT') + .expect('Allow', 'GET,PUT', done); }) }) @@ -35,10 +31,7 @@ describe('app.options()', function(){ request(app) .options('/users') - .end(function(res){ - res.body.should.equal('GET'); - res.headers.should.have.property('allow', 'GET'); - done(); - }); + .expect('GET') + .expect('Allow', 'GET', done); }) }) \ No newline at end of file diff --git a/test/app.param.js b/test/app.param.js index 15799e6d..355b8be0 100644 --- a/test/app.param.js +++ b/test/app.param.js @@ -29,14 +29,11 @@ describe('app', function(){ request(app) .get('/user/tj') - .end(function(res){ - res.body.should.equal('tj'); + .end(function(err, res){ + res.text.should.equal('tj'); request(app) .get('/user/123') - .end(function(res){ - res.should.have.status(404); - done(); - }); + .expect(404, done); }); }) @@ -67,15 +64,12 @@ describe('app', function(){ request(app) .get('/user/123') - .end(function(res){ - res.body.should.equal('123'); + .end(function(err, res){ + res.text.should.equal('123'); request(app) .get('/post/123') - .end(function(res){ - res.body.should.equal('123'); - done(); - }) + .expect('123', done); }) }) }) @@ -99,10 +93,7 @@ describe('app', function(){ request(app) .get('/user/123') - .end(function(res){ - res.body.should.equal('123'); - done(); - }) + .expect('123', done); }) }) }) diff --git a/test/app.request.js b/test/app.request.js index cbcd459b..6d311128 100644 --- a/test/app.request.js +++ b/test/app.request.js @@ -17,10 +17,7 @@ describe('app', function(){ request(app) .get('/foo?name=tobi') - .end(function(res){ - res.body.should.equal('name=tobi'); - done(); - }); + .expect('name=tobi', done); }) }) }) diff --git a/test/app.response.js b/test/app.response.js index 2e5a62ec..84911e4c 100644 --- a/test/app.response.js +++ b/test/app.response.js @@ -17,10 +17,7 @@ describe('app', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('HEY'); - done(); - }); + .expect('HEY', done); }) it('should not be influenced by other app protos', function(done){ @@ -41,10 +38,7 @@ describe('app', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('HEY'); - done(); - }); + .expect('HEY', done); }) }) }) diff --git a/test/app.router.js b/test/app.router.js index 77103319..f48e58e0 100644 --- a/test/app.router.js +++ b/test/app.router.js @@ -8,6 +8,7 @@ describe('app.router', function(){ describe('methods supported', function(){ methods.forEach(function(method){ it('should include ' + method.toUpperCase(), function(done){ + if (method == 'delete') method = 'del'; var app = express(); var calls = []; @@ -267,7 +268,7 @@ describe('app.router', function(){ request(app) .get('/user/10') - .end(function(res){ + .end(function(err, res){ res.statusCode.should.equal(200); request(app) .get('/user/tj') diff --git a/test/regression.js b/test/regression.js index 4e9f4864..fdb2c5f5 100644 --- a/test/regression.js +++ b/test/regression.js @@ -13,10 +13,7 @@ describe('throw after .end()', function(){ request(app) .get('/') - .end(function(res){ - res.should.have.status(200); - res.body.should.equal('yay'); - done(); - }); + .expect('yay') + .expect(200, done); }) }) diff --git a/test/req.fresh.js b/test/req.fresh.js index e3d6db9d..e0079bb9 100644 --- a/test/req.fresh.js +++ b/test/req.fresh.js @@ -29,10 +29,7 @@ describe('req', function(){ request(app) .get('/') .set('If-None-Match', '12345') - .end(function(res){ - res.body.should.equal('false'); - done(); - }); + .expect('false', done); }) }) }) diff --git a/test/req.get.js b/test/req.get.js index 8846f0a0..6e114509 100644 --- a/test/req.get.js +++ b/test/req.get.js @@ -16,10 +16,7 @@ describe('req', function(){ request(app) .post('/') .set('Content-Type', 'application/json') - .end(function(res){ - res.body.should.equal('application/json'); - done(); - }); + .expect('application/json', done); }) it('should special-case Referer', function(done){ @@ -32,10 +29,7 @@ describe('req', function(){ request(app) .post('/') .set('Referrer', 'http://foobar.com') - .end(function(res){ - res.body.should.equal('http://foobar.com'); - done(); - }); + .expect('http://foobar.com', done); }) }) }) \ No newline at end of file diff --git a/test/req.param.js b/test/req.param.js index e496f9d9..e26257d4 100644 --- a/test/req.param.js +++ b/test/req.param.js @@ -13,10 +13,7 @@ describe('req', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('tj'); - done(); - }) + .expect('tj', done); }) }) @@ -30,10 +27,7 @@ describe('req', function(){ request(app) .get('/?name=tj') - .end(function(res){ - res.body.should.equal('tj'); - done(); - }) + .expect('tj', done); }) it('should check req.body', function(done){ @@ -47,12 +41,8 @@ describe('req', function(){ request(app) .post('/') - .set('Content-Type', 'application/json') - .write('{"name":"tj"}') - .end(function(res){ - res.body.should.equal('tj'); - done(); - }) + .send({ name: 'tj' }) + .expect('tj', done); }) it('should check req.params', function(done){ @@ -64,10 +54,7 @@ describe('req', function(){ request(app) .get('/user/tj') - .end(function(res){ - res.body.should.equal('undefinedtj'); - done(); - }) + .expect('undefinedtj', done); }) }) }) diff --git a/test/req.path.js b/test/req.path.js index f265f4c8..f12df375 100644 --- a/test/req.path.js +++ b/test/req.path.js @@ -13,10 +13,7 @@ describe('req', function(){ request(app) .get('/login?redirect=/post/1/comments') - .end(function(res){ - res.body.should.equal('/login'); - done(); - }) + .expect('/login', done); }) }) }) diff --git a/test/req.protocol.js b/test/req.protocol.js index adb6b93b..d25671fe 100644 --- a/test/req.protocol.js +++ b/test/req.protocol.js @@ -13,10 +13,7 @@ describe('req', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('http'); - done(); - }) + .expect('http', done); }) describe('when "trust proxy" is enabled', function(){ @@ -32,10 +29,7 @@ describe('req', function(){ request(app) .get('/') .set('X-Forwarded-Proto', 'https') - .end(function(res){ - res.body.should.equal('https'); - done(); - }) + .expect('https', done); }) it('should default to http', function(done){ @@ -49,10 +43,7 @@ describe('req', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('http'); - done(); - }) + .expect('http', done); }) }) @@ -67,10 +58,7 @@ describe('req', function(){ request(app) .get('/') .set('X-Forwarded-Proto', 'https') - .end(function(res){ - res.body.should.equal('http'); - done(); - }) + .expect('http', done); }) }) }) diff --git a/test/req.stale.js b/test/req.stale.js index b07fb376..22262836 100644 --- a/test/req.stale.js +++ b/test/req.stale.js @@ -29,10 +29,7 @@ describe('req', function(){ request(app) .get('/') .set('If-None-Match', '12345') - .end(function(res){ - res.body.should.equal('true'); - done(); - }); + .expect('true', done); }) }) }) diff --git a/test/req.subdomains.js b/test/req.subdomains.js index aecf99c2..0447575e 100644 --- a/test/req.subdomains.js +++ b/test/req.subdomains.js @@ -15,10 +15,7 @@ describe('req', function(){ request(app) .get('/') .set('Host', 'tobi.ferrets.example.com') - .end(function(res){ - res.body.should.equal('["ferrets","tobi"]'); - done(); - }) + .expect('["ferrets","tobi"]', done); }) }) @@ -33,10 +30,7 @@ describe('req', function(){ request(app) .get('/') .set('Host', 'example.com') - .end(function(res){ - res.body.should.equal('[]'); - done(); - }) + .expect('[]', done); }) }) }) diff --git a/test/res.attachment.js b/test/res.attachment.js index b9a8e34d..aea5eb16 100644 --- a/test/res.attachment.js +++ b/test/res.attachment.js @@ -13,10 +13,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-disposition', 'attachment'); - done(); - }) + .expect('Content-Disposition', 'attachment', done); }) }) @@ -31,10 +28,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-disposition', 'attachment; filename="image.png"'); - done(); - }) + .expect('Content-Disposition', 'attachment; filename="image.png"', done); }) it('should set the Content-Type', function(done){ @@ -47,10 +41,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-type', 'image/png'); - done(); - }) + .expect('Content-Type', 'image/png', done); }) }) }) diff --git a/test/res.charset.js b/test/res.charset.js index dc5c2783..79ec3dfb 100644 --- a/test/res.charset.js +++ b/test/res.charset.js @@ -15,10 +15,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('text/x-foo; charset=utf-8'); - done(); - }) + .expect("text/x-foo; charset=utf-8", done); }) it('should take precedence over res.send() defaults', function(done){ @@ -31,10 +28,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-type', 'text/html; charset=whoop'); - done(); - }) + .expect('Content-Type', 'text/html; charset=whoop', done); }) }) }) diff --git a/test/res.clearCookie.js b/test/res.clearCookie.js index c6217841..99c56c0c 100644 --- a/test/res.clearCookie.js +++ b/test/res.clearCookie.js @@ -13,9 +13,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = 'sid=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT'; - res.headers['set-cookie'].should.eql([val]); + res.header['set-cookie'].should.eql([val]); done(); }) }) @@ -31,9 +31,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = 'sid=; Path=/admin; Expires=Thu, 01 Jan 1970 00:00:00 GMT'; - res.headers['set-cookie'].should.eql([val]); + res.header['set-cookie'].should.eql([val]); done(); }) }) diff --git a/test/res.cookie.js b/test/res.cookie.js index 0a5dae9b..e49331a1 100644 --- a/test/res.cookie.js +++ b/test/res.cookie.js @@ -14,7 +14,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = ['user=j:{%22name%22:%22tobi%22}; Path=/']; res.headers['set-cookie'].should.eql(val); done(); @@ -32,7 +32,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = ['name=tobi; Path=/']; res.headers['set-cookie'].should.eql(val); done(); @@ -50,7 +50,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = ['name=tobi; Path=/', 'age=1; Path=/']; res.headers['set-cookie'].should.eql(val); done(); @@ -69,7 +69,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = ['name=tobi; Path=/; HttpOnly; Secure']; res.headers['set-cookie'].should.eql(val); done(); @@ -87,7 +87,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers['set-cookie'][0].should.not.include('Thu, 01 Jan 1970 00:00:01 GMT'); done(); }) @@ -106,7 +106,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = res.headers['set-cookie'][0]; val = cookie.parse(val.split('.')[0]); val.user.should.equal('j:{"name":"tobi"}'); @@ -127,7 +127,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ var val = ['name=tobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; Path=/']; res.headers['set-cookie'].should.eql(val); done(); diff --git a/test/res.download.js b/test/res.download.js index b1c77461..5144865b 100644 --- a/test/res.download.js +++ b/test/res.download.js @@ -14,10 +14,10 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.should.have.header('Content-Type', 'text/html; charset=UTF-8'); res.should.have.header('Content-Disposition', 'attachment; filename="user.html"'); - res.body.should.equal('

    {{user.name}}

    '); + res.text.should.equal('

    {{user.name}}

    '); done(); }); }) @@ -33,7 +33,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.should.have.header('Content-Type', 'text/html; charset=UTF-8'); res.should.have.header('Content-Disposition', 'attachment; filename="document"'); done(); @@ -52,7 +52,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.should.have.header('Content-Type', 'text/html; charset=UTF-8'); res.should.have.header('Content-Disposition', 'attachment; filename="user.html"'); }); @@ -70,7 +70,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.should.have.header('Content-Type', 'text/html; charset=UTF-8'); res.should.have.header('Content-Disposition', 'attachment; filename="document"'); }); diff --git a/test/res.format.js b/test/res.format.js index 0e1a3226..c077d048 100644 --- a/test/res.format.js +++ b/test/res.format.js @@ -93,21 +93,15 @@ function test(app) { request(app) .get('/') .set('Accept', 'text/html; q=.5, text/plain') - .end(function(res){ - res.headers['content-type'].should.equal('text/plain'); - res.body.should.equal('hey'); - done(); - }); + .expect('Content-Type', 'text/plain') + .expect('hey', done); }) it('should Vary: Accept', function(done){ request(app) .get('/') .set('Accept', 'text/html; q=.5, text/plain') - .end(function(res){ - res.headers.vary.should.equal('Accept'); - done(); - }); + .expect('Vary', 'Accept', done); }) describe('when Accept is not present', function(){ @@ -123,11 +117,8 @@ function test(app) { request(app) .get('/') .set('Accept', 'foo/bar') - .end(function(res){ - res.should.have.status(406); - res.body.should.equal('Supports: text/plain, text/html, application/json'); - done(); - }); + .expect('Supports: text/plain, text/html, application/json') + .expect(406, done) }) }) } diff --git a/test/res.json.js b/test/res.json.js index 04c59989..8ae2f1cc 100644 --- a/test/res.json.js +++ b/test/res.json.js @@ -15,9 +15,9 @@ describe('res', function(){ request(app) .get('/?callback=something') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); - res.body.should.equal('something({"count":1});'); + res.text.should.equal('something({"count":1});'); done(); }) }) @@ -31,9 +31,9 @@ describe('res', function(){ request(app) .get('/?callback=callbacks[123]') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); - res.body.should.equal('callbacks[123]({"count":1});'); + res.text.should.equal('callbacks[123]({"count":1});'); done(); }) }) @@ -49,9 +49,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); - res.body.should.equal('null'); + res.text.should.equal('null'); done(); }) }) @@ -67,9 +67,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); - res.body.should.equal('["foo","bar","baz"]'); + res.text.should.equal('["foo","bar","baz"]'); done(); }) }) @@ -85,9 +85,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); - res.body.should.equal('{"name":"tobi"}'); + res.text.should.equal('{"name":"tobi"}'); done(); }) }) @@ -109,8 +109,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('{"name":"tobi"}'); + .end(function(err, res){ + res.text.should.equal('{"name":"tobi"}'); done(); }); }) @@ -140,8 +140,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('{\n "name": "tobi",\n "age": 2\n}'); + .end(function(err, res){ + res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}'); done(); }); }) @@ -158,10 +158,10 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.statusCode.should.equal(201); res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); - res.body.should.equal('{"id":1}'); + res.text.should.equal('{"id":1}'); done(); }) }) diff --git a/test/res.locals.use.js b/test/res.locals.use.js index 60cf5f0e..0c143d53 100644 --- a/test/res.locals.use.js +++ b/test/res.locals.use.js @@ -40,9 +40,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ calls.should.eql(['render', 'one', 'two']); - res.body.should.equal('

    tobi holowaychuk is a ferret

    '); + res.text.should.equal('

    tobi holowaychuk is a ferret

    '); done(); }) }) @@ -68,8 +68,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi holowaychuk is a ferret

    '); + .end(function(err, res){ + res.text.should.equal('

    tobi holowaychuk is a ferret

    '); done(); }) }) @@ -95,8 +95,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi ibot is a ferret

    '); + .end(function(err, res){ + res.text.should.equal('

    tobi ibot is a ferret

    '); done(); }) }) diff --git a/test/res.redirect.js b/test/res.redirect.js index 7806c836..dfa617bd 100644 --- a/test/res.redirect.js +++ b/test/res.redirect.js @@ -17,7 +17,7 @@ describe('res', function(){ .get('/') .set('Host', 'example.com') .set('X-Forwarded-Proto', 'https') - .end(function(res){ + .end(function(err, res){ res.statusCode.should.equal(302); res.headers.should.have.property('location', 'https://example.com/login'); done(); @@ -33,7 +33,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.statusCode.should.equal(302); res.headers.should.have.property('location', 'http://google.com'); done(); @@ -51,7 +51,7 @@ describe('res', function(){ request(app) .get('/') .set('Host', 'example.com') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://example.com/login'); done(); }) @@ -69,7 +69,7 @@ describe('res', function(){ request(app) .get('/post/1') .set('Host', 'example.com') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://example.com/post/1/./edit'); done(); }) @@ -87,7 +87,7 @@ describe('res', function(){ request(app) .get('/post/1') .set('Host', 'example.com') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://example.com/post/1/../new'); done(); }) @@ -105,7 +105,7 @@ describe('res', function(){ request(app) .get('/') .set('Host', 'example.com') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://example.com/login'); done(); }) @@ -129,7 +129,7 @@ describe('res', function(){ request(app) .get('/blog/admin') .set('Host', 'example.com') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://example.com/blog/admin/login'); done(); }) @@ -150,7 +150,7 @@ describe('res', function(){ request(app) .get('/blog') .set('Host', 'example.com') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://example.com/blog/admin/login'); done(); }) @@ -171,7 +171,7 @@ describe('res', function(){ request(app) .get('/blog') .set('Host', 'example.com') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://example.com/admin/login'); done(); }) @@ -190,7 +190,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.statusCode.should.equal(303); res.headers.should.have.property('location', 'http://google.com'); done(); @@ -208,9 +208,9 @@ describe('res', function(){ request(app) .head('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://google.com'); - res.body.should.equal(''); + res.text.should.equal(''); done(); }) }) @@ -227,9 +227,9 @@ describe('res', function(){ request(app) .get('/') .set('Accept', 'text/html') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://google.com'); - res.body.should.equal('

    Moved Temporarily. Redirecting to http://google.com

    '); + res.text.should.equal('

    Moved Temporarily. Redirecting to http://google.com

    '); done(); }) }) @@ -246,10 +246,10 @@ describe('res', function(){ request(app) .get('/') .set('Accept', 'text/plain, */*') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('location', 'http://google.com'); res.headers.should.have.property('content-length', '51'); - res.body.should.equal('Moved Temporarily. Redirecting to http://google.com'); + res.text.should.equal('Moved Temporarily. Redirecting to http://google.com'); done(); }) }) @@ -266,12 +266,12 @@ describe('res', function(){ request(app) .get('/') .set('Accept', 'foo/bar') - .end(function(res){ + .end(function(err, res){ res.should.have.status(302); res.headers.should.have.property('location', 'http://google.com'); res.headers.should.not.have.property('content-type'); res.headers.should.have.property('content-length', '0'); - res.body.should.equal(''); + res.text.should.equal(''); done(); }) }) diff --git a/test/res.render.js b/test/res.render.js index d11140a2..3c016094 100644 --- a/test/res.render.js +++ b/test/res.render.js @@ -15,10 +15,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi

    '); - done(); - }); + .expect('

    tobi

    ', done); }) it('should support absolute paths with "view engine"', function(done){ @@ -33,10 +30,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi

    '); - done(); - }); + .expect('

    tobi

    ', done); }) it('should expose app.locals', function(done){ @@ -51,10 +45,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi

    '); - done(); - }); + .expect('

    tobi

    ', done); }) it('should support index.', function(done){ @@ -69,10 +60,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    blog post

    '); - done(); - }); + .expect('

    blog post

    ', done); }) describe('when an error occurs', function(){ @@ -91,10 +79,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.match(/user is not defined/); - done(); - }); + .expect(/user is not defined/, done); }) }) @@ -111,10 +96,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    This is an email

    '); - done(); - }); + .expect('

    This is an email

    ', done); }) }) }) @@ -133,10 +115,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi

    '); - done(); - }); + .expect('

    tobi

    ', done); }) it('should expose app.locals', function(done){ @@ -151,10 +130,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi

    '); - done(); - }); + .expect('

    tobi

    ', done); }) it('should expose res.locals', function(done){ @@ -169,10 +145,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    tobi

    '); - done(); - }); + .expect('

    tobi

    ', done); }) it('should give precedence to res.locals over app.locals', function(done){ @@ -188,10 +161,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    jane

    '); - done(); - }); + .expect('

    jane

    ', done); }) it('should give precedence to res.render() locals over res.locals', function(done){ @@ -207,10 +177,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    jane

    '); - done(); - }); + .expect('

    jane

    ', done); }) it('should give precedence to res.render() locals over app.locals', function(done){ @@ -226,10 +193,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    jane

    '); - done(); - }); + .expect('

    jane

    ', done); }) }) @@ -249,10 +213,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    loki

    '); - done(); - }); + .expect('

    loki

    ', done); }) }) @@ -272,10 +233,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    loki

    '); - done(); - }); + .expect('

    loki

    ', done); }) describe('when an error occurs', function(){ @@ -292,12 +250,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.match(/is not defined/); - done(); - }); + .expect(/is not defined/, done); }) - }) }) }) diff --git a/test/res.send.js b/test/res.send.js index d3118099..3f49a07f 100644 --- a/test/res.send.js +++ b/test/res.send.js @@ -13,10 +13,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal(''); - done(); - }) + .expect('', done); }) }) @@ -30,10 +27,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal(''); - done(); - }) + .expect('', done); }) }) @@ -47,11 +41,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('Created'); - res.statusCode.should.equal(201); - done(); - }) + .expect('Created') + .expect(201, done); }) }) @@ -65,11 +56,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('Created :)'); - res.statusCode.should.equal(201); - done(); - }) + .expect('Created :)') + .expect(201, done); }) }) @@ -83,11 +71,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('Bad!'); - res.statusCode.should.equal(400); - done(); - }) + .expect('Bad!') + .expect(400, done); }) }) @@ -101,9 +86,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'text/html; charset=utf-8'); - res.body.should.equal('

    hey

    '); + res.text.should.equal('

    hey

    '); res.statusCode.should.equal(200); done(); }) @@ -119,10 +104,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('etag', '-1498647312'); - done(); - }) + .expect('ETag', '-1498647312') + .end(done); }) it('should not override Content-Type', function(done){ @@ -134,12 +117,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-type', 'text/plain'); - res.body.should.equal('hey'); - res.statusCode.should.equal(200); - done(); - }) + .expect('Content-Type', 'text/plain') + .expect('hey') + .expect(200, done); }) }) @@ -153,9 +133,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'text/html; charset=utf-8'); - res.body.should.equal('

    hey

    '); + res.text.should.equal('

    hey

    '); res.statusCode.should.equal(200); done(); }) @@ -170,9 +150,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'text/plain'); - res.body.should.equal('hey'); + res.text.should.equal('hey'); res.statusCode.should.equal(200); done(); }) @@ -189,9 +169,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'application/octet-stream'); - res.body.should.equal('hello'); + res.text.should.equal('hello'); res.statusCode.should.equal(200); done(); }) @@ -207,10 +187,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('etag', '-1498647312'); - done(); - }) + .expect('ETag', '-1498647312') + .end(done); }) it('should not override Content-Type', function(done){ @@ -222,9 +200,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'text/plain'); - res.body.should.equal('hey'); + res.text.should.equal('hey'); res.statusCode.should.equal(200); done(); }) @@ -241,9 +219,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); - res.body.should.equal('{"name":"tobi"}'); + res.text.should.equal('{"name":"tobi"}'); done(); }) }) @@ -259,10 +237,7 @@ describe('res', function(){ request(app) .head('/') - .end(function(res){ - res.body.should.equal(''); - done(); - }) + .expect('', done); }) }) @@ -276,10 +251,10 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.not.have.property('content-type'); res.headers.should.not.have.property('content-length'); - res.body.should.equal(''); + res.text.should.equal(''); done(); }) }) @@ -295,10 +270,10 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.headers.should.not.have.property('content-type'); res.headers.should.not.have.property('content-length'); - res.body.should.equal(''); + res.text.should.equal(''); done(); }) }) @@ -344,10 +319,7 @@ describe('res', function(){ request(app) .get('/') .set('If-None-Match', 'asdf') - .end(function(res){ - res.should.have.status(500); - res.body.should.equal('hey'); - done(); - }); + .expect('hey') + .expect(500, done); }) }) diff --git a/test/res.sendfile.js b/test/res.sendfile.js index 6a1037fe..d04ecc5d 100644 --- a/test/res.sendfile.js +++ b/test/res.sendfile.js @@ -17,9 +17,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.should.have.status(200); - }); + .expect(200) + .end(function(){}); }) it('should utilize the same options as express.static()', function(done){ @@ -31,10 +30,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.should.have.header('Cache-Control', 'public, max-age=60'); - done(); - }); + .expect('Cache-Control', 'public, max-age=60') + .end(done); }) it('should invoke the callback on 404', function(done){ @@ -51,9 +48,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ calls.should.equal(1); - res.body.should.equal('Not Found'); + res.text.should.equal('Not Found'); res.statusCode.should.equal(200); done(); }); @@ -69,10 +66,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.should.have.header('content-type', 'text/plain'); - done(); - }); + .expect('Content-Type', 'text/plain') + .end(done); }) it('should invoke the callback on 403', function(done){ @@ -89,12 +84,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('Forbidden'); - res.statusCode.should.equal(200); - calls.should.equal(1); - done(); - }); + .expect('Forbidden') + .expect(200, done); }) }) @@ -109,8 +100,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    {{user.name}}

    '); + .end(function(err, res){ + res.text.should.equal('

    {{user.name}}

    '); res.headers.should.have.property('content-type', 'text/html; charset=UTF-8'); done(); }); @@ -127,8 +118,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    {{user.name}}

    '); + .end(function(err, res){ + res.text.should.equal('

    {{user.name}}

    '); res.headers.should.have.property('content-type', 'text/html; charset=UTF-8'); done(); }); @@ -143,8 +134,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('

    {{user.name}}

    '); + .end(function(err, res){ + res.text.should.equal('

    {{user.name}}

    '); res.headers.should.have.property('content-type', 'text/html; charset=UTF-8'); done(); }); @@ -159,10 +150,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.statusCode.should.equal(403); - done(); - }); + .expect(403, done); }) it('should allow ../ when "root" is set', function(done){ @@ -174,10 +162,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.statusCode.should.equal(200); - done(); - }); + .expect(200, done); }) it('should disallow requesting out of "root"', function(done){ @@ -189,10 +174,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.statusCode.should.equal(403); - done(); - }); + .expect(403, done); }) it('should next(404) when not found', function(done){ @@ -214,7 +196,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ + .end(function(err, res){ res.statusCode.should.equal(404); calls.should.equal(1); done(); @@ -223,17 +205,16 @@ describe('res', function(){ describe('with non-GET', function(){ it('should still serve', function(done){ - var app = express() - , calls = 0; + var app = express() + , calls = 0; - app.use(function(req, res){ - res.sendfile(__dirname + '/fixtures/name.txt'); - }); + app.use(function(req, res){ + res.sendfile(__dirname + '/fixtures/name.txt'); + }); - - request(app) - .get('/') - .expect('tobi', done); + request(app) + .get('/') + .expect('tobi', done); }) }) }) diff --git a/test/res.set.js b/test/res.set.js index f40dbe08..ce001816 100644 --- a/test/res.set.js +++ b/test/res.set.js @@ -14,10 +14,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-type', 'text/x-foo'); - done(); - }) + .expect('Content-Type', 'text/x-foo') + .end(done); }) it('should coerce to a string', function(){ @@ -40,11 +38,9 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('x-foo', 'bar'); - res.headers.should.have.property('x-bar', 'baz'); - done(); - }) + .expect('X-Foo', 'bar') + .expect('X-Bar', 'baz') + .end(done); }) it('should coerce to a string', function(){ diff --git a/test/res.status.js b/test/res.status.js index a7d19d4c..0de25eb7 100644 --- a/test/res.status.js +++ b/test/res.status.js @@ -13,11 +13,8 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.body.should.equal('Created'); - res.statusCode.should.equal(201); - done(); - }) + .expect('Created') + .expect(201, done); }) }) }) diff --git a/test/res.type.js b/test/res.type.js index 90989788..09653e39 100644 --- a/test/res.type.js +++ b/test/res.type.js @@ -13,10 +13,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-type', 'application/javascript'); - done(); - }) + .expect('Content-Type', 'application/javascript', done); }) it('should default to application/octet-stream', function(done){ @@ -28,10 +25,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-type', 'application/octet-stream'); - done(); - }) + .expect('Content-Type', 'application/octet-stream', done); }) it('should set the Content-Type with type/subtype', function(done){ @@ -44,10 +38,7 @@ describe('res', function(){ request(app) .get('/') - .end(function(res){ - res.headers.should.have.property('content-type', 'application/vnd.amazon.ebook'); - done(); - }) + .expect('Content-Type', 'application/vnd.amazon.ebook', done); }) }) }) diff --git a/test/support/http.js b/test/support/http.js index 64df2ea0..382e63ce 100644 --- a/test/support/http.js +++ b/test/support/http.js @@ -1,105 +1,2 @@ -/** - * Module dependencies. - */ - -var EventEmitter = require('events').EventEmitter - , methods = require('methods') - , http = require('http'); - -module.exports = request; - -function request(app) { - return new Request(app); -} - -function Request(app) { - var self = this; - this.data = []; - this.header = {}; - this.app = app; - if (!this.server) { - this.server = http.Server(app); - this.server.listen(0, function(){ - self.addr = self.server.address(); - self.listening = true; - }); - } -} - -/** - * Inherit from `EventEmitter.prototype`. - */ - -Request.prototype.__proto__ = EventEmitter.prototype; - -methods.forEach(function(method){ - Request.prototype[method] = function(path){ - return this.request(method, path); - }; -}); - -Request.prototype.set = function(field, val){ - this.header[field] = val; - return this; -}; - -Request.prototype.write = function(data){ - this.data.push(data); - return this; -}; - -Request.prototype.request = function(method, path){ - this.method = method; - this.path = path; - return this; -}; - -Request.prototype.expect = function(body, fn){ - this.end(function(res){ - if ('number' == typeof body) { - res.statusCode.should.equal(body); - } else if (body instanceof RegExp) { - res.body.should.match(body); - } else { - res.body.should.equal(body); - } - fn(); - }); -}; - -Request.prototype.end = function(fn){ - var self = this; - - if (this.listening) { - var req = http.request({ - method: this.method - , port: this.addr.port - , host: this.addr.address - , path: this.path - , headers: this.header - }); - - this.data.forEach(function(chunk){ - req.write(chunk); - }); - - req.on('response', function(res){ - var buf = ''; - res.setEncoding('utf8'); - res.on('data', function(chunk){ buf += chunk }); - res.on('end', function(){ - res.body = buf; - fn(res); - }); - }); - - req.end(); - } else { - this.server.on('listening', function(){ - self.end(fn); - }); - } - - return this; -}; +module.exports = require('supertest'); \ No newline at end of file