File tree 2 files changed +9
-7
lines changed
2 files changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -11,15 +11,17 @@ describe('ctx.origin', () => {
11
11
const req = {
12
12
url : '/users/1?next=/dashboard' ,
13
13
headers : {
14
- host : 'localhost'
14
+ host : 'localhost' ,
15
+ origin : 'http://example.com'
15
16
} ,
16
17
socket,
17
18
__proto__ : Stream . Readable . prototype
18
19
}
19
20
const ctx = context ( req )
20
- assert . strictEqual ( ctx . origin , 'http://localhost' )
21
+ assert . strictEqual ( ctx . origin , 'http://example.com' )
22
+
21
23
// change it also work
22
24
ctx . url = '/foo/users/1?next=/dashboard'
23
- assert . strictEqual ( ctx . origin , 'http://localhost ' )
25
+ assert . strictEqual ( ctx . origin , 'http://example.com ' )
24
26
} )
25
27
} )
Original file line number Diff line number Diff line change @@ -89,14 +89,14 @@ module.exports = {
89
89
} ,
90
90
91
91
/**
92
- * Get origin of URL .
92
+ * Get the origin header .
93
93
*
94
94
* @return {String }
95
95
* @api public
96
96
*/
97
97
98
98
get origin ( ) {
99
- return ` ${ this . protocol } :// ${ this . host } `
99
+ return this . req . headers . origin || null
100
100
} ,
101
101
102
102
/**
@@ -109,7 +109,7 @@ module.exports = {
109
109
get href ( ) {
110
110
// support: `GET http://example.com/foo`
111
111
if ( / ^ h t t p s ? : \/ \/ / i. test ( this . originalUrl ) ) return this . originalUrl
112
- return this . origin + this . originalUrl
112
+ return this . protocol + '://' + this . host + this . originalUrl
113
113
} ,
114
114
115
115
/**
@@ -288,7 +288,7 @@ module.exports = {
288
288
if ( ! this . memoizedURL ) {
289
289
const originalUrl = this . originalUrl || '' // avoid undefined in template string
290
290
try {
291
- this . memoizedURL = new URL ( `${ this . origin } ${ originalUrl } ` )
291
+ this . memoizedURL = new URL ( `${ this . protocol } :// ${ this . host } ${ originalUrl } ` )
292
292
} catch ( err ) {
293
293
this . memoizedURL = Object . create ( null )
294
294
}
You can’t perform that action at this time.
0 commit comments