fixed issue with rewrite 'from' urls containing ports
This commit is contained in:
@ -14,7 +14,7 @@ const pathToRegexp = require('path-to-regexp')
|
|||||||
module.exports = localWebServer
|
module.exports = localWebServer
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a Koa application you can launch or mix into an existing app.
|
* Returns a Koa application you can launch or mix into an existing app.
|
||||||
*
|
*
|
||||||
* @param [options] {object} - options
|
* @param [options] {object} - options
|
||||||
* @param [options.static] {object} - koa-static config
|
* @param [options.static] {object} - koa-static config
|
||||||
@ -190,7 +190,7 @@ function proxyRequest (route, app) {
|
|||||||
|
|
||||||
/* test no keys remain in the new path */
|
/* test no keys remain in the new path */
|
||||||
keys.length = 0
|
keys.length = 0
|
||||||
pathToRegexp(route.new, keys)
|
pathToRegexp(url.parse(route.new).path, keys)
|
||||||
if (keys.length) {
|
if (keys.length) {
|
||||||
this.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
|
this.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
|
||||||
return next()
|
return next()
|
||||||
|
27
test/test.js
27
test/test.js
@ -9,7 +9,7 @@ function launchServer (app, options) {
|
|||||||
options = options || {}
|
options = options || {}
|
||||||
const path = `http://localhost:8100${options.path || '/'}`
|
const path = `http://localhost:8100${options.path || '/'}`
|
||||||
const server = http.createServer(app.callback())
|
const server = http.createServer(app.callback())
|
||||||
return server.listen(8100, () => {
|
return server.listen(options.port || 8100, () => {
|
||||||
const req = request(path, options.reqOptions)
|
const req = request(path, options.reqOptions)
|
||||||
if (options.onSuccess) req.then(options.onSuccess)
|
if (options.onSuccess) req.then(options.onSuccess)
|
||||||
if (!options.leaveOpen) req.then(() => server.close())
|
if (!options.leaveOpen) req.then(() => server.close())
|
||||||
@ -164,3 +164,28 @@ test('rewrite: proxy', function(t){
|
|||||||
t.ok(/db_name/.test(response.data))
|
t.ok(/db_name/.test(response.data))
|
||||||
}})
|
}})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('rewrite: proxy with port', function(t){
|
||||||
|
t.plan(2)
|
||||||
|
const one = localWebServer({
|
||||||
|
log: { format: 'none' },
|
||||||
|
static: { root: __dirname + '/fixture/one' }
|
||||||
|
})
|
||||||
|
const two = localWebServer({
|
||||||
|
log: { format: 'none' },
|
||||||
|
static: { root: __dirname + '/fixture/spa' },
|
||||||
|
rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1'} ]
|
||||||
|
})
|
||||||
|
const server1 = http.createServer(one.callback())
|
||||||
|
const server2 = http.createServer(two.callback())
|
||||||
|
server1.listen(9000, () => {
|
||||||
|
server2.listen(8100, () => {
|
||||||
|
request('http://localhost:8100/test/file.txt').then(response => {
|
||||||
|
t.strictEqual(response.res.statusCode, 200)
|
||||||
|
t.ok(/one/.test(response.data))
|
||||||
|
server1.close()
|
||||||
|
server2.close()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Reference in New Issue
Block a user