mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-08-04 21:39:04 +00:00
update to 4.0
This commit is contained in:
93
test/spec_lib/proxyServerModule.js
Normal file
93
test/spec_lib/proxyServerModule.js
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* test for rule replaceOption rule
|
||||
*
|
||||
*/
|
||||
const ip = require('ip');
|
||||
const AnyProxy = require('../../proxy');
|
||||
const { proxyGet, directGet } = require('../util/HttpUtil.js');
|
||||
const Server = require('../server/server.js');
|
||||
|
||||
const OUT_BOUND_IP = ip.address();
|
||||
|
||||
describe('AnyProxy.proxyServer basic test', () => {
|
||||
it('should successfully start a proxy server', done => {
|
||||
const options = {
|
||||
port: 8001,
|
||||
rule: null,
|
||||
webInterface: {
|
||||
enable: true,
|
||||
webPort: 8002,
|
||||
wsPort: 8003,
|
||||
},
|
||||
throttle: 10000,
|
||||
forceProxyHttps: false,
|
||||
silent: false
|
||||
};
|
||||
const proxyServer = new AnyProxy.ProxyServer(options);
|
||||
proxyServer.on('ready', () => {
|
||||
proxyServer.close();
|
||||
done();
|
||||
});
|
||||
proxyServer.on('error', done.fail);
|
||||
proxyServer.start();
|
||||
});
|
||||
});
|
||||
|
||||
describe('AnyProxy.proxyServer high order test', () => {
|
||||
let proxyServer;
|
||||
let serverInstance;
|
||||
beforeAll(done => {
|
||||
// jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
|
||||
serverInstance = new Server();
|
||||
|
||||
const options = {
|
||||
port: 8001,
|
||||
rule: null,
|
||||
webInterface: {
|
||||
enable: true,
|
||||
webPort: 8002,
|
||||
},
|
||||
throttle: 10000,
|
||||
forceProxyHttps: false,
|
||||
silent: false
|
||||
};
|
||||
proxyServer = new AnyProxy.ProxyServer(options);
|
||||
proxyServer.on('ready', done);
|
||||
proxyServer.start();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
proxyServer && proxyServer.close();
|
||||
serverInstance && serverInstance.close();
|
||||
});
|
||||
|
||||
it('should work as expected for domain host', done => {
|
||||
// test if proxy server works
|
||||
proxyGet('https://www.tmall.com', {}, {})
|
||||
.then(res => {
|
||||
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
|
||||
done();
|
||||
})
|
||||
.catch(done)
|
||||
});
|
||||
|
||||
it('should work as expected for ip host', done => {
|
||||
// test if proxy server works
|
||||
proxyGet(`https://${OUT_BOUND_IP}:3001/test`, {}, {})
|
||||
.then(res => {
|
||||
expect(res && res.statusCode && res.statusCode === 200).toBe(true);
|
||||
done();
|
||||
})
|
||||
.catch(done)
|
||||
});
|
||||
|
||||
it('should start webinterface correctly', done => {
|
||||
// test web interface
|
||||
directGet('http://127.0.0.1:8002', {}, {})
|
||||
.then(res => {
|
||||
expect(res && res.statusCode && res.statusCode === 200 && res.body.length > 300).toBe(true);
|
||||
done();
|
||||
})
|
||||
.catch(done)
|
||||
});
|
||||
});
|
||||
51
test/spec_lib/ruleLoader.js
Normal file
51
test/spec_lib/ruleLoader.js
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* test for rule replaceOption rule
|
||||
*
|
||||
*/
|
||||
|
||||
const ruleLoader = require('../../lib/ruleLoader');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const localModulePath = path.join(__dirname, '../util/CommonUtil.js');
|
||||
describe('rule loader', () => {
|
||||
it('should successfully cache a remote file', done => {
|
||||
ruleLoader.cacheRemoteFile('https://cdn.bootcss.com/lodash.js/4.16.4/lodash.min.js')
|
||||
.then(filePath => {
|
||||
let content;
|
||||
if (filePath) {
|
||||
content = fs.readFileSync(filePath, { encoding: 'utf8' });
|
||||
}
|
||||
expect(content && content.length > 100).toBe(true);
|
||||
done();
|
||||
})
|
||||
.catch(done.fail);
|
||||
});
|
||||
|
||||
it('should load a local module ../util/CommonUtil', done => {
|
||||
ruleLoader.loadLocalPath(localModulePath)
|
||||
.then(module => {
|
||||
expect(module.printLog).not.toBeUndefined();
|
||||
done();
|
||||
})
|
||||
.catch(done.fail);
|
||||
});
|
||||
|
||||
it('should smart load a remote module', done => {
|
||||
ruleLoader.requireModule('https://cdn.bootcss.com/lodash.js/4.16.4/lodash.min.js')
|
||||
.then(module => {
|
||||
expect(module.VERSION).toEqual('4.16.4');
|
||||
done();
|
||||
})
|
||||
.catch(done.fail);
|
||||
});
|
||||
|
||||
it('should smart load a local module', done => {
|
||||
ruleLoader.requireModule(localModulePath)
|
||||
.then(module => {
|
||||
expect(module.printLog).not.toBeUndefined();
|
||||
done();
|
||||
})
|
||||
.catch(done.fail);
|
||||
});
|
||||
});
|
||||
26
test/spec_lib/util.js
Normal file
26
test/spec_lib/util.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* test for rule replaceOption rule
|
||||
*
|
||||
*/
|
||||
const util = require('../../lib/util');
|
||||
|
||||
describe('utils', () => {
|
||||
it('should get some free ports', done => {
|
||||
const count = 100;
|
||||
const tasks = [];
|
||||
for (let i = 1; i <= count; i++) {
|
||||
tasks.push(util.getFreePort());
|
||||
}
|
||||
Promise.all(tasks)
|
||||
.then((results) => {
|
||||
// ensure ports are unique
|
||||
const portMap = {};
|
||||
results.map((portNumber) => {
|
||||
portMap[portNumber] = true;
|
||||
});
|
||||
expect(Object.keys(portMap).length).toEqual(count);
|
||||
done();
|
||||
})
|
||||
.catch(done.fail);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user