Merge pull request #329 from alibaba/webinterfac-fix

fix the start issue when webinterface is disabled, andd add test cases
This commit is contained in:
Otto Mao 2018-02-06 00:05:58 +08:00 committed by GitHub
commit a5027340dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 12 deletions

View File

@ -329,8 +329,6 @@ class ProxyServer extends ProxyCore {
// start web interface if neeeded // start web interface if neeeded
if (this.proxyWebinterfaceConfig && this.proxyWebinterfaceConfig.enable) { if (this.proxyWebinterfaceConfig && this.proxyWebinterfaceConfig.enable) {
this.webServerInstance = new WebInterface(this.proxyWebinterfaceConfig, this.recorder); this.webServerInstance = new WebInterface(this.proxyWebinterfaceConfig, this.recorder);
}
// start web server // start web server
this.webServerInstance.start().then(() => { this.webServerInstance.start().then(() => {
// start proxy core // start proxy core
@ -339,6 +337,9 @@ class ProxyServer extends ProxyCore {
.catch((e) => { .catch((e) => {
this.emit('error', e); this.emit('error', e);
}); });
} else {
super.start();
}
} }
close() { close() {

View File

@ -31,9 +31,11 @@ const ProxyServerUtil = require('../util/ProxyServerUtil.js');
testRequest('http'); testRequest('http');
testRequest('https'); testRequest('https');
testRequest('http', false);
testRequest('https', false);
// Test suites for http and https request // Test suites for http and https request
function testRequest(protocol = 'http') { function testRequest(protocol = 'http', needWeb = true) {
function constructUrl(urlPath) { function constructUrl(urlPath) {
return generateUrl(protocol, urlPath); return generateUrl(protocol, urlPath);
} }
@ -47,7 +49,7 @@ function testRequest(protocol = 'http') {
printLog('Start server for no_rule_spec'); printLog('Start server for no_rule_spec');
serverInstance = new Server(); serverInstance = new Server();
proxyServer = ProxyServerUtil.defaultProxyServer(); proxyServer = ProxyServerUtil.defaultProxyServer(needWeb);
setTimeout(() => { setTimeout(() => {
done(); done();
}, 2000); }, 2000);

View File

@ -23,10 +23,16 @@ const DEFAULT_OPTIONS = {
* *
* @return An instance of proxy, could be closed by calling `instance.close()` * @return An instance of proxy, could be closed by calling `instance.close()`
*/ */
function defaultProxyServer() { function defaultProxyServer(webinterfaceEnable = true) {
const AnyProxy = util.freshRequire('../proxy.js'); const AnyProxy = util.freshRequire('../proxy.js');
const options = util.merge({}, DEFAULT_OPTIONS); const options = util.merge({}, DEFAULT_OPTIONS);
util.merge(options, {
webInterface: {
enable: webinterfaceEnable,
webPort: 8002
}
})
const instance = new AnyProxy.ProxyServer(options); const instance = new AnyProxy.ProxyServer(options);
instance.on('error', e => { instance.on('error', e => {
console.log('server instance error', e); console.log('server instance error', e);