From 310f84d68f96efbbe3d0c5cd80a9024794505739 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A0=9A=E7=84=B6?= <yanran.wwj@alipay.com>
Date: Mon, 5 Feb 2018 21:04:27 +0800
Subject: [PATCH] fix the start issue when webinterface is disabled, andd add
 test cases

---
 proxy.js                       | 19 ++++++++++---------
 test/spec_rule/no_rule_spec.js |  6 ++++--
 test/util/ProxyServerUtil.js   |  8 +++++++-
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/proxy.js b/proxy.js
index 706dfdd..a80ca2b 100644
--- a/proxy.js
+++ b/proxy.js
@@ -329,16 +329,17 @@ class ProxyServer extends ProxyCore {
     // start web interface if neeeded
     if (this.proxyWebinterfaceConfig && this.proxyWebinterfaceConfig.enable) {
       this.webServerInstance = new WebInterface(this.proxyWebinterfaceConfig, this.recorder);
-    }
-
-    // start web server
-    this.webServerInstance.start().then(() => {
-      // start proxy core
+      // start web server
+      this.webServerInstance.start().then(() => {
+        // start proxy core
+        super.start();
+      })
+      .catch((e) => {
+        this.emit('error', e);
+      });
+    } else {
       super.start();
-    })
-    .catch((e) => {
-      this.emit('error', e);
-    });
+    }
   }
 
   close() {
diff --git a/test/spec_rule/no_rule_spec.js b/test/spec_rule/no_rule_spec.js
index 8157076..32373ef 100644
--- a/test/spec_rule/no_rule_spec.js
+++ b/test/spec_rule/no_rule_spec.js
@@ -31,9 +31,11 @@ const ProxyServerUtil = require('../util/ProxyServerUtil.js');
 
 testRequest('http');
 testRequest('https');
+testRequest('http', false);
+testRequest('https', false);
 
 // Test suites for http and https request
-function testRequest(protocol = 'http') {
+function testRequest(protocol = 'http', needWeb = true) {
   function constructUrl(urlPath) {
     return generateUrl(protocol, urlPath);
   }
@@ -47,7 +49,7 @@ function testRequest(protocol = 'http') {
       printLog('Start server for no_rule_spec');
 
       serverInstance = new Server();
-      proxyServer = ProxyServerUtil.defaultProxyServer();
+      proxyServer = ProxyServerUtil.defaultProxyServer(needWeb);
       setTimeout(() => {
         done();
       }, 2000);
diff --git a/test/util/ProxyServerUtil.js b/test/util/ProxyServerUtil.js
index f892c6a..68a8a86 100644
--- a/test/util/ProxyServerUtil.js
+++ b/test/util/ProxyServerUtil.js
@@ -23,10 +23,16 @@ const DEFAULT_OPTIONS = {
 *
 * @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 options = util.merge({}, DEFAULT_OPTIONS);
+  util.merge(options, {
+    webInterface: {
+      enable: webinterfaceEnable,
+      webPort: 8002
+    }
+  })
   const instance = new AnyProxy.ProxyServer(options);
   instance.on('error', e => {
     console.log('server instance error', e);