mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-17 23:45:29 -05:00
add test for parsing listen address
This commit is contained in:
parent
89353f9312
commit
1a9596737e
3 changed files with 61 additions and 22 deletions
28
lib/cli.js
28
lib/cli.js
|
@ -27,6 +27,7 @@ var YAML = require('js-yaml')
|
|||
var Path = require('path')
|
||||
var URL = require('url')
|
||||
var server = require('./index')
|
||||
var Utils = require('./utils')
|
||||
var pkg_file = '../package.yaml'
|
||||
var pkg = YAML.safeLoad(fs.readFileSync(__dirname+'/'+ pkg_file, 'utf8'))
|
||||
|
||||
|
@ -76,34 +77,17 @@ function get_listen_addresses() {
|
|||
}
|
||||
|
||||
addresses = addresses.map(function(addr) {
|
||||
var m
|
||||
addr = Utils.parse_address(addr)
|
||||
|
||||
//
|
||||
// Allow:
|
||||
//
|
||||
// - https:localhost:1234 - protocol + host + port
|
||||
// - localhost:1234 - host + port
|
||||
// - 1234 - port
|
||||
// - http::1234 - protocol + port
|
||||
// - https://localhost:443/ - full url + https
|
||||
// - http://[::1]:443/ - ipv6
|
||||
//
|
||||
// protocol : // ( host )|( ipv6 ): port /
|
||||
var m = /^((https?):(\/\/)?)?((([^\/:]+)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr)
|
||||
|
||||
if (!m) {
|
||||
if (!addr) {
|
||||
logger.logger.warn({ addr: addr },
|
||||
'invalid address - @{addr}, we expect a port (e.g. "4873"),'
|
||||
+ ' host:port (e.g. "localhost:4873") or full url'
|
||||
+ ' (e.g. "http://localhost:4873/")')
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
proto: m[2] || 'http',
|
||||
host: m[6] || m[7] || 'localhost',
|
||||
port: m[8] || '4873',
|
||||
}
|
||||
return addr
|
||||
|
||||
}).filter(Boolean)
|
||||
|
||||
return addresses
|
||||
|
@ -177,7 +161,7 @@ function afterConfigLoad() {
|
|||
|
||||
// undocumented stuff for tests
|
||||
if (typeof(process.send) === 'function') {
|
||||
process.send({ sinopia_started: hostport })
|
||||
process.send({ sinopia_started: true })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
23
lib/utils.js
23
lib/utils.js
|
@ -122,6 +122,29 @@ module.exports.get_version = function(object, version) {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports.parse_address = function parse_address(addr) {
|
||||
//
|
||||
// Allow:
|
||||
//
|
||||
// - https:localhost:1234 - protocol + host + port
|
||||
// - localhost:1234 - host + port
|
||||
// - 1234 - port
|
||||
// - http::1234 - protocol + port
|
||||
// - https://localhost:443/ - full url + https
|
||||
// - http://[::1]:443/ - ipv6
|
||||
//
|
||||
// protocol : // ( host )|( ipv6 ): port /
|
||||
var m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr)
|
||||
|
||||
if (!m) return null
|
||||
|
||||
return {
|
||||
proto: m[2] || 'http',
|
||||
host: m[6] || m[7] || 'localhost',
|
||||
port: m[8] || '4873',
|
||||
}
|
||||
}
|
||||
|
||||
// function filters out bad semver versions and sorts the array
|
||||
module.exports.semver_sort = function semver_sort(array) {
|
||||
return array
|
||||
|
|
32
test/unit/listen_addr.js
Normal file
32
test/unit/listen_addr.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
var assert = require('assert')
|
||||
var parse = require('../../lib/utils').parse_address
|
||||
|
||||
describe('Parse address', function() {
|
||||
function addTest(what, proto, host, port) {
|
||||
it(what, function() {
|
||||
if (proto === null) {
|
||||
assert.strictEqual(parse(what), null)
|
||||
} else {
|
||||
assert.deepEqual(parse(what), {
|
||||
proto: proto,
|
||||
host: host,
|
||||
port: port,
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
addTest('4873', 'http', 'localhost', '4873')
|
||||
addTest(':4873', 'http', 'localhost', '4873')
|
||||
addTest('blah:4873', 'http', 'blah', '4873')
|
||||
addTest('http://:4873', 'http', 'localhost', '4873')
|
||||
addTest('https::4873', 'https', 'localhost', '4873')
|
||||
addTest('https:blah:4873', 'https', 'blah', '4873')
|
||||
addTest('https://blah:4873/', 'https', 'blah', '4873')
|
||||
addTest('[::1]:4873', 'http', '::1', '4873')
|
||||
addTest('https:[::1]:4873', 'https', '::1', '4873')
|
||||
|
||||
addTest('blah', null)
|
||||
addTest('blah://4873', null)
|
||||
addTest('https://blah:4873///', null)
|
||||
})
|
Loading…
Add table
Reference in a new issue