Repository: aliyun/fun (js)

Issue:
fun config.yml 中的 endpoint https 没有生效

Pull request:
sunfeiyu/fix https in config.yml do not work 
fix #225 

Patch:
diff --git a/lib/deploy/deploy-support.js b/lib/deploy/deploy-support.js
index 9dfeec6..87a3074 100644
--- a/lib/deploy/deploy-support.js
+++ b/lib/deploy/deploy-support.js
@@ -46,6 +46,7 @@ const getFcClient = async () => {
     accessKeySecret: profile.accessKeySecret,
     region: profile.defaultRegion,
     timeout: profile.timeout * 1000,
+    secure: profile.protocol !== 'http',
     headers: {
       'user-agent': `${pkg.name}/v${pkg.version} ( Node.js ${process.version}; OS ${process.platform} ${process.arch}; language ${locale}; mid ${mid})`
     }
diff --git a/lib/profile.js b/lib/profile.js
index 88a402e..95e062b 100644
--- a/lib/profile.js
+++ b/lib/profile.js
@@ -10,6 +10,8 @@ const debug = require('debug')('fun:profile');
 
 const { red } = require('colors');
 
+const _ = require('lodash');
+
 const dotenv = require('dotenv').config();
 
 const readFile = util.promisify(fs.readFile);
@@ -38,6 +40,11 @@ function extractRegion(endpoint) {
   return extract(/^https?:\/\/[^.]+\.([^.]+)\..+$/, endpoint);
 }
 
+function extractProtocol(endpoint) {
+  const array = _.split(endpoint, ':', 1);
+  return array.length !== 0 ? array[0] : '';
+}
+
 
 async function getProfileFromFile() {
   const profPath = path.join(os.homedir(), '.fcli/config.yaml');
@@ -55,6 +62,8 @@ async function getProfileFromFile() {
   if (profYml.endpoint) {
     profile.accountId = extractAccountId(profYml.endpoint);
     profile.defaultRegion = extractRegion(profYml.endpoint);
+    profile.protocol = extractProtocol(profYml.endpoint);
+    
   }
 
   if (profYml.access_key_id) {


Test patch:
diff --git a/test/profile.test.js b/test/profile.test.js
index deb7a87..22955e7 100644
--- a/test/profile.test.js
+++ b/test/profile.test.js
@@ -51,7 +51,7 @@ describe('without local ~/.fcli/config.yaml', () => {
   });
 });
 
-describe('with local ~/.fcli/config.yaml', () => {
+describe('with local ~/.fcli/config.yaml for endpoint https', () => {
   let restoreProcess;
 
   beforeEach(async () => {
@@ -96,6 +96,7 @@ describe('with local ~/.fcli/config.yaml', () => {
     expect(profile.defaultRegion).to.be(process.env.DEFAULT_REGION);
     expect(profile.timeout).to.be(process.env.TIMEOUT);
     expect(profile.retries).to.be(process.env.RETRIES);
+    expect(profile.protocol).to.be('https');
   });
 
   it('without env', async () => {
@@ -106,6 +107,7 @@ describe('with local ~/.fcli/config.yaml', () => {
     expect(profile.defaultRegion).to.be('cn-hangzhou');
     expect(profile.timeout).to.be(60);
     expect(profile.retries).to.be(10);
+    expect(profile.protocol).to.be('https');
   });
 
   it('pattern', ()=>{
@@ -114,4 +116,64 @@ describe('with local ~/.fcli/config.yaml', () => {
     expect(correctValue).to.be(true);
     expect(notCorrectValue).to.be(false);
   });
+});
+
+describe('with local ~/.fcli/config.yaml for endpoint http', () => {
+  let restoreProcess;
+
+  beforeEach(async () => {
+
+    restoreProcess = setProcess({
+      HOME: os.tmpdir()
+    });
+
+    await mkdirp(`${os.homedir}/.fcli/`);
+    await writeFile(`${os.homedir}/.fcli/config.yaml`, yaml.dump({
+      endpoint: `http://123344234.cn-hangzhou.fc.aliyuncs.com`,
+      api_version: '2016-08-15',       
+      access_key_id: '22222',
+      access_key_secret: '3333333',
+      security_token: '',
+      user_agent: 'fcli-0.1',
+      debug: false,
+      timeout: 60,
+      sls_endpoint: `cn-hangzhou.log.aliyuncs.com`,
+      retries: 10
+    }));
+  });
+
+  afterEach(async () => {
+    rimraf.sync(`${os.homedir}/.fcli/`);
+
+    restoreProcess();
+  });
+
+  it('with env', async () => {    
+    process.env.ACCOUNT_ID = '111111';
+    process.env.ACCESS_KEY_ID = '121111';
+    process.env.ACCESS_KEY_SECRET = '111311';
+    process.env.DEFAULT_REGION = '141111';
+    process.env.TIMEOUT = 10;
+    process.env.RETRIES = 2;
+
+    const profile = await getProfile();
+    expect(profile.accountId).to.be(process.env.ACCOUNT_ID);
+    expect(profile.accessKeyId).to.be(process.env.ACCESS_KEY_ID);
+    expect(profile.accessKeySecret).to.be(process.env.ACCESS_KEY_SECRET);
+    expect(profile.defaultRegion).to.be(process.env.DEFAULT_REGION);
+    expect(profile.timeout).to.be(process.env.TIMEOUT);
+    expect(profile.retries).to.be(process.env.RETRIES);
+    expect(profile.protocol).to.be('http');
+  });
+
+  it('without env', async () => {
+    const profile = await getProfile();
+    expect(profile.accountId).to.be('123344234');
+    expect(profile.accessKeyId).to.be('22222');
+    expect(profile.accessKeySecret).to.be('3333333');
+    expect(profile.defaultRegion).to.be('cn-hangzhou');
+    expect(profile.timeout).to.be(60);
+    expect(profile.retries).to.be(10);
+    expect(profile.protocol).to.be('http');
+  });
 });
\ No newline at end of file