Skip to content

Commit

Permalink
修复ssr配置项的一个问题,修改spec测试
Browse files Browse the repository at this point in the history
  • Loading branch information
erguotou520 committed Feb 8, 2018
1 parent 33ac4d2 commit cb91d46
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 96 deletions.
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -22,7 +22,8 @@
"pack:main": "cross-env NODE_ENV=production webpack --progress --colors --config .electron-vue/webpack.main.config.js",
"pack:renderer": "cross-env NODE_ENV=production webpack --progress --colors --config .electron-vue/webpack.renderer.config.js",
"test": "npm run unit",
"unit": "karma start test/unit/karma.conf.js"
"unit": "karma start test/unit/karma.conf.js",
"mocha": "./node_modules/.bin/mocha --compilers js:babel-core/regi ster test/unit/specs/qrcode.spec.js"
},
"dependencies": {
"auto-launch": "^5.0.5",
Expand All @@ -35,6 +36,7 @@
"rxjs": "^5.5.5",
"simple-web-proxy": "^0.1.0",
"sudo-prompt": "^8.1.0",
"urlsafe-base64": "^1.0.0",
"vue": "^2.3.3",
"vuex": "^3.0.1"
},
Expand All @@ -49,7 +51,7 @@
"babel-register": "^6.24.1",
"babili-webpack-plugin": "^0.1.2",
"cfonts": "^1.1.3",
"chai": "^4.0.0",
"chai": "^4.1.2",
"chalk": "^2.1.0",
"copy-webpack-plugin": "^4.0.1",
"cross-env": "^5.0.5",
Expand All @@ -68,7 +70,7 @@
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^0.11.2",
"html-webpack-plugin": "^2.30.1",
"mocha": "^3.0.2",
"mocha": "^5.0.0",
"multispinner": "^0.2.1",
"node-loader": "^0.6.0",
"style-loader": "^0.18.2",
Expand Down
13 changes: 3 additions & 10 deletions src/shared/ssr.js
@@ -1,19 +1,12 @@
import Base64 from 'urlsafe-base64'
import { generateID, isNumber, isObject } from './utils'

