github-actions: 运行单元测试

pull/448/head
王良 2025-02-08 16:25:13 +08:00
parent 85f7b1aea0
commit f62d1a3ad6
25 changed files with 242 additions and 160 deletions

View File

@ -101,6 +101,16 @@ jobs:
echo "--------------------"; echo "--------------------";
pnpm install; pnpm install;
- name: 'test packages/core'
run: |
cd packages/core;
pnpm run test;
- name: 'test packages/mitmproxy'
run: |
cd packages/mitmproxy;
pnpm run test;
- name: 'npm run electron:build' - name: 'npm run electron:build'
run: | run: |
echo "======================================================================"; echo "======================================================================";

View File

@ -104,6 +104,16 @@ jobs:
echo "--------------------"; echo "--------------------";
pnpm install; pnpm install;
- name: 'test packages/core'
run: |
cd packages/core;
pnpm run test;
- name: 'test packages/mitmproxy'
run: |
cd packages/mitmproxy;
pnpm run test;
- name: 'npm run electron:build' - name: 'npm run electron:build'
run: | run: |
echo "======================================================================"; echo "======================================================================";

View File

@ -1,12 +1,13 @@
node -v node -v
# 安装ncu
# npm install -g npm-check-updates # npm install -g npm-check-updates
cd ../packages/core cd ../packages/core
ncu -u ncu -u
cd ../gui # cd ../packages/gui
ncu -u # ncu -u
cd ../mitmproxy # cd ../packages/mitmproxy
ncu -u # ncu -u

View File

@ -1,5 +1,4 @@
cd ../
node -v node -v
cd ../
npm install -g pnpm --registry=https://registry.npmmirror.com npm install -g pnpm --registry=https://registry.npmmirror.com

View File

@ -1,7 +1,5 @@
cd ../
chcp 65001
node -v node -v
cd ../
chcp 65001
pnpm install pnpm install

View File

@ -1,7 +1,5 @@
cd ../packages/gui
chcp 65001
node -v node -v
cd ../packages/gui
chcp 65001
npm run electron npm run electron

View File

@ -1,7 +1,7 @@
cd ../packages/gui
node -v node -v
cd ../packages/gui
if not exist "dist_electron" mkdir "dist_electron" if not exist "dist_electron" mkdir "dist_electron"
start dist_electron start dist_electron

View File

@ -1,5 +1,4 @@
cd ../packages/core
node -v node -v
cd ../packages/core
pnpm run test pnpm run test

View File

@ -0,0 +1,4 @@
node -v
cd ../packages/mitmproxy
pnpm run test

View File

@ -1,41 +1,41 @@
const https = require('node:https') // const https = require('node:https')
//
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1' // process.env.NODE_TLS_REJECT_UNAUTHORIZED = '1'
//
function request () { // function request () {
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
const options = { // const options = {
hostname: 'test1.gagedigital.com', // hostname: 'test1.gagedigital.com',
port: 443, // port: 443,
path: '/ssltest.php', // path: '/ssltest.php',
method: 'GET', // method: 'GET',
rejectUnauthorized: true, // rejectUnauthorized: true,
} // }
console.log('ssl test: gagedigital') // console.log('ssl test: gagedigital')
const req = https.request(options, (res) => { // const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode) // console.log('statusCode:', res.statusCode)
console.log('headers:', res.headers) // console.log('headers:', res.headers)
//
res.on('data', (d) => { // res.on('data', (d) => {
process.stdout.write(d) // process.stdout.write(d)
resolve() // resolve()
}) // })
}) // })
//
req.on('error', (e) => { // req.on('error', (e) => {
console.error(e) // console.error(e)
reject(e) // reject(e)
}) // })
req.end() // req.end()
}) // })
} // }
// eslint-disable-next-line no-undef // // eslint-disable-next-line no-undef
describe('ssl.verify', () => { // describe('ssl.verify', () => {
// eslint-disable-next-line no-undef // // eslint-disable-next-line no-undef
it('regex.test.js', async () => { // it('regex.test.js', async () => {
// https.request('https://test1.gagedigital.com/ssltest.php') // // https.request('https://test1.gagedigital.com/ssltest.php')
await request() // await request()
//
// expect(ret).be.ok // // expect(ret).be.ok
}) // })
}) // })

View File

