implement ctx.origin
This commit is contained in:
parent
520163fe57
commit
85860587cc
3 changed files with 37 additions and 1 deletions
|
@ -176,6 +176,7 @@ delegate(proto, 'request')
|
|||
.access('query')
|
||||
.access('path')
|
||||
.access('url')
|
||||
.getter('origin')
|
||||
.getter('href')
|
||||
.getter('subdomains')
|
||||
.getter('protocol')
|
||||
|
|
|
@ -59,6 +59,17 @@ module.exports = {
|
|||
this.req.url = val;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get origin of URL.
|
||||
*
|
||||
* @return {String}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
get origin() {
|
||||
return this.protocol + '://' + this.host;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get full request URL.
|
||||
*
|
||||
|
@ -71,7 +82,7 @@ module.exports = {
|
|||
if (/^https?:\/\//i.test(this.originalUrl)) {
|
||||
return this.originalUrl;
|
||||
}
|
||||
return this.protocol + '://' + this.host + this.originalUrl;
|
||||
return this.origin + this.originalUrl;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
24
test/request/origin.js
Normal file
24
test/request/origin.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
|
||||
var Stream = require('stream');
|
||||
var http = require('http');
|
||||
var koa = require('../../');
|
||||
var context = require('../context');
|
||||
|
||||
describe('ctx.origin', function(){
|
||||
it('should return the origin of url', function(){
|
||||
var socket = new Stream.Duplex();
|
||||
var req = {
|
||||
url: '/users/1?next=/dashboard',
|
||||
headers: {
|
||||
host: 'localhost'
|
||||
},
|
||||
socket: socket,
|
||||
__proto__: Stream.Readable.prototype
|
||||
};
|
||||
var ctx = context(req);
|
||||
ctx.origin.should.equal('http://localhost');
|
||||
// change it also work
|
||||
ctx.url = '/foo/users/1?next=/dashboard';
|
||||
ctx.origin.should.equal('http://localhost');
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue