mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-05-10 14:58:27 +00:00
add test code
This commit is contained in:
parent
e1053b15d4
commit
f0832f37dc
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "anyproxy",
|
"name": "anyproxy",
|
||||||
"version": "1.1.1",
|
"version": "1.1.2",
|
||||||
"description": "a charles/fiddle like web proxy for developers which can intercept https requests without browser warning",
|
"description": "a charles/fiddle like web proxy for developers which can intercept https requests without browser warning",
|
||||||
"main": "proxy.js",
|
"main": "proxy.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
@ -12,7 +12,9 @@
|
|||||||
"colorful": "^2.1.0",
|
"colorful": "^2.1.0",
|
||||||
"commander": "~2.3.0"
|
"commander": "~2.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {},
|
"devDependencies": {
|
||||||
|
"tunnel": "0.0.3"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "nodeunit test.js"
|
"test": "nodeunit test.js"
|
||||||
},
|
},
|
||||||
|
9
proxy.js
9
proxy.js
@ -1,5 +1,3 @@
|
|||||||
//TODO : get free port
|
|
||||||
|
|
||||||
var http = require('http'),
|
var http = require('http'),
|
||||||
https = require('https'),
|
https = require('https'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
@ -10,6 +8,7 @@ var http = require('http'),
|
|||||||
program = require('commander'),
|
program = require('commander'),
|
||||||
color = require('colorful'),
|
color = require('colorful'),
|
||||||
certMgr = require("./lib/certMgr"),
|
certMgr = require("./lib/certMgr"),
|
||||||
|
getPort = require("./lib/getPort"),
|
||||||
requestHandler = require("./lib/requestHandler");
|
requestHandler = require("./lib/requestHandler");
|
||||||
|
|
||||||
var T_TYPE_HTTP = 0,
|
var T_TYPE_HTTP = 0,
|
||||||
@ -25,6 +24,10 @@ function proxyServer(type, port, hostname,ruleFile){
|
|||||||
proxyHost = hostname || DEFAULT_HOST;
|
proxyHost = hostname || DEFAULT_HOST;
|
||||||
|
|
||||||
self.httpProxyServer = null;
|
self.httpProxyServer = null;
|
||||||
|
self.close = function(){
|
||||||
|
self.httpProxyServer && self.httpProxyServer.close();
|
||||||
|
console.log(color.green("server closed :" + proxyHost + ":" + proxyPort));
|
||||||
|
}
|
||||||
|
|
||||||
if(ruleFile){
|
if(ruleFile){
|
||||||
if(fs.existsSync(ruleFile)){
|
if(fs.existsSync(ruleFile)){
|
||||||
@ -68,7 +71,6 @@ function proxyServer(type, port, hostname,ruleFile){
|
|||||||
self.httpProxyServer.listen(proxyPort);
|
self.httpProxyServer.listen(proxyPort);
|
||||||
callback(null);
|
callback(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
//final callback
|
//final callback
|
||||||
@ -84,7 +86,6 @@ function proxyServer(type, port, hostname,ruleFile){
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return self.httpProxyServer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.proxyServer = proxyServer;
|
module.exports.proxyServer = proxyServer;
|
||||||
|
84
test.js
84
test.js
@ -1,20 +1,78 @@
|
|||||||
|
|
||||||
var https = require("https"),
|
var https = require("https"),
|
||||||
http = require("http");
|
http = require("http"),
|
||||||
|
proxy = require("./proxy"),
|
||||||
|
tunnel= require('tunnel'),
|
||||||
|
tls = require("tls");
|
||||||
|
|
||||||
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
|
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
|
||||||
|
|
||||||
|
module.exports.httpOverHttp = function(test){
|
||||||
|
var testDesc = "httpOverHttp",
|
||||||
|
proxyServer = new proxy.proxyServer("http","8004");
|
||||||
|
|
||||||
|
try{
|
||||||
|
var test_option_http_over_http = {
|
||||||
|
host: "localhost",
|
||||||
|
port: 8004,
|
||||||
|
path: "/",
|
||||||
|
headers: {
|
||||||
|
Host: "www.baidu.com"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var options = {
|
http.get(test_option_http_over_http, function(res) {
|
||||||
host: "localhost",
|
var data = "";
|
||||||
port: 8001,
|
res.on("data",function(chunk){
|
||||||
path: "/",
|
data += chunk;
|
||||||
headers: {
|
});
|
||||||
Host: "www.alipay.com"
|
res.on("end",function(){
|
||||||
|
proxyServer.close();
|
||||||
|
test.ok(data.length > 50, testDesc);
|
||||||
|
test.done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}catch(e){
|
||||||
|
console.log(e);
|
||||||
|
test.ok(false,testDesc);
|
||||||
|
test.done();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
https.get(options, function(res) {
|
|
||||||
console.log(res);
|
|
||||||
res.pipe(process.stdout);
|
module.exports.testHttpsOverHttp = function(test){
|
||||||
});
|
var testDesc = "httpsOverHttp";
|
||||||
|
var proxyServer = new proxy.proxyServer("http","8004");
|
||||||
|
|
||||||
|
try{
|
||||||
|
var tunnelingAgent = tunnel.httpsOverHttp({
|
||||||
|
proxy: {
|
||||||
|
host: 'localhost',
|
||||||
|
port: 8004
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var req = https.request({
|
||||||
|
host: 'www.alipay.com',
|
||||||
|
port: 443,
|
||||||
|
agent: tunnelingAgent
|
||||||
|
},function(res){
|
||||||
|
var data = "";
|
||||||
|
res.on("data",function(chunk){
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on("end",function(){
|
||||||
|
proxyServer.close();
|
||||||
|
test.ok(data.length > 50, testDesc);
|
||||||
|
test.done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
|
||||||
|
}catch(e){
|
||||||
|
console.log(e);
|
||||||
|
test.ok(false,testDesc);
|
||||||
|
test.done();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user