1
0
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:
加里 2014-08-19 15:31:28 +08:00
parent e1053b15d4
commit f0832f37dc
3 changed files with 80 additions and 19 deletions

@ -1,6 +1,6 @@
{
"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",
"main": "proxy.js",
"bin": {
@ -12,7 +12,9 @@
"colorful": "^2.1.0",
"commander": "~2.3.0"
},
"devDependencies": {},
"devDependencies": {
"tunnel": "0.0.3"
},
"scripts": {
"test": "nodeunit test.js"
},

@ -1,5 +1,3 @@
//TODO : get free port
var http = require('http'),
https = require('https'),
fs = require('fs'),
@ -10,6 +8,7 @@ var http = require('http'),
program = require('commander'),
color = require('colorful'),
certMgr = require("./lib/certMgr"),
getPort = require("./lib/getPort"),
requestHandler = require("./lib/requestHandler");
var T_TYPE_HTTP = 0,
@ -25,6 +24,10 @@ function proxyServer(type, port, hostname,ruleFile){
proxyHost = hostname || DEFAULT_HOST;
self.httpProxyServer = null;
self.close = function(){
self.httpProxyServer && self.httpProxyServer.close();
console.log(color.green("server closed :" + proxyHost + ":" + proxyPort));
}
if(ruleFile){
if(fs.existsSync(ruleFile)){
@ -68,7 +71,6 @@ function proxyServer(type, port, hostname,ruleFile){
self.httpProxyServer.listen(proxyPort);
callback(null);
}
],
//final callback
@ -84,7 +86,6 @@ function proxyServer(type, port, hostname,ruleFile){
}
);
return self.httpProxyServer;
}
module.exports.proxyServer = proxyServer;

84
test.js

@ -1,20 +1,78 @@
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';
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 = {
host: "localhost",
port: 8001,
path: "/",
headers: {
Host: "www.alipay.com"
http.get(test_option_http_over_http, 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();
});
});
}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();
}
}