@ -1,3 +1,5 @@
const assert = require('node:assert')
// const childProcess = require('child_process') // const childProcess = require('child_process')
// const util = require('util') // const util = require('util')
// const exec = util.promisify(childProcess.exec) // const exec = util.promisify(childProcess.exec)
@ -14,3 +16,4 @@
let wifiAdaptor = '(151) test' let wifiAdaptor = '(151) test'
wifiAdaptor = wifiAdaptor.substring(wifiAdaptor.indexOf(' ')).trim() wifiAdaptor = wifiAdaptor.substring(wifiAdaptor.indexOf(' ')).trim()
console.log(wifiAdaptor) console.log(wifiAdaptor)
assert.strictEqual(wifiAdaptor, 'test')

View File

@ -1,3 +1,4 @@
const assert = require('node:assert')
const lodash = require('lodash') const lodash = require('lodash')
const mergeApi = require('../src/merge.js') const mergeApi = require('../src/merge.js')
@ -84,5 +85,8 @@ const doMergeExpect = {
f: {}, f: {},
g: [1, 2], g: [1, 2],
} }
console.log('check merge result:', lodash.isEqual(doMergeResult, doMergeExpect))
const result = lodash.isEqual(doMergeResult, doMergeExpect)
console.log('check merge result:', result)
console.log('\r') console.log('\r')
assert.strictEqual(result, true)

View File

