forked from vokimon/testfarm-server2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Service_test.py
executable file
·382 lines (321 loc) · 9.48 KB
/
Service_test.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/python
_serviceCode = """\
#!/usr/bin/python
import sys # not used, just to try to call it
Protocol="TestingProtocol"
_private = "Private content"
Numeric = 13
def Function0() :
return "Function0 content"
def Function0_html() :
return "Function0_html <b>content</b>"
Function0_html.content_type = 'text/html'
def ErrorFunction() :
return [][0]
def Function1(param1) :
return "param1 = %s"%param1
def Function1Optional(param1="defaultValue") :
return "param1 = %s"%param1
def FunctionKeyword(**kwd) :
return str(kwd)
def FunctionPositional(a, *b) :
return "a = '%s'\\nargs = %s"%(a,b)
def FunctionRequest(request, a, b) :
return request.method
def FunctionRequestKeyword(request, **kwd) :
return request.method
def FunctionReturningResponse(request) :
import webob
return webob.Response("Content",
content_type='text/plain',
)
def dummySigner(signature, id, **kwd) :
import Service
keys = dict(
alibaba="sesame, open",
)
if id not in keys : raise Service.Forbidden("Not such id")
expectedKey = keys[id]+'0' #str(len(kwd))
if signature != expectedKey : raise Service.Forbidden("Bad signature")
import spike_expanddecorator
@spike_expanddecorator.expand_decorator(dummySigner)
def signedFunction0() :
return "Ok"
"""
import wsgi_intercept.urllib2_intercept
import unittest
import urllib2
import HttpFormPost
import os
class ServiceTest(unittest.TestCase) :
def setUp(self) :
source = open("TestingService.py",'w')
source.write(_serviceCode)
source.close()
del source
import Service
self.app = Service.Reload(Service.Service([
"TestingService",
]))
def createApp() : return self.app
wsgi_intercept.urllib2_intercept.install_opener()
wsgi_intercept.add_wsgi_intercept('myhost', 80, createApp)
def tearDown(self) :
wsgi_intercept.urllib2_intercept.uninstall_opener()
os.unlink("TestingService.py")
if os.path.exists("TestingService.pyc") :
os.unlink("TestingService.pyc")
def request(self, query, postdata=None) :
body = None
headers = {}
if postdata is not None :
content_type, body = HttpFormPost.encode_multipart_formdata_dictionary(postdata)
headers['Content-Type'] = content_type
req=urllib2.Request('http://myhost:80/'+query, body, headers)
return urllib2.urlopen(req)
def assertContent(self, query, body=None, headers=None, post=None) :
try :
req = self.request(query, post)
requestBody = req.read()
if body is not None :
self.assertEquals(body, requestBody)
if headers is not None :
headers = headers.format(bodysize=len(requestBody))
self.assertEquals(headers, str(req.headers))
except urllib2.HTTPError as e :
print (e.read())
raise
def assertError(self, query, code, body=None, headers=None, post=None) :
try :
res = self.request(query, post)
self.fail("HTTP error expected. Received '%s'"%res.read())
except urllib2.HTTPError as e :
requestBody = e.read()
if body is not None :
self.assertEquals(body, requestBody)
self.assertEquals(code, e.getcode())
if headers is not None:
headers = headers.format(bodysize=len(requestBody))
self.assertEquals(headers, str(e.headers))
def headerHtmlText(self) :
return (
"""Content-Type: text/html; charset=UTF-8\n"""
"""Content-Length: {bodysize}\n"""
)
def headerPlainText(self) :
return (
"""Content-Type: text/plain; charset=UTF-8\n"""
"""Content-Length: {bodysize}\n"""
)
def testMissingModule(self) :
self.assertError(
'BadModule/Protocol',
code = 404,
body = "NotFound: Bad service BadModule\n",
headers = self.headerPlainText(),
)
def testMissingTarget(self) :
self.assertError(
'TestingService/MissingTarget',
code = 404,
body = "NotFound: Bad function TestingService.MissingTarget\n",
headers = self.headerPlainText(),
)
def testGetAttributes(self) :
self.assertContent(
'TestingService/Protocol',
body="TestingProtocol",
)
def testGetAttributes_defaultsToPlainText(self) :
self.assertContent(
'TestingService/Protocol',
headers = self.headerPlainText(),
)
def testPrivateObject(self) :
self.assertError(
'TestingService/_private',
code = 403,
body = "Forbidden: Private object\n",
headers = self.headerPlainText(),
)
def testNumericAttribute(self) :
self.assertContent(
"TestingService/Numeric",
'13')
def testModule_failsNotFound(self) :
self.assertError(
'TestingService/sys',
code = 404,
body = "NotFound: Bad function TestingService.sys\n",
headers = self.headerPlainText(),
)
def testFunction0_html(self) :
self.assertContent(
"TestingService/Function0_html",
'Function0_html <b>content</b>',
headers = self.headerHtmlText(),
)
def testErrorFunction0_html(self) :
self.assertError(
"TestingService/ErrorFunction",
500,
'IndexError: list index out of range\n',
)
def testFunction1_withNoParams(self) :
self.assertError(
"TestingService/Function1",
400,
'BadRequest: Missing parameters: param1\n',
headers = self.headerPlainText(),
)
def testFunction1_usingGet(self) :
self.assertContent(
"TestingService/Function1?param1=value1",
body = 'param1 = value1',
headers = self.headerPlainText(),
)
def testFunction1_usingMultipleGet_lastWins(self) :
self.assertContent(
"TestingService/Function1?param1=value1¶m1=value2",
body = 'param1 = value2',
headers = self.headerPlainText(),
)
def testFunction1_usingPost(self) :
self.assertContent(
"TestingService/Function1",
post = dict(param1='post value'),
body = 'param1 = post value',
headers = self.headerPlainText(),
)
def testFunction1_usingPostAndUri_getWins(self) :
self.assertContent(
"TestingService/Function1?param1=get",
post = dict(param1='post'),
body = 'param1 = get',
headers = self.headerPlainText(),
)
def testFunction0_withParams(self) :
self.assertError(
"TestingService/Function0?param=value",
400,
'BadRequest: Unavailable parameter: param\n',
headers = self.headerPlainText(),
)
def testFunction1Optional_withoutTheParam(self) :
self.assertContent(
"TestingService/Function1Optional",
body = 'param1 = defaultValue',
headers = self.headerPlainText(),
)
def testFunctionKeyword_withoutParams(self) :
self.assertContent(
"TestingService/FunctionKeyword",
body = '{}',
headers = self.headerPlainText(),
)
def testFunctionKeyword_withParams(self) :
self.assertContent(
"TestingService/FunctionKeyword?a=1&b=2",
body = "{u'a': u'1', u'b': u'2'}",
headers = self.headerPlainText(),
)
def testFunctionPositional(self) :
self.assertContent(
"TestingService/FunctionPositional?a=1",
body = "a = '1'\nargs = ()",
headers = self.headerPlainText(),
)
def testFunctionPositional_withExtraParam(self) :
self.assertError(
"TestingService/FunctionPositional?a=1&c=2",
code = 400,
body = "BadRequest: Unavailable parameter: c\n",
headers = self.headerPlainText(),
)
def testFunctionPositional_withExtraParamNamedLikeThePositional(self) :
self.assertError(
"TestingService/FunctionPositional?a=1&b=2",
code = 400,
body = "BadRequest: Unavailable parameter: b\n",
headers = self.headerPlainText(),
)
def testFunctionRequest(self) :
self.assertContent(
"TestingService/FunctionRequest?a=1&b=2",
body = "GET",
headers = self.headerPlainText(),
)
def testFunctionRequest_requestHijack(self) :
self.assertError(
"TestingService/FunctionRequest?a=1&b=2&request='hijack'",
400,
body = "BadRequest: Unavailable parameter: request\n",
headers = self.headerPlainText(),
)
def testFunctionRequest(self) :
self.assertContent(
"TestingService/FunctionRequestKeyword?a=1&b=2",
body = "GET",
headers = self.headerPlainText(),
)
def testFunctionRequestKeyword_requestHijack(self) :
self.assertError(
"TestingService/FunctionRequestKeyword?request='hijack'",
400,
body = "BadRequest: Unavailable parameter: request\n",
headers = self.headerPlainText(),
)
def testReload(self) :
import time
script = "TestingService.py"
self.request("TestingService/Function0").read()
creationtime = os.stat(script).st_mtime
while True : # mtime has just one second of resolution
source = open(script,'w')
source.write(
"print 'Loading'\n"
"def Function0() : return 'Reloaded!!'\n"
)
source.close()
if os.stat(script).st_mtime != creationtime : break
self.assertContent(
"TestingService/Function0",
body = "Reloaded!!",
headers = self.headerPlainText(),
)
def test_noService(self) :
self.assertError(
"TestingService?bad=boom&good=nice",
400,
body = "BadRequest: Specify a subservice within 'TestingService'\n",
headers = self.headerPlainText(),
)
def test_FunctionReturningResponse(self) :
self.assertContent(
"TestingService/FunctionReturningResponse",
body = "Content",
headers = self.headerPlainText(),
)
def test_signedFunction0_withNoParameters(self) :
self.assertError(
"TestingService/signedFunction0",
400,
body = "BadRequest: Missing parameters: signature, id\n",
headers = self.headerPlainText(),
)
def test_signedFunction0_withBadId(self) :
self.assertError(
"TestingService/signedFunction0?id=badId&signature=nevermind",
403,
body = "Forbidden: Not such id\n",
headers = self.headerPlainText(),
)
def test_signedFunction0_withBadSignature(self) :
self.assertError(
"TestingService/signedFunction0?id=alibaba&signature=sesame0",
403,
body = "Forbidden: Bad signature\n",
headers = self.headerPlainText(),
)
if __name__=="__main__" :
unittest.main()