Skip to content
Myr

req.accepts.js

This file

Path

test/req.accepts.js

125 lines · 2.9 KiB · JavaScript

Last changed by

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

History of this file

Take it

The walk stopped after 200 commits: lines older than 2a0c35a point at it.

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 1why, one step back
1'use strict'

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 2–125why, one step back
2
3var express = require('../')4 , request = require('supertest');56describe('req', function(){7 describe('.accepts(type)', function(){8 it('should return true when Accept is not present', function(done){9 var app = express();1011 app.use(function(req, res, next){12 res.end(req.accepts('json') ? 'yes' : 'no');13 });1415 request(app)16 .get('/')17 .expect('yes', done);18 })1920 it('should return true when present', function(done){21 var app = express();2223 app.use(function(req, res, next){24 res.end(req.accepts('json') ? 'yes' : 'no');25 });2627 request(app)28 .get('/')29 .set('Accept', 'application/json')30 .expect('yes', done);31 })3233 it('should return false otherwise', function(done){34 var app = express();3536 app.use(function(req, res, next){37 res.end(req.accepts('json') ? 'yes' : 'no');38 });3940 request(app)41 .get('/')42 .set('Accept', 'text/html')43 .expect('no', done);44 })45 })4647 it('should accept an argument list of type names', function(done){48 var app = express();4950 app.use(function(req, res, next){51 res.end(req.accepts('json', 'html'));52 });5354 request(app)55 .get('/')56 .set('Accept', 'application/json')57 .expect('json', done);58 })5960 describe('.accepts(types)', function(){61 it('should return the first when Accept is not present', function(done){62 var app = express();6364 app.use(function(req, res, next){65 res.end(req.accepts(['json', 'html']));66 });6768 request(app)69 .get('/')70 .expect('json', done);71 })7273 it('should return the first acceptable type', function(done){74 var app = express();7576 app.use(function(req, res, next){77 res.end(req.accepts(['json', 'html']));78 });7980 request(app)81 .get('/')82 .set('Accept', 'text/html')83 .expect('html', done);84 })8586 it('should return false when no match is made', function(done){87 var app = express();8889 app.use(function(req, res, next){90 res.end(req.accepts(['text/html', 'application/json']) ? 'yup' : 'nope');91 });9293 request(app)94 .get('/')95 .set('Accept', 'foo/bar, bar/baz')96 .expect('nope', done);97 })9899 it('should take quality into account', function(done){100 var app = express();101102 app.use(function(req, res, next){103 res.end(req.accepts(['text/html', 'application/json']));104 });105106 request(app)107 .get('/')108 .set('Accept', '*/html; q=.5, application/json')109 .expect('application/json', done);110 })111112 it('should return the first acceptable type with canonical mime types', function(done){113 var app = express();114115 app.use(function(req, res, next){116 res.end(req.accepts(['application/json', 'text/html']));117 });118119 request(app)120 .get('/')121 .set('Accept', '*/html')122 .expect('text/html', done);123 })124 })125})