-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxyServer.js
227 lines (187 loc) · 4.78 KB
/
proxyServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* User: jo
* Date: 25.04.13
* Time: 15:05
*
*/
var http = require('http'),
fs = require('fs'),
path =require('path'),
should = require('should'),
req = require('superagent'),
ProxyServer = require('../lib/ProxyServer.js');
var LOCAL_PROXY_PORT = 9999;
var LOCAL_HTTP_PORT = 9998;
var CACHE_DIR = 'test/cachedir';
/**
* Test for ProxyServer
*/
describe('ProxyServer', function () {
/**
* Test for constructor
*/
describe('Constructor', function () {
var proxy, dest;
dest = { host: 'www.google.ru' };
proxy = new ProxyServer({
dest: dest,
localport: LOCAL_PROXY_PORT,
cachedir: CACHE_DIR
});
it('should have dest = "' + dest + '"', function () {
proxy.should.have.property('dest');
proxy.dest.should.have.property('host', dest.host);
proxy.dest.should.have.property('port', 80);
});
it('should have localport = ' + LOCAL_PROXY_PORT, function () {
proxy.should.have.property('localport', LOCAL_PROXY_PORT);
});
it('should have cachedir, and it should be resolved', function () {
proxy.should.have.property('cachedir', path.resolve(CACHE_DIR));
});
it('should have "close" method', function () {
proxy.should.have.property('close');
});
it('should have "listen" method', function () {
proxy.should.have.property('listen');
});
it('should have "reset" method', function () {
proxy.should.have.property('reset');
});
it('should have "plugin" method', function () {
proxy.should.have.property('plugin');
});
});
/**
* Test for proxy
*/
describe('proxy', function () {
var server, requestCount, proxy,
lastRequestHeaders;
requestCount = 0;
server = http.createServer(function (req, res) {
requestCount += 1;
lastRequestHeaders = req.headers;
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello');
});
proxy = new ProxyServer({
dest: { port: LOCAL_HTTP_PORT },
localport: LOCAL_PROXY_PORT,
cachedir: CACHE_DIR
});
before(function (done) {
proxy.listen(function () {
server.listen(LOCAL_HTTP_PORT, done);
});
});
after(function (done) {
proxy.close(function () {
server.close();
done();
});
});
beforeEach(function () {
proxy.reset();
requestCount = 0;
lastRequestHeaders = null;
});
it('should proxied request', function (done) {
function makeReq(done) {
return function () {
req
.get('localhost:' + LOCAL_PROXY_PORT)
.end(function (err, res) {
should.not.exist(err);
should.exist(res);
res.ok.should.be.ok;
res.text.should.be.eql('Hello');
requestCount.should.be.eql(1);
done();
});
}
}
makeReq(makeReq(done))();
});
it('should change host', function (done) {
req
.get('localhost:' + LOCAL_PROXY_PORT)
.end(function (err, res) {
should.not.exist(err);
lastRequestHeaders.should.have.property('host', proxy.dest.host + ':' + proxy.dest.port);
done();
});
});
});
/**
* Test for #reset
*/
describe('#reset', function () {
var proxy = new ProxyServer({
cachedir: CACHE_DIR
});
var hidden = path.join(proxy.cachedir, '.hidden'),
other = path.join(proxy.cachedir, 'blabla');
before(function () {
fs.writeFileSync(hidden, 'hello');
fs.writeFileSync(other, 'hello');
});
after(function () {
if (fs.existsSync(hidden)) fs.unlinkSync(hidden);
if (fs.existsSync(other)) fs.unlinkSync(other);
});
it('should remove all file on cache dir expect hidden', function () {
proxy.reset();
fs.existsSync(hidden).should.be.ok;
fs.existsSync(other).should.be.not.ok;
});
});
/**
* Test for plugin
*/
describe('#plugin', function () {
var server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello');
});
var proxy = new ProxyServer({
dest: { port: LOCAL_HTTP_PORT },
localport: LOCAL_PROXY_PORT,
cachedir: CACHE_DIR
});
before(function (done) {
proxy.listen(function () {
server.listen(LOCAL_HTTP_PORT, done);
});
});
beforeEach(function () {
proxy.reset();
});
after(function (done) {
proxy.close(function () {
server.close();
done();
});
});;
it('should be add function in "__plugins["onRequestComplete"]"', function () {
function onRequestComplete(data, next) { next(); };
proxy.plugin('onRequestComplete', onRequestComplete);
proxy.__plugins["onRequestComplete"].should.include(onRequestComplete);
});
it('should call plugin function', function (done) {
var isCall = false;
function foo(data, next) {
isCall = true;
next();
}
proxy.plugin('onRequestComplete', foo);
req
.get('localhost:' + LOCAL_PROXY_PORT)
.end(function (err, res) {
should.not.exist(err);
isCall.should.be.ok;
done();
});
});
});
});