async def aiohttp_request_method(self, client: ClientSession, method: str,
                                  url: str,
                                  **kwargs) -> HttpRecorderClientResponse:
     try:
         if hasattr(client, "_reentrant_ref_count"):
             client._reentrant_ref_count += 1
         else:
             client._reentrant_ref_count = 1
             client._original_response_class = client._response_class
             client._response_class = HttpRecorderClientResponse
         request_type: HttpRequestType = HttpRequestType.PLAIN
         request_params: Optional[Dict[str, str]] = None
         request_json: Optional[Any] = None
         if "params" in kwargs:
             request_type = HttpRequestType.WITH_PARAMS
             request_params = kwargs.get("params")
         if "json" in kwargs:
             request_type = HttpRequestType.WITH_JSON
             request_json = kwargs.get("json")
         response: HttpRecorderClientResponse = await client._original_request_func(
             method, url, **kwargs)
         response.parent_recorder = self
         with self.begin() as session:
             session: Session = session
             playback_entry: HttpPlayback = HttpPlayback(
                 timestamp=int(time.time() * 1e3),
                 url=url,
                 method=method,
                 request_type=request_type,
                 request_params=request_params,
                 request_json=request_json,
                 response_type=HttpResponseType.HEADER_ONLY,
                 response_code=response.status)
             session.add(playback_entry)
             session.flush()
             response.database_id = playback_entry.id
         return response
     finally:
         client._reentrant_ref_count -= 1
         if client._reentrant_ref_count < 1:
             client._response_class = client._original_response_class
             del client._original_response_class
             del client._reentrant_ref_count
示例#2
0
    def build_response(self, method: str, url: str, payload: str, *,
                       session: ClientSession, status: int) -> ClientResponse:
        response = session._response_class(
            method,
            URL(url),
            request_info=self.mock_module.Mock(),
            writer=self.mock_module.Mock(),
            continue100=None,
            timer=TimerNoop(),
            traces=[],
            loop=session.loop,
            session=session)
        response._headers = CIMultiDictProxy(
            CIMultiDict({'Content-Type': 'application/json'}))
        response.status = status
        if status >= 400:
            response.reason = payload

        response.content = StreamReader(ResponseHandler(session.loop))
        response.content.feed_data(str.encode(payload))
        response.content.feed_eof()
        return response