export function encode (str) {
return Buffer.from(str, 'utf-8').toString('base64')
.replace(/\+/g, '-') // Convert '+' to '-'
.replace(/\//g, '_') // Convert '/' to '_'
.replace(/=+$/, '') // Remove ending '=')
return Base64.encode(Buffer.from(str, 'utf-8'))
}

export function decode (str) {
// Add removed at end '='
const string = `${str}${Array(5 - str.length % 4).join('=')}`
return string
.replace(/\-/g, '+') // Convert '-' to '+'
.replace(/\_/g, '/') // Convert '_' to '/'
.toString('utf-8')
return Base64.decode(str).toString('utf-8')
}

function merge (ssr, target) {
Expand Down
5 changes: 3 additions & 2 deletions src/shared/utils.js
@@ -1,7 +1,8 @@
import fs from 'fs'
import path from 'path'
import { net } from 'electron'
import { loadConfigsFromString, decode } from './ssr'
import Base64 from 'urlsafe-base64'
import { loadConfigsFromString } from './ssr'

const STRING_PROTOTYPE = '[object String]'
const NUMBER_PROTOTYPE = '[object Number]'
Expand Down Expand Up @@ -292,7 +293,7 @@ export function isSubscribeContentValid (content) {
if (!content) {
return [false]
}
const decoded = decode(content)
const decoded = Base64.decode(content).toString('utf-8')
const configs = loadConfigsFromString(decoded)
if (!configs.length) {
return [false]
Expand Down
34 changes: 17 additions & 17 deletions test/unit/specs/qrcode.spec.js
@@ -1,5 +1,5 @@
var expect = require('chai').expect
var Config = require('../../app/src/Config')
var Config = require('../../../src/shared/ssr').default

var strWithRemark = 'ssr://MTI3LjAuMC4xOjEyMzQ6YXV0aF9hZXMxMjhfbWQ1OmFlcy0xMjgtY2ZiOnRsczEuMl90aWNrZXRfYXV0aDpZV0ZoWW1KaS8_b2Jmc3BhcmFtPVluSmxZV3QzWVRFeExtMXZaUSZyZW1hcmtzPTVyV0w2Sy1WNUxpdDVwYUg'
var strNoRemark = 'ssr://MTI3LjAuMC4xOjEyMzQ6YXV0aF9hZXMxMjhfbWQ1OmFlcy0xMjgtY2ZiOnRsczEuMl90aWNrZXRfYXV0aDpZV0ZoWW1KaS8_b2Jmc3BhcmFtPVluSmxZV3QzWVRFeExtMXZaUQ'
Expand All @@ -16,8 +16,8 @@ function clone (obj) {
describe('Config', function () {
it('ssr-qrcode', function () {
var base = {
host: '127.0.0.1',
port: '1234',
server: '127.0.0.1',
server_port: 1234,
password: 'aaabbb',
method: 'aes-128-cfb',
protocol: 'auth_aes128_md5',
Expand All @@ -26,16 +26,16 @@ describe('Config', function () {
obfsparam: 'breakwa11.moe'
}
var withRemark = clone(base)
withRemark.remark = '测试中文'
withRemark.remarks = '测试中文'
expect(new Config(base).getSSRLink()).to.equal(strNoRemark)
expect(new Config(withRemark).getSSRLink()).to.equal(strWithRemark)
})

it('ssr-decode', function () {
var base = new Config()
base.setSSRLink(strNoRemark)
expect(base.host).to.equal('127.0.0.1')
expect(base.port).to.equal('1234')
expect(base.server).to.equal('127.0.0.1')
expect(base.server_port).to.equal(1234)
expect(base.password).to.equal('aaabbb')
expect(base.method).to.equal('aes-128-cfb')
expect(base.protocol).to.equal('auth_aes128_md5')
Expand All @@ -45,35 +45,35 @@ describe('Config', function () {

var withRemark = new Config()
withRemark.setSSRLink(strWithRemark)
expect(withRemark.host).to.equal('127.0.0.1')
expect(withRemark.port).to.equal('1234')
expect(withRemark.server).to.equal('127.0.0.1')
expect(withRemark.server_port).to.equal(1234)
expect(withRemark.password).to.equal('aaabbb')
expect(withRemark.method).to.equal('aes-128-cfb')
expect(withRemark.protocol).to.equal('auth_aes128_md5')
expect(base.protocolparam).to.equal('')
expect(withRemark.obfs).to.equal('tls1.2_ticket_auth')
expect(withRemark.obfsparam).to.equal('breakwa11.moe')
expect(withRemark.remark).to.equal('测试中文')
expect(withRemark.remarks).to.equal('测试中文')
})

it('ss-qrcode', function () {
var base = {
host: '192.168.100.1',
port: '8888',
server: '192.168.100.1',
server_port: '8888',
password: 'test',
method: 'bf-cfb'
}
var withRemark = clone(base)
withRemark.remark = 'example-server'
withRemark.remarks = 'example-server'
expect(new Config(base).getSSLink()).to.equal(ssStr)
expect(new Config(withRemark).getSSLink()).to.equal(ssStr + '#example-server')
})

it('ss-decode', function () {
var base = new Config()
base.setSSLink(ssStr)
expect(base.host).to.equal('192.168.100.1')
expect(base.port).to.equal('8888')
expect(base.server).to.equal('192.168.100.1')
expect(base.server_port).to.equal(8888)
expect(base.password).to.equal('test')
expect(base.method).to.equal('bf-cfb')
expect(base.protocol).to.equal('origin')
Expand All @@ -82,13 +82,13 @@ describe('Config', function () {

var withRemark = new Config()
withRemark.setSSLink(ssStr + '#example-server')
expect(base.host).to.equal('192.168.100.1')
expect(base.port).to.equal('8888')
expect(base.server).to.equal('192.168.100.1')
expect(base.server_port).to.equal(8888)
expect(base.password).to.equal('test')
expect(base.method).to.equal('bf-cfb')
expect(base.protocol).to.equal('origin')
expect(base.obfs).to.equal('plain')
expect(base.obfsparam).to.equal('')
expect(withRemark.remark).to.equal('example-server')
expect(withRemark.remarks).to.equal('example-server')
})
})

0 comments on commit cb91d46

Please sign in to comment.