Lazily initialize request.accept
and delegate context.accept
(#1209)
This commit is contained in:
parent
8f047ddb84
commit
02feadc4db
4 changed files with 50 additions and 2 deletions
|
@ -15,7 +15,6 @@ const context = require('./context');
|
||||||
const request = require('./request');
|
const request = require('./request');
|
||||||
const statuses = require('statuses');
|
const statuses = require('statuses');
|
||||||
const Cookies = require('cookies');
|
const Cookies = require('cookies');
|
||||||
const accepts = require('accepts');
|
|
||||||
const Emitter = require('events');
|
const Emitter = require('events');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const Stream = require('stream');
|
const Stream = require('stream');
|
||||||
|
@ -175,7 +174,6 @@ module.exports = class Application extends Emitter {
|
||||||
secure: request.secure
|
secure: request.secure
|
||||||
});
|
});
|
||||||
request.ip = request.ips[0] || req.socket.remoteAddress || '';
|
request.ip = request.ips[0] || req.socket.remoteAddress || '';
|
||||||
context.accept = request.accept = accepts(req);
|
|
||||||
context.state = {};
|
context.state = {};
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
|
@ -204,6 +204,7 @@ delegate(proto, 'request')
|
||||||
.access('query')
|
.access('query')
|
||||||
.access('path')
|
.access('path')
|
||||||
.access('url')
|
.access('url')
|
||||||
|
.access('accept')
|
||||||
.getter('origin')
|
.getter('origin')
|
||||||
.getter('href')
|
.getter('href')
|
||||||
.getter('subdomains')
|
.getter('subdomains')
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
const URL = require('url').URL;
|
const URL = require('url').URL;
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
|
const accepts = require('accepts');
|
||||||
const contentType = require('content-type');
|
const contentType = require('content-type');
|
||||||
const stringify = require('url').format;
|
const stringify = require('url').format;
|
||||||
const parse = require('parseurl');
|
const parse = require('parseurl');
|
||||||
|
@ -463,6 +464,27 @@ module.exports = {
|
||||||
.slice(offset);
|
.slice(offset);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get accept object.
|
||||||
|
* Lazily memoized.
|
||||||
|
*
|
||||||
|
* @return {Object}
|
||||||
|
* @api private
|
||||||
|
*/
|
||||||
|
get accept() {
|
||||||
|
return this._accept || (this._accept = accepts(this.req));
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set accept object.
|
||||||
|
*
|
||||||
|
* @param {Object}
|
||||||
|
* @api private
|
||||||
|
*/
|
||||||
|
set accept(obj) {
|
||||||
|
return this._accept = obj;
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the given `type(s)` is acceptable, returning
|
* Check if the given `type(s)` is acceptable, returning
|
||||||
* the best match when true, otherwise `false`, in which
|
* the best match when true, otherwise `false`, in which
|
||||||
|
|
27
test/request/accept.js
Normal file
27
test/request/accept.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Accept = require('accepts');
|
||||||
|
const assert = require('assert');
|
||||||
|
const context = require('../helpers/context');
|
||||||
|
|
||||||
|
describe('ctx.accept', () => {
|
||||||
|
it('should return an Accept instance', () => {
|
||||||
|
const ctx = context();
|
||||||
|
ctx.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
|
||||||
|
assert(ctx.accept instanceof Accept);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ctx.accept=', () => {
|
||||||
|
it('should replace the accept object', () => {
|
||||||
|
const ctx = context();
|
||||||
|
ctx.req.headers.accept = 'text/plain';
|
||||||
|
assert.deepEqual(ctx.accepts(), ['text/plain']);
|
||||||
|
|
||||||
|
const request = context.request();
|
||||||
|
request.req.headers.accept = 'application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain';
|
||||||
|
ctx.accept = Accept(request.req);
|
||||||
|
assert.deepEqual(ctx.accepts(), ['text/html', 'text/plain', 'image/jpeg', 'application/*']);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue