def test_basic_get(self, logging_info): config_ = DotDict(logger=logging, web_server=DotDict(ip_address='127.0.0.1', port='88888')) # what the middleware app does is that it creates a class based on # another and sets an attribute called `cls` class MadeUp(middleware_app.MeasuringImplementationWrapper): cls = AuxImplementation1 all_services = {} config = config_ server = CherryPy(config_, (('/aux/(.*)', MadeUp), )) testapp = TestApp(server._wsgi_func) response = testapp.get('/aux/', params={'add': 1}) eq_(response.status, 200) for call in logging_info.call_args_list: # mock calls are funny args = call[0] arg = args[0] if re.findall('measuringmiddleware:[\d\.]+\t/aux/\t\?add=1', arg): break else: raise AssertionError('call never found')
def test_basic_get_with_parsed_query_string(self, logging_info): # what the middleware app does is that it creates a class based on # another and sets an attribute called `cls` class MadeUp(middleware_app.ImplementationWrapper): cls = AuxImplementation5 all_services = {} config = DotDict(logger=logging, web_server=DotDict(ip_address='127.0.0.1', port='88888')) server = CherryPy(config, (('/aux/(.*)', MadeUp), )) testapp = TestApp(server._wsgi_func) response = testapp.get( '/aux/', { 'foo': 'bar', 'names': ['peter', 'anders'] }, ) eq_(response.status, 200) eq_(json.loads(response.body), { 'foo': 'bar', 'names': ['peter', 'anders'] }) logging_info.assert_called_with('Running AuxImplementation5')
def test_errors_to_sentry(self, logging_info, raven_client_mocked): # what the middleware app does is that it creates a class based on # another and sets an attribute called `cls` class MadeUp(middleware_app.ImplementationWrapper): cls = AuxImplementationErroring all_services = {} FAKE_DSN = 'https://[email protected]/XX' mock_logging = mock.MagicMock() config = DotDict(logger=mock_logging, web_server=DotDict(ip_address='127.0.0.1', port='88888'), sentry=DotDict(dsn=FAKE_DSN)) server = CherryPy(config, (('/aux/(.*)', MadeUp), )) def fake_get_ident(exception): return '123456789' mocked_client = mock.MagicMock() mocked_client.get_ident.side_effect = fake_get_ident def fake_client(dsn): assert dsn == FAKE_DSN return mocked_client raven_client_mocked.side_effect = fake_client testapp = TestApp(server._wsgi_func) response = testapp.get('/aux/bla', expect_errors=True) eq_(response.status, 500) mock_logging.info.has_call( [mock.call('Error captured in Sentry. Reference: 123456789')])
def test_basic_get_args(self, logging_info): # what the middleware app does is that it creates a class based on # another and sets an attribute called `cls` class MadeUp(middleware_app.ImplementationWrapper): cls = AuxImplementation2 all_services = {} config = DotDict(logger=logging, web_server=DotDict(ip_address='127.0.0.1', port='88888')) server = CherryPy(config, (('/aux/(age|gender|misconfigured)/(.*)', MadeUp), )) testapp = TestApp(server._wsgi_func) response = testapp.get('/aux/age/') eq_(response.status, 200) eq_(json.loads(response.body), {'age': 100}) eq_(response.header_dict['content-length'], str(len(response.body))) eq_(response.header_dict['content-type'], 'application/json') logging_info.assert_called_with('Running AuxImplementation2') response = testapp.get('/aux/gender/', expect_errors=True) eq_(response.status, 200) eq_(json.loads(response.body), {'gender': 0}) # if the URL allows a certain first argument but the implementation # isn't prepared for it, it barfs a 405 at you response = testapp.get('/aux/misconfigured/', expect_errors=True) eq_(response.status, 405)
def test_basic_get(self, logging_info): # what the middleware app does is that creates a class based on another # and sets an attribute called `cls` class MadeUp(middleware_app.ImplementationWrapper): cls = AuxImplementation1 config = DotDict( logger=logging, web_server=DotDict( ip_address='127.0.0.1', port='88888' ) ) server = CherryPy(config, ( ('/aux/(.*)', MadeUp), )) testapp = TestApp(server._wsgi_func) response = testapp.get('/aux/') self.assertEqual(response.status, 200) self.assertEqual(json.loads(response.body), {'age': 100}) logging_info.assert_called_with('Running AuxImplementation1') response = testapp.get('/xxxjunkxxx', expect_errors=True) self.assertEqual(response.status, 404)
def test_put_with_data(self, logging_info): # what the middleware app does is that it creates a class based on # another and sets an attribute called `cls` class MadeUp(middleware_app.ImplementationWrapper): cls = AuxImplementation4 all_services = {} config = DotDict( logger=logging, web_server=DotDict( ip_address='127.0.0.1', port='88888' ) ) server = CherryPy(config, ( ('/aux/(.*)', MadeUp), )) testapp = TestApp(server._wsgi_func) response = testapp.put('/aux/', params={'add': 1}) eq_(response.status, 200) eq_(json.loads(response.body), {'age': 101}) logging_info.assert_called_with('Running AuxImplementation4')
def test_errors(self, logging_info): # what the middleware app does is that it creates a class based on # another and sets an attribute called `cls` class WithNotFound(middleware_app.ImplementationWrapper): cls = AuxImplementationWithNotFoundError all_services = {} class WithUnavailable(middleware_app.ImplementationWrapper): cls = AuxImplementationWithUnavailableError all_services = {} class WithMissingArgument(middleware_app.ImplementationWrapper): cls = AuxImplementationWithMissingArgumentError all_services = {} class WithBadArgument(middleware_app.ImplementationWrapper): cls = AuxImplementationWithBadArgumentError all_services = {} config = DotDict(logger=logging, web_server=DotDict(ip_address='127.0.0.1', port='88888')) server = CherryPy(config, ( ('/aux/notfound', WithNotFound), ('/aux/unavailable', WithUnavailable), ('/aux/missing', WithMissingArgument), ('/aux/bad', WithBadArgument), )) testapp = TestApp(server._wsgi_func) # Test a Not Found error response = testapp.get('/aux/notfound', expect_errors=True) eq_(response.status, 404) eq_(response.header('content-type'), 'application/json; charset=UTF-8') body = json.loads(response.body) eq_(body['error']['message'], 'not here') # Test a Timeout error response = testapp.get('/aux/unavailable', expect_errors=True) eq_(response.status, 408) eq_(response.header('content-type'), 'application/json; charset=UTF-8') body = json.loads(response.body) eq_(body['error']['message'], 'unavailable') # Test BadRequest errors response = testapp.get('/aux/missing', expect_errors=True) eq_(response.status, 400) eq_(response.header('content-type'), 'application/json; charset=UTF-8') body = json.loads(response.body) eq_(body['error']['message'], "Mandatory parameter(s) 'missing arg' is missing or empty.") response = testapp.get('/aux/bad', expect_errors=True) eq_(response.status, 400) eq_(response.header('content-type'), 'application/json; charset=UTF-8') body = json.loads(response.body) eq_(body['error']['message'], "Bad value for parameter(s) 'bad arg'")
def test_errors(self, logging_info): # what the middleware app does is that it creates a class based on # another and sets an attribute called `cls` class WithNotFound(middleware_app.ImplementationWrapper): cls = AuxImplementationWithNotFoundError class WithUnavailable(middleware_app.ImplementationWrapper): cls = AuxImplementationWithUnavailableError class WithMissingArgument(middleware_app.ImplementationWrapper): cls = AuxImplementationWithMissingArgumentError class WithBadArgument(middleware_app.ImplementationWrapper): cls = AuxImplementationWithBadArgumentError config = DotDict(logger=logging, web_server=DotDict(ip_address='127.0.0.1', port='88888')) server = CherryPy(config, ( ('/aux/notfound', WithNotFound), ('/aux/unavailable', WithUnavailable), ('/aux/missing', WithMissingArgument), ('/aux/bad', WithBadArgument), )) testapp = TestApp(server._wsgi_func) # Test a Not Found error response = testapp.get('/aux/notfound', expect_errors=True) self.assertEqual(response.status, 404) # Test a Timeout error response = testapp.get('/aux/unavailable', expect_errors=True) self.assertEqual(response.status, 408) # Test BadRequest errors response = testapp.get('/aux/missing', expect_errors=True) self.assertEqual(response.status, 400) response = testapp.get('/aux/bad', expect_errors=True) self.assertEqual(response.status, 400)