@ -1,12 +1,15 @@
const assert = require('node:assert')
const expect = require('chai').expect const expect = require('chai').expect
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
describe('test', () => { describe('test', () => {
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
it('regexp', () => { it('regexp', () => {
const test = '^/[^/]+/[^/]+(/releases(/.*)?)?$' const test = '^/[^/]+/[^/]+(?:/releases(?:/.*)?)?$'
const reg = new RegExp(test) const reg = new RegExp(test)
const ret = reg.test('/docmirror/dev-sidecar/releases/tag') const ret = reg.test('/docmirror/dev-sidecar/releases/tag')
console.log(ret)
assert.strictEqual(ret, true)
expect(ret).be.ok expect(ret).be.ok
}) })

View File

@ -9,7 +9,9 @@
"dev-sidecar" "dev-sidecar"
], ],
"main": "src/index.js", "main": "src/index.js",
"scripts": {}, "scripts": {
"test": "mocha"
},
"dependencies": { "dependencies": {
"@docmirror/dev-sidecar": "workspace:*", "@docmirror/dev-sidecar": "workspace:*",
"agentkeepalive": "^4.5.0", "agentkeepalive": "^4.5.0",

View File

@ -37,16 +37,16 @@ function createPacClient (pacFilePath) {
const __RULES__ = getRules(pacFilePath) const __RULES__ = getRules(pacFilePath)
/* eslint-disable */ /* eslint-disable */
// Was generated by gfwlist2pac in precise mode // Was generated by gfwlist2pac in precise mode
// https://github.com/clowwindy/gfwlist2pac // https://github.com/clowwindy/gfwlist2pac
// 2019-10-06: More 'javascript' way to interaction with main program // 2019-10-06: More 'javascript' way to interaction with main program
// 2019-02-08: Updated to support shadowsocks-windows user rules. // 2019-02-08: Updated to support shadowsocks-windows user rules.
const proxy = __PROXY__ const proxy = __PROXY__
const rules = [] const rules = []
// convert to abp grammar // convert to abp grammar
for (let i = 0; i < __RULES__.length; i++) { for (let i = 0; i < __RULES__.length; i++) {
let s = __RULES__[i] let s = __RULES__[i]
if (s.substring(0, 2) === "||") s += "^" if (s.substring(0, 2) === "||") s += "^"
@ -669,7 +669,8 @@ function createPacClient (pacFilePath) {
return { return {
FindProxyForURL FindProxyForURL,
proxyUrl: __PROXY__
} }
} }

View File

@ -33,6 +33,6 @@ const dnsMap = dns.initDNS({
SpeedTest.initSpeedTest({ hostnameList: {}, dnsMap }) SpeedTest.initSpeedTest({ hostnameList: {}, dnsMap })
const tester = new SpeedTester({ hostname: 'github.com' }) const tester = new SpeedTester({ hostname: 'github.com' })
tester.test().then((ret) => { tester.test().then(() => {
console.log(tester.alive) console.log('github.com tester.alive = ', tester.alive)
}) })

View File

@ -42,7 +42,6 @@ const dnsProviders = dns.initDNS({
// server: ' https://dns.adguard.com/dns-query', // server: ' https://dns.adguard.com/dns-query',
// cacheSize: 1000 // cacheSize: 1000
// } // }
}) })
// const test = '111<tr><th>IP Address</th><td><ul class="comma-separated"><li>140.82.113.4</li></ul></td></tr>2222' // const test = '111<tr><th>IP Address</th><td><ul class="comma-separated"><li>140.82.113.4</li></ul></td></tr>2222'
@ -52,26 +51,34 @@ const dnsProviders = dns.initDNS({
// const matched = regexp.exec(test) // const matched = regexp.exec(test)
// console.log('data:', matched) // console.log('data:', matched)
const hostname0 = 'github.com' const hostname1 = 'github.com'
// console.log('first') const hostname2 = 'api.github.com'
// dnsProviders.cloudflare.lookup(hostname0) const hostname3 = 'hk.docmirror.cn'
console.log('test') const hostname4 = 'github.docmirror.cn'
dnsProviders.py233.lookup(hostname0) const hostname5 = 'gh.docmirror.top'
// dnsProviders.cloudflare.lookup(hostname0) const hostname6 = 'gh2.docmirror.top'
// dnsProviders.ipaddress.lookup(hostname0)
// dnsProviders.ipaddress.lookup(hostname0)
// const hostname = 'api.github.com' let ip
// dnsProviders.cloudflare.lookup(hostname)
// const hostname1 = 'api.github.com'
// dnsProviders.cloudflare.lookup(hostname1) // console.log('test cloudflare')
// // ip = await dnsProviders.cloudflare.lookup(hostname1)
// const hostname2 = 'hk.docmirror.cn' // console.log('ip:', ip)
// dnsProviders.cloudflare.lookup(hostname2) // ip = await dnsProviders.cloudflare.lookup(hostname2)
// const hostname3 = 'github.docmirror.cn' // console.log('ip:', ip)
// dnsProviders.cloudflare.lookup(hostname3) // ip = await dnsProviders.cloudflare.lookup(hostname3)
// const hostname4 = 'gh.docmirror.top' // console.log('ip:', ip)
// dnsProviders.cloudflare.lookup(hostname4) // ip = await dnsProviders.cloudflare.lookup(hostname4)
// const hostname5 = 'gh2.docmirror.top' // console.log('ip:', ip)
// dnsProviders.cloudflare.lookup(hostname5) // ip = await dnsProviders.cloudflare.lookup(hostname5)
// console.log('ip:', ip)
// ip = await dnsProviders.cloudflare.lookup(hostname6)
// console.log('ip:', ip)
// console.log('test py233')
// ip = await dnsProviders.py233.lookup(hostname1)
// console.log('ip:', ip)
// console.log('test ipaddress')
// ip = await dnsProviders.ipaddress.lookup(hostname0)
// console.log('ip:', ip)

View File

@ -1,4 +0,0 @@
const monkey = require('../../../src/lib/monkey')
const scripts = monkey.load()
console.log(scripts[0])

View File

@ -1,10 +1,14 @@
const assert = require('node:assert')
const name = '/docmirror/dev-sidecar/raw/master/doc/index.png' const name = '/docmirror/dev-sidecar/raw/master/doc/index.png'
// https://raw.fastgit.org/docmirror/dev-sidecar/master/doc/index.png // https://raw.fastgit.org/docmirror/dev-sidecar/master/doc/index.png
const ret = name.replace(/^(.+)\/raw\/(.+)$/, 'raw.fastgit.org$1/$2') const ret = name.replace(/^(.+)\/raw\/(.+)$/, 'raw.fastgit.org$1/$2')
console.log(ret) console.log(ret)
assert.strictEqual(ret, 'raw.fastgit.org/docmirror/dev-sidecar/master/doc/index.png')
const reg = new RegExp('^/[^/]+/[^/]+$') const reg = /^\/[^/]+\/[^/]+$/
console.log('/greper/d2-crud-plus/blob/master/.eslintignore'.match(reg)) console.log('/greper/d2-crud-plus/blob/master/.eslintignore'.match(reg))
assert.strictEqual('/greper/d2-crud-plus/blob/master/.eslintignore'.match(reg), null)
const chunk = Buffer.from('<head></head>') const chunk = Buffer.from('<head></head>')
const script = '<script>a</script>' const script = '<script>a</script>'
@ -15,6 +19,12 @@ chunk.copy(chunkNew, 0, 0, index)
scriptBuf.copy(chunkNew, index, 0) scriptBuf.copy(chunkNew, index, 0)
chunk.copy(chunkNew, index + scriptBuf.length, index) chunk.copy(chunkNew, index + scriptBuf.length, index)
console.log(chunkNew.toString()) console.log(chunkNew.toString())
assert.strictEqual(chunkNew.toString(), '<head><script>a</script></head>')
const reg2 = '/aaaa/i' const reg2 = /aaaa/i
console.log(new RegExp(reg2).test('aaaa')) console.log(reg2.test('aaaa')) // true
assert.strictEqual(reg2.test('aaaa'), true)
const reg3 = '/aaaa/i'
console.log(new RegExp(reg3).test('aaaa')) // false
assert.strictEqual(new RegExp(reg3).test('aaaa'), false)

View File

@ -64,9 +64,9 @@ assert.strictEqual(value41, undefined)
assert.strictEqual(value42, true) assert.strictEqual(value42, true)
assert.strictEqual(value43, true) assert.strictEqual(value43, true)
const value51 = matchUtil.matchHostname(hostMap, 'eee.cn', 'test5.1') const value51 = matchUtil.matchHostname(hostMap, 'zzz.cn', 'test5.1')
const value52 = matchUtil.matchHostname(hostMap, 'x.eee.cn', 'test5.2') const value52 = matchUtil.matchHostname(hostMap, 'x.zzz.cn', 'test5.2')
const value53 = matchUtil.matchHostname(hostMap, 'aaaa.cnet.com', 'test5.3') const value53 = matchUtil.matchHostname(hostMap, 'zzz.cnet.com', 'test5.3')
console.log('test5: *.cn') console.log('test5: *.cn')
assert.strictEqual(value51, true) assert.strictEqual(value51, true)
assert.strictEqual(value52, true) assert.strictEqual(value52, true)

View File

@ -0,0 +1,14 @@
const assert = require('node:assert')
const monkey = require('../src/lib/monkey')
let scripts
try {
scripts = monkey.load('../gui/extra/scripts/') // 相对于 mitmproxy 目录的相对路径,而不是当前 test 目录的。
} catch {
scripts = monkey.load('../../gui/extra/scripts/') // 相对于 当前 test 目录的相对路径
}
// console.log(scripts)
assert.strictEqual(scripts.github != null, true)
assert.strictEqual(scripts.google != null, true)
assert.strictEqual(scripts.tampermonkey != null, true)

View File

@ -1,7 +1,12 @@
const pac = require('../src/lib/proxy/middleware/source/pac').createPacClient('../../gui/extra/pac/pac.txt') const assert = require('node:assert')
const pac = require('../src/lib/proxy/middleware/source/pac')
const string = pac.FindProxyForURL('https://www.facebook.com', 'www.facebook.com') const pacClient = pac.createPacClient('../gui/extra/pac/pac.txt') // 相对于 mitmproxy 目录的相对路径,而不是当前 test 目录的。
console.log(string)
const string2 = pac.FindProxyForURL('https://http2.golang.org', 'http2.golang.org') const string = pacClient.FindProxyForURL('https://www.facebook.com', 'www.facebook.com')
console.log(string2) console.log(`facebook: ${string}`)
assert.strictEqual(string, pacClient.proxyUrl)
const string2 = pacClient.FindProxyForURL('https://http2.golang.org', 'http2.golang.org')
console.log(`golang: ${string2}`)
assert.strictEqual(string2, 'DIRECT;')

View File

@ -1,22 +1,22 @@
const http = require('node:http') // const http = require('node:http')
//
const options = { // const options = {
headers: { // headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', // 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
}, // },
lookup (hostname, options, callback) { // lookup (hostname, options, callback) {
const ip = '106.52.191.148' // const ip = '106.52.191.148'
console.log('lookup') // console.log('lookup')
callback(null, ip, 4) // callback(null, ip, 4)
}, // },
} // }
//
const request = http.get('http://test.target/', options, (response) => { // const request = http.get('http://test.target/', options, (response) => {
response.on('data', (data) => { // response.on('data', (data) => {
process.stdout.write(data) // process.stdout.write(data)
}) // })
}) // })
//
request.on('error', (error) => { // request.on('error', (error) => {
console.log(error) // console.log(error)
}) // })

View File

@ -1,4 +1,5 @@
const responseReplace = require('../../../../../src/lib/interceptor/impl/res/responseReplace') const assert = require('node:assert')
const responseReplace = require('../src/lib/interceptor/impl/res/responseReplace')
const headers = {} const headers = {}
const res = { const res = {
@ -29,3 +30,19 @@ const result = responseReplace.replaceResponseHeaders(newHeaders, res, proxyRes)
console.log(proxyRes.rawHeaders) console.log(proxyRes.rawHeaders)
console.log(headers) console.log(headers)
console.log(result) console.log(result)
assert.deepStrictEqual(proxyRes.rawHeaders, [
'Content-Type', 'application/json; charset=utf-8',
'Content-Length', '3',
'ETag', 'W/"2"',
'Date', '',
'Connection', 'keep-alive'
])
assert.deepStrictEqual(headers, {
xxx: 1,
})
assert.deepStrictEqual(result, {
'content-length': '2',
'date': 'Thu, 01 Jan 1970 00:00:00 GMT',
'xxx': null,
})

View File

@ -1,49 +1,50 @@
const assert = require('node:assert')
const util = require('../src/lib/proxy/common/util') const util = require('../src/lib/proxy/common/util')
let arr let arr
arr = util.parseHostnameAndPort('www.baidu.com') arr = util.parseHostnameAndPort('www.baidu.com')
console.log(arr) console.log('arr1:', arr)
console.log(arr.length === 1) // true assert.strictEqual(arr.length === 1, true) // true
console.log(arr[0] === 'www.baidu.com') // true assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
arr = util.parseHostnameAndPort('www.baidu.com', 80) arr = util.parseHostnameAndPort('www.baidu.com', 80)
console.log(arr) console.log('arr2:', arr)
console.log(arr.length === 2) // true assert.strictEqual(arr.length === 2, true) // true
console.log(arr[0] === 'www.baidu.com') // true assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
console.log(arr[1] === 80) // true assert.strictEqual(arr[1] === 80, true) // true
arr = util.parseHostnameAndPort('www.baidu.com:8080') arr = util.parseHostnameAndPort('www.baidu.com:8080')
console.log(arr) console.log('arr3:', arr)
console.log(arr.length === 2) // true assert.strictEqual(arr.length === 2, true) // true
console.log(arr[0] === 'www.baidu.com') // true assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
console.log(arr[1] === 8080) // true assert.strictEqual(arr[1] === 8080, true) // true
arr = util.parseHostnameAndPort('www.baidu.com:8080', 8080) arr = util.parseHostnameAndPort('www.baidu.com:8080', 8080)
console.log(arr) console.log('arr4:', arr)
console.log(arr.length === 2) // true assert.strictEqual(arr.length === 2, true) // true
console.log(arr[0] === 'www.baidu.com') // true assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
console.log(arr[1] === 8080) // true assert.strictEqual(arr[1] === 8080, true) // true
arr = util.parseHostnameAndPort('[2001:abcd::1]') arr = util.parseHostnameAndPort('[2001:abcd::1]')
console.log(arr) console.log('arr5:', arr)
console.log(arr.length === 1) // true assert.strictEqual(arr.length === 1, true) // true
console.log(arr[0] === '[2001:abcd::1]') // ture assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // ture
arr = util.parseHostnameAndPort('[2001:abcd::1]', 80) arr = util.parseHostnameAndPort('[2001:abcd::1]', 80)
console.log(arr) console.log('arr6:', arr)
console.log(arr.length === 2) // true assert.strictEqual(arr.length === 2, true) // true
console.log(arr[0] === '[2001:abcd::1]') // ture assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // ture
console.log(arr[1] === 80) // ture assert.strictEqual(arr[1] === 80, true) // ture
arr = util.parseHostnameAndPort('[2001:abcd::1]:8080') arr = util.parseHostnameAndPort('[2001:abcd::1]:8080')
console.log(arr) console.log('arr7:', arr)
console.log(arr.length === 2) // true assert.strictEqual(arr.length === 2, true) // true
console.log(arr[0] === '[2001:abcd::1]') // true assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // true
console.log(arr[1] === 8080) // ture assert.strictEqual(arr[1] === 8080, true) // ture
arr = util.parseHostnameAndPort('[2001:abcd::1]:8080', 8080) arr = util.parseHostnameAndPort('[2001:abcd::1]:8080', 8080)
console.log(arr) console.log('arr8:', arr)
console.log(arr.length === 2) // true assert.strictEqual(arr.length === 2, true) // true
console.log(arr[0] === '[2001:abcd::1]') // true assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // true
console.log(arr[1] === 8080) // ture assert.strictEqual(arr[1] === 8080, true) // ture