-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathproxyfactory.test.js
149 lines (129 loc) · 4.52 KB
/
proxyfactory.test.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
var assert = require( 'assert' );
var ProxyFactory = require( '../lib-cov/proxyfactory' );
var InterfaceManager = require( '../lib-cov/interfacemanager' );
var ifmgr = new InterfaceManager( '../tests/interface_test.json' );
var cookie = 'ali_ab=42.120.74.193.1395041649126.7; l=c%E6%B5%8B%E8%AF%95%E8%B4%A6%E5%8F%B7135::1395387929931::11; cna=KcVJCxpk1XkCAX136Nv5aaC4; _tb_token_=DcE1K7Gbq9n; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; whl=-1%260%260%260; ck1=; lzstat_uv=16278696413116954092|2511607@2511780@2738597@3258521@878758@2735853@2735859@2735862@2735864@2341454@2868200@2898598; lzstat_ss=3744453007_0_1396468901_2868200|970990289_0_1396468901_2898598; uc3=nk2=AKigXc46EgNui%2FwL&id2=Vy%2BbYvqj0fGT&vt3=F8dHqR%2F5HOhOUWkAFIo%3D&lg2=UtASsssmOIJ0bQ%3D%3D; lgc=c%5Cu6D4B%5Cu8BD5%5Cu8D26%5Cu53F7135; tracknick=c%5Cu6D4B%5Cu8BD5%5Cu8D26%5Cu53F7135; _cc_=U%2BGCWk%2F7og%3D%3D; tg=0; mt=ci=3_1&cyk=0_0; cookie2=1c5db2f359099faff00e14d7f39e16f2; t=e8bd0dbbf4bdb8f3704a1974b8a166b5; v=0; uc1=cookie14=UoLVYyvcdJF0aw%3D%3D';
describe( 'ProxyFactory', function() {
it( 'can only use object of InterfaceManager to initial the factory', function() {
assert.throws( function() {
ProxyFactory.use( {} );
}, function( err ) {
return err.toString()
.indexOf( 'Proxy can only use instance of InterfacefManager' ) !== -1
} );
} );
ProxyFactory.use( ifmgr );
describe( '#getInterfaceIdsByPrefix()', function() {
it( 'should return an id array', function() {
assert.equal( ProxyFactory.getInterfaceIdsByPrefix( 'Search.' ).length, 3 );
} );
} );
describe( '#create()', function() {
it( 'should throw exception when the interface id is invalid', function() {
assert.throws( function() {
ProxyFactory.create( 'Search.getItems' );
}, function( err ) {
return err.toString()
.indexOf( 'Invalid interface id: Search.getItems' ) !== -1;
} );
} );
} );
describe( '#Interceptor()', function() {
it( 'should intercept the request which interface id is matched', function( done ) {
var req = {
headers: {
cookie: ''
},
url: '/Search.suggest?q=a',
on: function( eventName, callback ) {
if ( eventName === 'data' ) {
callback( 'mock chunk' );
} else if ( eventName === 'end' ) {
callback();
}
}
};
var res = {
headers: {
},
end: function( data ) {
assert.notEqual( data.length, 0 );
done();
},
setHeader: function( key, value ) {
this.headers[key] = value;
},
on: function( eventName, callback ) {
if ( eventName === 'data' ) {
callback( 'mock chunk' );
} else if ( eventName === 'end' ) {
callback();
}
}
};
ProxyFactory.Interceptor( req, res );
} );
it( 'should response 404 when the interface id is not matched', function() {
var req = {
headers: {
cookie: ''
},
url: '/Search.what?q=a'
};
var res = {
headers: {
},
end: function( data ) {
assert.strictEqual( this.statusCode, 404 );
},
setHeader: function( key, value ) {
this.headers[key] = value;
}
};
ProxyFactory.Interceptor( req, res );
} );
it( 'should response 404 when the interface id is matched but the intercepted field is configurated as false'
, function() {
var req = {
headers: {
cookie: ''
},
url: '/D.getNav?q=c'
};
var res = {
headers: {
},
end: function( data ) {
assert.strictEqual( this.statusCode, 404 );
},
setHeader: function( key, value ) {
this.headers[key] = value;
}
};
ProxyFactory.Interceptor( req, res );
} );
it( 'should response client interfaces', function( done ) {
var req = {
headers: {
cookie: ''
},
url: '/$interfaces',
on: function( eventName, callback ) {
if ( eventName === 'data' ) {
callback( 'mock chunk' );
} else if ( eventName === 'end' ) {
callback();
}
}
};
var res = {
end: function( data ) {
assert.notEqual( data.length, 0 );
done();
}
};
ProxyFactory.Interceptor( req, res );
} )
} );
} );
var Proxy = ProxyFactory;