1#!/usr/bin/python2 2 3import unittest 4import common 5from autotest_lib.frontend import setup_django_environment 6import serviceHandler 7 8 9class RpcMethodHolder(object): 10 @staticmethod 11 def service_1(x, y): 12 return x + y 13 14 @staticmethod 15 def service_2(path): 16 return path.split('/')[-1] 17 18 19json_request1 = """ 20{ 21 "method": "service_1", 22 "params": [7, 9], 23 "id": null 24} 25""" 26 27expected_response1 = '{"error": null, "result": 16, "id": null}' 28 29 30json_request2 = """ 31{ 32 "method": "service_2", 33 "params": ["http://www.some.url.com/path/to/package.rpm"], 34 "id": null 35} 36""" 37 38expected_response2 = '{"error": null, "result": "package.rpm", "id": null}' 39 40json_request3 = """ 41{ 42 "method": "service_3", 43 "params": ["I wonder if this works"], 44 "id": null 45} 46""" 47 48 49class TestServiceHandler(unittest.TestCase): 50 def setUp(self): 51 holder = RpcMethodHolder() 52 self.serviceHandler = serviceHandler.ServiceHandler(holder) 53 54 55 def test_handleRequest1(self): 56 response = self.serviceHandler.handleRequest(json_request1) 57 self.assertEquals(response, expected_response1) 58 59 60 def test_handleRequest2(self): 61 response = self.serviceHandler.handleRequest(json_request2) 62 self.assertEquals(response, expected_response2) 63 64 65 def test_handleRequest3(self): 66 response = self.serviceHandler.handleRequest(json_request3) 67 response_obj = eval(response.replace('null', 'None')) 68 self.assertNotEquals(response_obj['error'], 'None') 69 70 71if __name__ == "__main__": 72 unittest.main() 73