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 "--------------------";
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'
run: |
echo "======================================================================";

View File

@ -104,6 +104,16 @@ jobs:
echo "--------------------";
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'
run: |
echo "======================================================================";

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,4 @@
const assert = require('node:assert')
const lodash = require('lodash')
const mergeApi = require('../src/merge.js')
@ -84,5 +85,8 @@ const doMergeExpect = {
f: {},
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')
assert.strictEqual(result, true)

View File

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

View File

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

View File

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

View File

@ -33,6 +33,6 @@ const dnsMap = dns.initDNS({
SpeedTest.initSpeedTest({ hostnameList: {}, dnsMap })
const tester = new SpeedTester({ hostname: 'github.com' })
tester.test().then((ret) => {
console.log(tester.alive)
tester.test().then(() => {
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',
// 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'
@ -52,26 +51,34 @@ const dnsProviders = dns.initDNS({
// const matched = regexp.exec(test)
// console.log('data:', matched)
const hostname0 = 'github.com'
// console.log('first')
// dnsProviders.cloudflare.lookup(hostname0)
console.log('test')
dnsProviders.py233.lookup(hostname0)
// dnsProviders.cloudflare.lookup(hostname0)
// dnsProviders.ipaddress.lookup(hostname0)
// dnsProviders.ipaddress.lookup(hostname0)
const hostname1 = 'github.com'
const hostname2 = 'api.github.com'
const hostname3 = 'hk.docmirror.cn'
const hostname4 = 'github.docmirror.cn'
const hostname5 = 'gh.docmirror.top'
const hostname6 = 'gh2.docmirror.top'
// const hostname = 'api.github.com'
// dnsProviders.cloudflare.lookup(hostname)
let ip
// const hostname1 = 'api.github.com'
// dnsProviders.cloudflare.lookup(hostname1)
//
// const hostname2 = 'hk.docmirror.cn'
// dnsProviders.cloudflare.lookup(hostname2)
// const hostname3 = 'github.docmirror.cn'
// dnsProviders.cloudflare.lookup(hostname3)
// const hostname4 = 'gh.docmirror.top'
// dnsProviders.cloudflare.lookup(hostname4)
// const hostname5 = 'gh2.docmirror.top'
// dnsProviders.cloudflare.lookup(hostname5)
// console.log('test cloudflare')
// ip = await dnsProviders.cloudflare.lookup(hostname1)
// console.log('ip:', ip)
// ip = await dnsProviders.cloudflare.lookup(hostname2)
// console.log('ip:', ip)
// ip = await dnsProviders.cloudflare.lookup(hostname3)
// console.log('ip:', ip)
// ip = await dnsProviders.cloudflare.lookup(hostname4)
// console.log('ip:', ip)
// 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'
// https://raw.fastgit.org/docmirror/dev-sidecar/master/doc/index.png
const ret = name.replace(/^(.+)\/raw\/(.+)$/, 'raw.fastgit.org$1/$2')
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))
assert.strictEqual('/greper/d2-crud-plus/blob/master/.eslintignore'.match(reg), null)
const chunk = Buffer.from('<head></head>')
const script = '<script>a</script>'
@ -15,6 +19,12 @@ chunk.copy(chunkNew, 0, 0, index)
scriptBuf.copy(chunkNew, index, 0)
chunk.copy(chunkNew, index + scriptBuf.length, index)
console.log(chunkNew.toString())
assert.strictEqual(chunkNew.toString(), '<head><script>a</script></head>')
const reg2 = '/aaaa/i'
console.log(new RegExp(reg2).test('aaaa'))
const reg2 = /aaaa/i
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(value43, true)
const value51 = matchUtil.matchHostname(hostMap, 'eee.cn', 'test5.1')
const value52 = matchUtil.matchHostname(hostMap, 'x.eee.cn', 'test5.2')
const value53 = matchUtil.matchHostname(hostMap, 'aaaa.cnet.com', 'test5.3')
const value51 = matchUtil.matchHostname(hostMap, 'zzz.cn', 'test5.1')
const value52 = matchUtil.matchHostname(hostMap, 'x.zzz.cn', 'test5.2')
const value53 = matchUtil.matchHostname(hostMap, 'zzz.cnet.com', 'test5.3')
console.log('test5: *.cn')
assert.strictEqual(value51, 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')
console.log(string)
const pacClient = pac.createPacClient('../gui/extra/pac/pac.txt') // 相对于 mitmproxy 目录的相对路径,而不是当前 test 目录的。
const string2 = pac.FindProxyForURL('https://http2.golang.org', 'http2.golang.org')
console.log(string2)
const string = pacClient.FindProxyForURL('https://www.facebook.com', 'www.facebook.com')
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 options = {
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',
},
lookup (hostname, options, callback) {
const ip = '106.52.191.148'
console.log('lookup')
callback(null, ip, 4)
},
}
const request = http.get('http://test.target/', options, (response) => {
response.on('data', (data) => {
process.stdout.write(data)
})
})
request.on('error', (error) => {
console.log(error)
})
// const http = require('node:http')
//
// const options = {
// 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',
// },
// lookup (hostname, options, callback) {
// const ip = '106.52.191.148'
// console.log('lookup')
// callback(null, ip, 4)
// },
// }
//
// const request = http.get('http://test.target/', options, (response) => {
// response.on('data', (data) => {
// process.stdout.write(data)
// })
// })
//
// request.on('error', (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 res = {
@ -29,3 +30,19 @@ const result = responseReplace.replaceResponseHeaders(newHeaders, res, proxyRes)
console.log(proxyRes.rawHeaders)
console.log(headers)
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')
let arr
arr = util.parseHostnameAndPort('www.baidu.com')
console.log(arr)
console.log(arr.length === 1) // true
console.log(arr[0] === 'www.baidu.com') // true
console.log('arr1:', arr)
assert.strictEqual(arr.length === 1, true) // true
assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
arr = util.parseHostnameAndPort('www.baidu.com', 80)
console.log(arr)
console.log(arr.length === 2) // true
console.log(arr[0] === 'www.baidu.com') // true
console.log(arr[1] === 80) // true
console.log('arr2:', arr)
assert.strictEqual(arr.length === 2, true) // true
assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
assert.strictEqual(arr[1] === 80, true) // true
arr = util.parseHostnameAndPort('www.baidu.com:8080')
console.log(arr)
console.log(arr.length === 2) // true
console.log(arr[0] === 'www.baidu.com') // true
console.log(arr[1] === 8080) // true
console.log('arr3:', arr)
assert.strictEqual(arr.length === 2, true) // true
assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
assert.strictEqual(arr[1] === 8080, true) // true
arr = util.parseHostnameAndPort('www.baidu.com:8080', 8080)
console.log(arr)
console.log(arr.length === 2) // true
console.log(arr[0] === 'www.baidu.com') // true
console.log(arr[1] === 8080) // true
console.log('arr4:', arr)
assert.strictEqual(arr.length === 2, true) // true
assert.strictEqual(arr[0] === 'www.baidu.com', true) // true
assert.strictEqual(arr[1] === 8080, true) // true
arr = util.parseHostnameAndPort('[2001:abcd::1]')
console.log(arr)
console.log(arr.length === 1) // true
console.log(arr[0] === '[2001:abcd::1]') // ture
console.log('arr5:', arr)
assert.strictEqual(arr.length === 1, true) // true
assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // ture
arr = util.parseHostnameAndPort('[2001:abcd::1]', 80)
console.log(arr)
console.log(arr.length === 2) // true
console.log(arr[0] === '[2001:abcd::1]') // ture
console.log(arr[1] === 80) // ture
console.log('arr6:', arr)
assert.strictEqual(arr.length === 2, true) // true
assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // ture
assert.strictEqual(arr[1] === 80, true) // ture
arr = util.parseHostnameAndPort('[2001:abcd::1]:8080')
console.log(arr)
console.log(arr.length === 2) // true
console.log(arr[0] === '[2001:abcd::1]') // true
console.log(arr[1] === 8080) // ture
console.log('arr7:', arr)
assert.strictEqual(arr.length === 2, true) // true
assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // true
assert.strictEqual(arr[1] === 8080, true) // ture
arr = util.parseHostnameAndPort('[2001:abcd::1]:8080', 8080)
console.log(arr)
console.log(arr.length === 2) // true
console.log(arr[0] === '[2001:abcd::1]') // true
console.log(arr[1] === 8080) // ture
console.log('arr8:', arr)
assert.strictEqual(arr.length === 2, true) // true
assert.strictEqual(arr[0] === '[2001:abcd::1]', true) // true
assert.strictEqual(arr[1] === 8080, true) // ture