def __call__(self, request): start_time = time.time() request_body = self.prepare_request_body(request) response = self.get_response(request) interval = str((time.time() - start_time) * 1000) method = request.method if request_body is None: request_body = self.prepare_request_body(request, response) try: if response.content: response_body = str(response.content.decode("utf8")) else: response_body = None except AttributeError: response_body = None HttpMessage.send( self.logger, request=HttpRequestImpl( method=method, url=str(request.build_absolute_uri()), headers=request.headers, params=request.POST if method == "POST" else request.GET, body=request_body, ), response=HttpResponseImpl( status=response.status_code, body=response_body, headers=response, ), interval=interval, ) return response
def test_formats_response_with_missing_details(): queue = [] logger = HttpLogger(queue=queue, rules="include debug") HttpMessage.send(logger, request=mock_request(), response=HttpResponseImpl(), response_body=None, request_body=None, now=None, interval=None) assert len(queue) == 1 msg = queue[0] assert parseable(msg) is True assert f"response_body" not in msg assert f"response_code" not in msg assert f"response_header" not in msg assert f"interval" not in msg
def __call__(self, environ, start_response) -> ClosingIterator: body__ = self.request_body(environ) def _start_response(status, response_headers, *args): self.start_response(status, response_headers) return start_response(status, response_headers, *args) response_chunks = self.finish_response( self.app(environ, _start_response)) request = Request(environ) parased_raw_params: Dict[str, List[str]] = parse.parse_qs( parse.urlparse(request.url).query) params: Dict[str, str] = {} # Type correction for k, v in parased_raw_params.items(): params[k] = v[0] HttpMessage.send( self.logger, request=HttpRequestImpl( method=request.method, url=str(request.url), headers=dict(request.headers), params=params, body=body__, ), response=HttpResponseImpl( status=self.status, body=str(self.response[0].decode()) if self.response else None, headers=dict(self.response_headers), ), interval=str(self.interval), ) return ClosingIterator(response_chunks)
def mock_response(): r = HttpResponseImpl() r.status = 200 return r
def test_body(): r = HttpResponseImpl() r.body = MOCK_JSON assert r.body == MOCK_JSON
def test_url(): r = HttpResponseImpl() r.url = MOCK_URL assert r.url == MOCK_URL
def test_headers(): r = HttpResponseImpl() r.headers['ABC'] = '123' assert len(r.headers) == 1 assert r.headers['ABC'] == '123'
def test_headers(): r = HttpResponseImpl() r.headers["ABC"] = "123" assert len(r.headers) == 1 assert r.headers["ABC"] == "123"