-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
href.js
50 lines (47 loc) · 1.34 KB
/
href.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict'
const assert = require('assert')
const Stream = require('stream')
const http = require('http')
const Koa = require('../../')
const context = require('../../test-helpers/context')
describe('ctx.href', () => {
it('should return the full request url', () => {
const socket = new Stream.Duplex()
const req = {
url: '/users/1?next=/dashboard',
headers: {
host: 'localhost'
},
socket,
__proto__: Stream.Readable.prototype
}
const ctx = context(req)
assert.strictEqual(ctx.href, 'http:https://localhost/users/1?next=/dashboard')
// change it also work
ctx.url = '/foo/users/1?next=/dashboard'
assert.strictEqual(ctx.href, 'http:https://localhost/users/1?next=/dashboard')
})
it('should work with `GET http:https://example.com/foo`', done => {
const app = new Koa()
app.use(ctx => {
ctx.body = ctx.href
})
app.listen(function () {
const address = this.address()
http.get({
host: 'localhost',
path: 'http:https://example.com/foo',
port: address.port
}, res => {
assert.strictEqual(res.statusCode, 200)
let buf = ''
res.setEncoding('utf8')
res.on('data', s => { buf += s })
res.on('end', () => {
assert.strictEqual(buf, 'http:https://example.com/foo')
done()
})
})
})
})
})