def test_register_api_allow_different_methods(self): metadata = APIMetadata() api1 = AcceptableAPI('api1', '/api', 1) api2 = AcceptableAPI('api2', '/api', 1, options={'methods': ['POST']}) metadata.register_service('test', None) metadata.register_service('other', None) metadata.register_api('test', None, api1) metadata.register_api('other', None, api2)
def serialize_preserves_declaration_order(self): metadata = APIMetadata() svc = metadata.register_service('test', 'group') svc.api('api5', '/api/5', 1) svc.api('api1', '/api/1', 1) svc.api('api3', '/api/3', 1) svc.api('api2', '/api/2', 1) serialized = metadata.serialize() self.assertEqual(['api5', 'api1', 'api3', 'api2'], list(serialized['group']['apis']))
def _setUp(self): self.metadata = APIMetadata() self.service = AcceptableService('service', metadata=self.metadata) foo_api = self.service.api('/foo', 'foo_api', methods=['POST']) @foo_api.view(introduced_at='1.0') def foo(): return "foo", 200
def test_register_api_duplicate_url(self): metadata = APIMetadata() api1 = AcceptableAPI('api1', '/api', 1) api2 = AcceptableAPI('api2', '/api', 1) metadata.register_service('test', None) metadata.register_service('other', None) metadata.register_api('test', None, api1) self.assertRaises( InvalidAPI, metadata.register_api, 'other', None, api2, )
def test_register_service_handles_multiple(self): metadata = APIMetadata() api = AcceptableAPI('api', '/api', 1) metadata.register_service('test', None) self.assertEqual(metadata.services['test', None], {}) metadata.register_api('test', None, api) self.assertEqual(metadata.services['test', None], {'api': api}) # register service again, shouldn't remove any apis metadata.register_service('test', None) self.assertEqual(metadata.services['test', None], {'api': api})
def test_can_register_url_route(self): def view(): return "test view", 200 service = AcceptableService('service', metadata=APIMetadata()) api = service.api('/foo', 'foo_api') api.register_view(view, '1.0') app = Flask(__name__) service.bind(app) client = app.test_client() resp = client.get('/foo') self.assertThat(resp, IsResponse('test view'))
def test_bind_works(self): app = Flask(__name__) metadata = APIMetadata() metadata.register_service('test', None) api1 = AcceptableAPI(None, 'api1', '/api1', 1) api2 = AcceptableAPI(None, 'api2', '/api2', 1) metadata.register_api('test', None, api1) metadata.register_api('test', None, api2) @api1.view(introduced_at=1) def api1_impl(): return 'api1' metadata.bind(app, 'test') self.assertEqual(api1_impl, app.view_functions['api1']) self.assertNotIn('api2', app.view_functions) resp = app.test_client().get('/api1') self.assertThat(resp, IsResponse('api1'))