@@ -26,14 +26,21 @@ def setUp(self):
2626 self .app = Flask ('test_app' )
2727 self .api_client = self .app .test_client ()
2828 self .router = mock .Mock ()
29+ self .module1 = mock .Mock ()
30+ self .module2 = mock .Mock ()
31+ self .modules = {'module1' : self .module1 ,
32+ 'module2' : self .module2 }
2933
30- self .api = Api (self .app , self .router )
34+ self .api = Api (self .modules , self .app , self .router )
35+
36+ def generate_module_path (self , module_name ):
37+ return '/{0}/' .format (module_name )
3138
3239 def test_list_implemented_methods (self ):
3340 self .router .list_implemented_methods .return_value = ['abcd' , 'efgh' ]
3441
35- output = self .api_client .get ('/ module1/' )
36- self .router .list_implemented_methods .assert_called_with (' module1' )
42+ output = self .api_client .get (self . generate_module_path ( ' module1' ) )
43+ self .router .list_implemented_methods .assert_called_with (self . module1 )
3744
3845 assert_that (json .loads (output .data .decode (output .charset )), is_ ({
3946 "implemented_methods" : [
@@ -44,7 +51,7 @@ def test_list_implemented_methods(self):
4451
4552 def test_execute_method_returns_string (self ):
4653 self .router .invoke_method .return_value = 'simple string'
47- output = self .api_client .post ('/ module2/' ,
54+ output = self .api_client .post (self . generate_module_path ( ' module2' ) ,
4855 headers = {'Content-Type' : 'application/json' },
4956 data = json .dumps (
5057 {
@@ -57,12 +64,12 @@ def test_execute_method_returns_string(self):
5764 }
5865 ))
5966
60- self .router .invoke_method .assert_called_with (module_name = ' module2' , method = 'remote_method' , params = [], env = {'variable1' : 'value1' }, callback = {})
67+ self .router .invoke_method .assert_called_with (module = self . module2 , method = 'remote_method' , params = [], env = {'variable1' : 'value1' }, callback = {})
6168 assert_that (json .loads (output .data .decode (output .charset )), is_ ('simple string' ))
6269
6370 def test_execute_method_returns_list (self ):
6471 self .router .invoke_method .return_value = ['a' , 'b' , 'c' ]
65- output = self .api_client .post ('/ module2/' ,
72+ output = self .api_client .post (self . generate_module_path ( ' module2' ) ,
6673 headers = {'Content-Type' : 'application/json' },
6774 data = json .dumps (
6875 {
@@ -75,6 +82,30 @@ def test_execute_method_returns_list(self):
7582 }
7683 ))
7784
78- self .router .invoke_method .assert_called_with (module_name = ' module2' , method = 'remote_method' , params = [], env = {'variable1' : 'value1' }, callback = {})
85+ self .router .invoke_method .assert_called_with (module = self . module2 , method = 'remote_method' , params = [], env = {'variable1' : 'value1' }, callback = {})
7986 assert_that (json .loads (output .data .decode (output .charset )), is_ (['a' , 'b' , 'c' ]))
8087
88+ def test_invoking_unknown_module_returns_a_404 (self ):
89+ output = self .api_client .post (self .generate_module_path ('new_module' ),
90+ headers = {'Content-Type' : 'application/json' },
91+ data = json .dumps (
92+ {
93+ "method" : "remote_method" ,
94+ "params" : [],
95+ "env" : {
96+ "variable1" : "value1"
97+ },
98+ "callback" : {}
99+ }
100+ ))
101+
102+ assert_that (output .status_code , is_ (404 ))
103+
104+ def test_listing_unknown_module_returns_a_404 (self ):
105+ output = self .api_client .get (self .generate_module_path ('new_module' ))
106+
107+ assert_that (output .status_code , is_ (404 ))
108+
109+ class NoTrailingSlashApiTest (ApiTest ):
110+ def generate_module_path (self , module_name ):
111+ return '/{0}' .format (module_name )
0 commit comments