-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugins.js
130 lines (105 loc) · 3.22 KB
/
plugins.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
/**
* User: jo
* Date: 14.05.13
* Time: 14:41
*
* Test plugins func
*/
var should = require('should'),
url = require('url'),
Proxy = require('../lib/ProxyServer.js'),
tools = require('../lib/tools.js');
/**
* Test for plugins
*/
describe('plugins', function () {
var plugins = Proxy.plugins;
/**
* Test for replaceParamsInHash
*/
describe('#replaceParamsInHash', function () {
var func = plugins.replaceParamsInHash;
it("should change hash, replace bar and doesn't change raw", function (done) {
var raw = new Buffer('GET /index?bar=1&foo=2 HTTP/1.1\r\nHost: localhost\r\n\r\n'),
hash = tools.getHash().update(raw).end(),
plugin = func({ bar: 2 });
var obj = { raw: raw, hash: hash };
plugin(obj, function (err) {
should.not.exist(err);
//data doesn't changed
obj.raw.should.eql(raw);
var newHash = tools.getHash()
.update('GET /index?bar=2&foo=2 HTTP/1.1\r\nHost: localhost\r\n\r\n').end();
obj.hash.should.not.eql(hash);
obj.hash.should.eql(newHash);
done();
});
});
});
/**
* Test for modifyHash
*/
describe('#modifyHash', function () {
var modifyHash = plugins.modifyHash;
it('should call mod function and delegate parsed params', function (done) {
var raw = new Buffer('POST /index?bar=1&foo=2 HTTP/1.1\r\nHost: localhost\r\n\r\nsome data'),
hash = tools.getHash().update(raw).end(),
is1 = false,
is2 = false,
urlObj = url.parse('/index?bar=1&foo=2', true),
uri = {
pathname: urlObj.pathname,
query: urlObj.query
};
var obj = { raw: raw, hash: hash };
function modfn1 (req, next) {
is1 = true;
req.should.have.property('_raw', raw);
req.should.have.property('_hash', hash);
req.should.have.property('method', 'POST');
req.should.have.property('uri').and.obj.should.eql(uri);
req.should.have.property('version', '1.1');
req.should.have.property('headers').and.obj.should.eql({ 'Host': 'localhost' });
req.should.have.property('body');
next();
}
function modfn2 (req, next) {
is2 = true;
req.should.have.property('_raw', raw);
req.should.have.property('_hash', hash);
req.should.have.property('method', 'POST');
req.should.have.property('uri').and.obj.should.eql(uri);
req.should.have.property('version', '1.1');
req.should.have.property('headers').and.obj.should.eql({ 'Host': 'localhost' });
req.should.have.property('body');
next();
}
var plugin = modifyHash([modfn1, modfn2]);
plugin(obj, function (err) {
is1.should.be.ok;
is2.should.be.ok;
done();
});
});
it('should calculate new hash sum', function (done) {
var raw = new Buffer('GET /index?bar=1&foo=2 HTTP/1.1\r\nHost: localhost\r\n\r\n'),
hash = tools.getHash().update(raw).end(),
expectHash = tools.getHash()
.update(new Buffer('GET /index?bar=1&foo=4 HTTP/1.1\r\nHost: localhost1\r\n\r\n'))
.end();
var obj = { raw: raw, hash: hash };
function mod(req, next) {
req.uri.query.foo = 4;
req.headers.Host = 'localhost1';
next();
}
var plugin = modifyHash([mod]);
plugin(obj, function (err) {
should.not.exist(err);
obj.raw.should.eql(raw);
obj.hash.should.eql(expectHash);
done();
});
});
});
});