def test_success(self): """ Succeessful responses capture status code. """ with self.graph.flask.test_request_context("/"): request_info = RequestInfo(self.options, test_func, None) request_info.capture_response( MagicMock( data="{}", status_code=201, )) dct = request_info.to_dict() assert_that( dct, is_( equal_to( dict( operation="test_func", method="GET", func="test_func", success=True, status_code=201, ))), )
def test_response_body_with_field_renaming(self): """ Can capture the response body with field renaming """ with self.graph.flask.test_request_context("/"): g.show_response_fields = dict(foo="baz") request_info = RequestInfo(self.options, test_func, None) request_info.capture_response( MagicMock( data='{"foo": "bar"}', status_code=200, )) dct = request_info.to_dict() assert_that( dct, is_( equal_to( dict( operation="test_func", method="GET", func="test_func", success=True, status_code=200, response_body=dict(baz="bar"), ))), )
def test_response_body_with_field_deletion(self): """ Can capture the response body with fields removed """ with self.graph.flask.test_request_context("/"): g.hide_response_fields = ["foo"] request_info = RequestInfo(self.options, test_func, None) request_info.capture_response( MagicMock( data='{"foo": "bar", "this": "that"}', status_code=200, )) dct = request_info.to_dict() assert_that( dct, is_( equal_to( dict( operation="test_func", method="GET", func="test_func", success=True, status_code=200, response_body=dict(this="that"), ))), )
def test_response_body_with_field_deletion(self): """ Can capture the response body with fields removed """ with self.graph.flask.test_request_context("/"): g.hide_response_fields = ["foo"] request_info = RequestInfo(self.options, test_func, None) request_info.capture_response(MagicMock( data='{"foo": "bar", "this": "that"}', status_code=200, )) dct = request_info.to_dict() assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", success=True, status_code=200, response_body=dict(this="that"), ))), )
def test_error(self): """ Errors responses capture the error """ with self.graph.flask.test_request_context("/"): request_info = RequestInfo(self.options, test_func, None) try: raise NotFound("Not Found") except Exception as error: request_info.capture_error(error) dct = request_info.to_dict() # NB: stack trace is hard (and pointless) to compare stack_trace = dct.pop("stack_trace") assert_that(stack_trace, is_not(none())) assert_that( dct, is_( equal_to( dict( operation="test_func", method="GET", func="test_func", success=False, status_code=404, message="Not Found", context=dict(errors=[]), ))), )
def test_response_body_with_field_renaming(self): """ Can capture the response body with field renaming """ with self.graph.flask.test_request_context("/"): g.show_response_fields = dict(foo="baz") request_info = RequestInfo(self.options, test_func, None) request_info.capture_response(MagicMock( data='{"foo": "bar"}', status_code=200, )) dct = request_info.to_dict() assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", success=True, status_code=200, response_body=dict(baz="bar"), ))), )
def test_error(self): """ Errors responses capture the error """ with self.graph.flask.test_request_context("/"): request_info = RequestInfo(self.options, test_func, None) try: raise NotFound("Not Found") except Exception as error: request_info.capture_error(error) dct = request_info.to_dict() # NB: stack trace is hard (and pointless) to compare stack_trace = dct.pop("stack_trace") assert_that(stack_trace, is_not(none())) assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", success=False, status_code=404, message="Not Found", context=dict(errors=[]), ))), )
def test_base_info(self): """ Every log entry identifies the request. """ with self.graph.flask.test_request_context("/"): request_info = RequestInfo(self.options, test_func, None) dct = request_info.to_dict() assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", ))), )
def test_request_context(self): """ Log entries can include context from headers. """ with self.graph.flask.test_request_context("/", headers={"X-Request-Id": "request-id"}): request_info = RequestInfo(self.options, test_func, self.graph.request_context) dct = request_info.to_dict() request_id = dct.pop("X-Request-Id") assert_that(request_id, is_(equal_to("request-id"))) assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", ))), )
def test_request_body(self): """ Can capture the request body. """ with self.graph.flask.test_request_context("/", data='{"foo": "bar"}'): request_info = RequestInfo(self.options, test_func, None) request_info.capture_request() dct = request_info.to_dict() assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", request_body=dict(foo="bar"), ))), )
def test_request_body_with_field_deletion(self): """ Can capture the request body with fields removed """ with self.graph.flask.test_request_context("/", data='{"foo": "bar", "this": "that"}'): g.hide_request_fields = ["foo"] request_info = RequestInfo(self.options, test_func, None) request_info.capture_request() dct = request_info.to_dict() assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", request_body=dict(this="that"), ))), )
def test_success(self): """ Succeessful responses capture status code. """ with self.graph.flask.test_request_context("/"): request_info = RequestInfo(self.options, test_func, None) request_info.capture_response(MagicMock( data="{}", status_code=201, )) dct = request_info.to_dict() assert_that( dct, is_(equal_to(dict( operation="test_func", method="GET", func="test_func", success=True, status_code=201, ))), )