-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
263 lines (220 loc) · 6.19 KB
/
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
var http = require('http')
, JSONe = require('json-extended')
, request = require('request')
, test = require('tape')
, rpcClient = require('./client')(request)
, rpcServer = require('./server')
, startServer = function (handler, callback) {
http.createServer(handler).listen(0, function () {
this.unref()
callback(null, 'http://localhost:' + this.address().port)
})
}
, setupTest = function (methods, callback) {
var handler = rpcServer({ url: '/rpc', methods: methods})
startServer(handler, function (err, baseUrl) {
if (err) return callback(err)
var client = rpcClient({ url: baseUrl + '/rpc', methodNames: handler.methodNames })
callback(null, handler, client)
})
}
test('compability', function (t) {
var methods = {
foo: function (beep, boop, callback) {
t.equal(beep, 'beep')
t.equal(boop, 'boop')
callback(null, 1, 2, 3)
}
}
setupTest(methods, function (err, handler, client) {
client.foo('beep', 'boop', function (err, a1, a2, a3) {
if (err) return t.end(err)
t.equal(a1, 1)
t.equal(a2, 2)
t.equal(a3, 3)
t.end()
})
})
})
test('error', function (t) {
var methods = {
bar: function (callback) {
var err = new Error('the message')
err.foo = 'bar'
err.hello = -1
callback(err)
}
}
setupTest(methods, function (err, handler, client) {
client.bar(function (err) {
t.ok(err instanceof Error)
if (err) {
t.equal(err.message, 'the message')
t.equal(err.foo, 'bar')
t.equal(err.hello, -1)
}
t.end()
})
})
})
test('nested', function (t) {
var methods = {
foo: {
bar: function (world, callback) {
t.equal(world, 'world')
callback(null, 'Hello, ' + world)
}
}
}
setupTest(methods, function (err, handler, client) {
client.foo.bar('world', function (err, msg) {
if (err) return t.end(err)
t.equal(msg, 'Hello, world')
t.end()
})
})
})
test('deeply nested', function (t) {
var methods = {
foo: {
bar: {
bas: function (world, callback) {
t.equal(world, 'world')
callback(null, 'Hello, ' + world)
}
}
}
}
setupTest(methods, function (err, handler, client) {
t.deepEqual(handler.methodNames, [ 'foo.bar.bas' ])
client.foo.bar.bas('world', function (err, msg) {
if (err) return t.end(err)
t.equal(msg, 'Hello, world')
t.end()
})
})
})
test('server wrapping none-methods', function (t) {
var handler = rpcServer({ url: '/rpc', methods: { foo: 'bar' } })
t.deepEqual(handler.methodNames, [])
t.end()
})
test('error handling in client', function (t) {
var client = rpcClient({
url: 'http://does.not/exists'
, methodNames: [ 'foo' ]
})
client.foo(function (err) {
t.ok(err instanceof Error)
t.end()
})
})
test('bad method name from client', function (t) {
startServer(rpcServer({ url: '/rpc', methods: {} }), function (err, baseUrl) {
var client = rpcClient({
url: baseUrl + '/rpc'
, methodNames: ['foo']
})
client.foo(function (err) {
t.ok(err instanceof Error)
t.end()
})
})
})
test('none-json from client', function (t) {
startServer(rpcServer({ url: '/rpc', methods: { foo: function () {} } }), function (err, baseUrl) {
request.post(baseUrl + '/rpc/foo', { body: 'huh?' }, function (err, res) {
t.equal(res.statusCode, 500)
t.end()
})
})
})
test('error handling bad formatted data from server', function (t) {
var responded = false
, handler = function (req, res) { res.end('badly formatted json')}
startServer(handler, function (err, baseUrl) {
var client = rpcClient({
url: baseUrl + '/rpc'
, methodNames: [ 'foo' ]
})
client.foo(function (err) {
t.ok(err instanceof Error)
t.is(err.type, 'ParseError', 'is custom error type')
t.is(err.source, 'badly formatted json', 'error contains source string that could not be parsed')
t.end()
})
})
})
test('timeout is configurable', function (t) {
var responded = false
, handler = rpcServer({
url: '/rpc'
, methods: {
foo: function (callback) {
setTimeout(function () {
callback(null, 'huh?')
t.equal(responded, true)
t.end()
}, 100)
}
}
})
startServer(handler, function (err, baseUrl) {
var client = rpcClient({
url: baseUrl + '/rpc'
, methodNames: [ 'foo' ]
, timeout: 50
})
client.foo(function (err) {
responded = true
t.ok(err instanceof Error)
})
})
})
test('wrap method that does not take callback', function (t) {
var methods = {
hello: function (world) {
t.equal(world, 'world')
t.end()
}
}
setupTest(methods, function (err, handler, client) {
client.hello('world')
})
})
test('custom encoding (json-extended)', function (t) {
var methods = {
hello: function (data, callback) {
t.deepEqual(data, { foo: new Date(0) })
callback(null, { bar: new Buffer([ 0, 1, 2 ]) })
}
}
, handler = rpcServer({ url: '/rpc', methods: methods, encoding: JSONe })
startServer(handler, function (err, baseUrl) {
if (err) return callback(err)
var client = rpcClient({
url: baseUrl + '/rpc'
, methodNames: [ 'hello' ]
, encoding: JSONe
})
client.hello({ foo: new Date(0) }, function (err, data) {
t.deepEqual(data, { bar: new Buffer([ 0, 1, 2 ]) })
t.end()
})
})
})
test('unicode char in answer', function (t) {
var methods = {
hello: function (callback) {
callback(null, '’')
}
}
setupTest(methods, function (err, handler, client) {
if (err) return t.end(err)
client.hello(function (err, unicodeString) {
t.notOk(err)
t.equal(unicodeString, '’')
t.end()
})
})
})