def test_exception(self): self.agent = HttpAgent('http://localhost:%d' % (HttpAgentTest.PORT + 1)) response = self.agent.get('test') self.assertIsNone(response.http_code) self.assertIsNone(response.output) self.assertIsNotNone(response.exception) self.assertTrue(isinstance(response.exception, URLError))
def test_exception(self): self.agent = HttpAgent('http://localhost:%d' % (HttpAgentTest.PORT+1)) response = self.agent.get('test') self.assertIsNone(response.http_code) self.assertIsNone(response.output) self.assertIsNotNone(response.exception) self.assertTrue(isinstance(response.exception, URLError))
def new_agent(self, bindings): """Implements citest.service_testing.AgentTestScenario.new_agent.""" return HttpAgent('http://{host}:{port}'.format( host=bindings['HOST'], port=bindings['PORT']))
def setUp(self): self.agent = HttpAgent('http://localhost:%d' % HttpAgentTest.PORT) self.context = ExecutionContext()
class HttpAgentTest(unittest.TestCase): PORT = None SERVER = None @staticmethod def __alloc_port(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('', 0)) port = sock.getsockname()[1] sock.close() return int(port) @staticmethod def setUpClass(): HttpAgentTest.PORT = HttpAgentTest.__alloc_port() HttpAgentTest.SERVER = TestServer.make(HttpAgentTest.PORT) server_thread = threading.Thread( target=HttpAgentTest.SERVER.serve_forever, name='server') server_thread.daemon = True server_thread.start() def setUp(self): self.agent = HttpAgent('http://localhost:%d' % HttpAgentTest.PORT) self.context = ExecutionContext() def test_http_get(self): response = self.agent.get('test') self.assertEquals(200, response.http_code) self.assertEquals('', response.output) self.assertIsNone(response.exception) tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, msg in tests: response = self.agent.get('test?code=%d&message=%s' % (code, msg)) self.assertEquals(code, response.http_code) self.assertEquals(msg, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'GET', headers_to_dict(response.headers).get('XCall')) def test_http_delete(self): response = self.agent.get('test') self.assertEquals(200, response.http_code) self.assertEquals('', response.output) self.assertIsNone(response.exception) tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, msg in tests: response = self.agent.delete('test?code=%d' % code, msg) self.assertEquals(code, response.http_code) self.assertEquals(msg, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'DELETE', headers_to_dict(response.headers).get('XCall')) def test_http_post(self): tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, payload in tests: response = self.agent.post('test?code=%d' % code, payload) self.assertEquals(code, response.http_code) self.assertEquals(payload, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'POST', headers_to_dict(response.headers).get('XCall')) def test_exception(self): self.agent = HttpAgent('http://localhost:%d' % (HttpAgentTest.PORT + 1)) response = self.agent.get('test') self.assertIsNone(response.http_code) self.assertIsNone(response.output) self.assertIsNotNone(response.exception) self.assertTrue(isinstance(response.exception, URLError)) def test_post_operation_ok(self): op = self.agent.new_post_operation('TestPostOk', 'test/path?code=201', 'Test Data') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertTrue(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals('Test Data', status.detail) self.assertIsNone(status.error) raw_response = status.raw_http_response self.assertEquals(201, raw_response.http_code) def test_post_operation_bad(self): op = self.agent.new_post_operation('TestPostError', 'test/path?code=403', 'Test Error') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals('Test Error', status.detail) self.assertEquals('Test Error', status.error) raw_response = status.raw_http_response self.assertEquals(403, raw_response.http_code) def test_post_operation_timeout(self): # A response code of HTTP 408 indicates a timeout op = self.agent.new_post_operation('TestPostError', 'test/path?code=408', 'Test Error') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertTrue(status.timed_out) self.assertEquals('Test Error', status.detail) self.assertEquals('Test Error', status.error) raw_response = status.raw_http_response self.assertEquals(408, raw_response.http_code) def test_post_operation_async_ok(self): op = self.agent.new_post_operation('TestAsync', 'test/path?code=201', 'Test Response Line', status_class=TestAsyncStatusFactory( 208, 1)) status = op.execute(self.agent) self.assertFalse(status.finished) self.assertFalse(status.finished_ok) status.wait() self.assertTrue(status.finished) self.assertTrue(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals(208, status.raw_http_response.http_code) def test_post_operation_async_timeout(self): op = self.agent.new_post_operation('TestAsync', 'test/path?code=201', 'Test Response Line', status_class=TestAsyncStatusFactory( 408, 1)) status = op.execute(self.agent) self.assertFalse(status.finished) self.assertFalse(status.finished_ok) status.wait() self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertTrue(status.timed_out) self.assertEquals(408, status.raw_http_response.http_code) def test_contract_ok(self): builder = HttpContractBuilder(self.agent) # Here we're setting the observer_factory to make an HttpResponseObserver # so that the observation objects are the HttpResponseType instance rather # than the normal HttpObjectObserver where the objects are the payload data. (builder.new_clause_builder('Expect OK').get_url_path( 'testpath?code=202', observer_factory=HttpResponseObserver).EXPECT( ov_factory.value_list_contains( HttpResponsePredicate(http_code=202)))) contract = builder.build() results = contract.verify(self.context) self.assertTrue(results) def test_contract_failure_ok(self): builder = HttpContractBuilder(self.agent) # Here we're setting the observer_factory to make an HttpResponseObserver # so that the observation objects are the HttpResponseType instance rather # than the normal HttpObjectObserver where the objects are the payload data. # # When we encounter the HTTP error, the HttpResponseType is wrapped in an # HttpAgentError object and put into the observation error list. # So we need to dig it back out of there. # In addition, we're using error_list_matches rather than error_list_contains # so we can strict=True to show exactly the one error. "matches" takes a list # of predicates, so we wrap the error check into a list. (builder.new_clause_builder('Expect NotFound').get_url_path( 'testpath?code=404', observer_factory=HttpResponseObserver).EXPECT( ov_factory.error_list_matches([ HttpAgentErrorPredicate( HttpResponsePredicate(http_code=404)) ], strict=True)) ) # Only the one error in the list contract = builder.build() results = contract.verify(self.context) self.assertTrue(results)
class HttpAgentTest(unittest.TestCase): PORT = None SERVER = None @staticmethod def __alloc_port(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('', 0)) port = sock.getsockname()[1] sock.close() return int(port) @staticmethod def setUpClass(): HttpAgentTest.PORT = HttpAgentTest.__alloc_port() HttpAgentTest.SERVER = TestServer.make(HttpAgentTest.PORT) server_thread = threading.Thread( target=HttpAgentTest.SERVER.serve_forever, name='server') server_thread.daemon = True server_thread.start() def setUp(self): self.agent = HttpAgent('http://localhost:%d' % HttpAgentTest.PORT) self.context = ExecutionContext() def test_http_get(self): response = self.agent.get('test') self.assertEquals(200, response.http_code) self.assertEquals('', response.output) self.assertIsNone(response.exception) tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, msg in tests: response = self.agent.get('test?code=%d&message=%s' % (code, msg)) self.assertEquals(code, response.http_code) self.assertEquals(msg, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'GET', headers_to_dict(response.headers).get('XCall')) def test_http_delete(self): response = self.agent.get('test') self.assertEquals(200, response.http_code) self.assertEquals('', response.output) self.assertIsNone(response.exception) tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, msg in tests: response = self.agent.delete('test?code=%d' % code, msg) self.assertEquals(code, response.http_code) self.assertEquals(msg, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'DELETE', headers_to_dict(response.headers).get('XCall')) def test_http_post(self): tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, payload in tests: response = self.agent.post('test?code=%d' % code, payload) self.assertEquals(code, response.http_code) self.assertEquals(payload, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'POST', headers_to_dict(response.headers).get('XCall')) def test_exception(self): self.agent = HttpAgent('http://localhost:%d' % (HttpAgentTest.PORT+1)) response = self.agent.get('test') self.assertIsNone(response.http_code) self.assertIsNone(response.output) self.assertIsNotNone(response.exception) self.assertTrue(isinstance(response.exception, URLError)) def test_post_operation_ok(self): op = self.agent.new_post_operation( 'TestPostOk', 'test/path?code=201', 'Test Data') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertTrue(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals('Test Data', status.detail) self.assertIsNone(status.error) raw_response = status.raw_http_response self.assertEquals(201, raw_response.http_code) def test_post_operation_bad(self): op = self.agent.new_post_operation( 'TestPostError', 'test/path?code=403', 'Test Error') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals('Test Error', status.detail) self.assertEquals('Test Error', status.error) raw_response = status.raw_http_response self.assertEquals(403, raw_response.http_code) def test_post_operation_timeout(self): # A response code of HTTP 408 indicates a timeout op = self.agent.new_post_operation( 'TestPostError', 'test/path?code=408', 'Test Error') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertTrue(status.timed_out) self.assertEquals('Test Error', status.detail) self.assertEquals('Test Error', status.error) raw_response = status.raw_http_response self.assertEquals(408, raw_response.http_code) def test_post_operation_async_ok(self): op = self.agent.new_post_operation( 'TestAsync', 'test/path?code=201', 'Test Response Line', status_class=TestAsyncStatusFactory(208, 1)) status = op.execute(self.agent) self.assertFalse(status.finished) self.assertFalse(status.finished_ok) status.wait() self.assertTrue(status.finished) self.assertTrue(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals(208, status.raw_http_response.http_code) def test_post_operation_async_timeout(self): op = self.agent.new_post_operation( 'TestAsync', 'test/path?code=201', 'Test Response Line', status_class=TestAsyncStatusFactory(408, 1)) status = op.execute(self.agent) self.assertFalse(status.finished) self.assertFalse(status.finished_ok) status.wait() self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertTrue(status.timed_out) self.assertEquals(408, status.raw_http_response.http_code) def test_contract_ok(self): builder = HttpContractBuilder(self.agent) # Here we're setting the observer_factory to make an HttpResponseObserver # so that the observation objects are the HttpResponseType instance rather # than the normal HttpObjectObserver where the objects are the payload data. (builder.new_clause_builder('Expect OK') .get_url_path('testpath?code=202', observer_factory=HttpResponseObserver) .EXPECT(ov_factory.value_list_contains(HttpResponsePredicate(http_code=202)))) contract = builder.build() results = contract.verify(self.context) self.assertTrue(results) def test_contract_failure_ok(self): builder = HttpContractBuilder(self.agent) # Here we're setting the observer_factory to make an HttpResponseObserver # so that the observation objects are the HttpResponseType instance rather # than the normal HttpObjectObserver where the objects are the payload data. # # When we encounter the HTTP error, the HttpResponseType is wrapped in an # HttpAgentError object and put into the observation error list. # So we need to dig it back out of there. # In addition, we're using error_list_matches rather than error_list_contains # so we can strict=True to show exactly the one error. "matches" takes a list # of predicates, so we wrap the error check into a list. (builder.new_clause_builder('Expect NotFound') .get_url_path('testpath?code=404', observer_factory=HttpResponseObserver) .EXPECT(ov_factory.error_list_matches( [HttpAgentErrorPredicate(HttpResponsePredicate(http_code=404))], strict=True))) # Only the one error in the list contract = builder.build() results = contract.verify(self.context) self.assertTrue(results)
def setUp(self): self.agent = HttpAgent('http://localhost:%d' % HttpAgentTest.PORT)
class HttpAgentTest(unittest.TestCase): PORT = None SERVER = None @staticmethod def __alloc_port(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('', 0)) port = sock.getsockname()[1] sock.close() return int(port) @staticmethod def setUpClass(): HttpAgentTest.PORT = HttpAgentTest.__alloc_port() HttpAgentTest.SERVER = TestServer.make(HttpAgentTest.PORT) server_thread = threading.Thread( target=HttpAgentTest.SERVER.serve_forever, name='server') server_thread.daemon = True server_thread.start() def setUp(self): self.agent = HttpAgent('http://localhost:%d' % HttpAgentTest.PORT) def test_http_get(self): response = self.agent.get('test') self.assertEquals(200, response.http_code) self.assertEquals('', response.output) self.assertIsNone(response.exception) tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, msg in tests: response = self.agent.get('test?code=%d&message=%s' % (code, msg)) self.assertEquals(code, response.http_code) self.assertEquals(msg, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'GET', headers_to_dict(response.headers).get('XCall')) def test_http_delete(self): response = self.agent.get('test') self.assertEquals(200, response.http_code) self.assertEquals('', response.output) self.assertIsNone(response.exception) tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, msg in tests: response = self.agent.delete('test?code=%d' % code, msg) self.assertEquals(code, response.http_code) self.assertEquals(msg, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'DELETE', headers_to_dict(response.headers).get('XCall')) def test_http_post(self): tests = [(200, 'OK'), (201, 'StillOk'), (400, 'Dang'), (404, 'Whoops'), (500, 'Yikes')] for code, payload in tests: response = self.agent.post('test?code=%d' % code, payload) self.assertEquals(code, response.http_code) self.assertEquals(payload, response.output) if code < 300: # urllib2 strips out headers from errors self.assertEquals( 'POST', headers_to_dict(response.headers).get('XCall')) def test_exception(self): self.agent = HttpAgent('http://localhost:%d' % (HttpAgentTest.PORT + 1)) response = self.agent.get('test') self.assertIsNone(response.http_code) self.assertIsNone(response.output) self.assertIsNotNone(response.exception) self.assertTrue(isinstance(response.exception, urllib2.URLError)) def test_post_operation_ok(self): op = self.agent.new_post_operation('TestPostOk', 'test/path?code=201', 'Test Data') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertTrue(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals('Test Data', status.detail) self.assertIsNone(status.error) raw_response = status.raw_http_response self.assertEquals(201, raw_response.http_code) def test_post_operation_bad(self): op = self.agent.new_post_operation('TestPostError', 'test/path?code=403', 'Test Error') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals('Test Error', status.detail) self.assertEquals('Test Error', status.error) raw_response = status.raw_http_response self.assertEquals(403, raw_response.http_code) def test_post_operation_timeout(self): # A response code of HTTP 408 indicates a timeout op = self.agent.new_post_operation('TestPostError', 'test/path?code=408', 'Test Error') status = op.execute(self.agent) self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertTrue(status.timed_out) self.assertEquals('Test Error', status.detail) self.assertEquals('Test Error', status.error) raw_response = status.raw_http_response self.assertEquals(408, raw_response.http_code) def test_post_operation_async_ok(self): op = self.agent.new_post_operation('TestAsync', 'test/path?code=201', 'Test Response Line', status_class=TestAsyncStatusFactory( 208, 1)) status = op.execute(self.agent) self.assertFalse(status.finished) self.assertFalse(status.finished_ok) status.wait() self.assertTrue(status.finished) self.assertTrue(status.finished_ok) self.assertFalse(status.timed_out) self.assertEquals(208, status.raw_http_response.http_code) def test_post_operation_async_timeout(self): op = self.agent.new_post_operation('TestAsync', 'test/path?code=201', 'Test Response Line', status_class=TestAsyncStatusFactory( 408, 1)) status = op.execute(self.agent) self.assertFalse(status.finished) self.assertFalse(status.finished_ok) status.wait() self.assertTrue(status.finished) self.assertFalse(status.finished_ok) self.assertTrue(status.timed_out) self.assertEquals(408, status.raw_http_response.http_code)