def test_from_headers(self, mimetype): headers = {"Content-Type": MimeType.to_string(mimetype)} extracted = MimeType.from_headers(headers) if isinstance(mimetype, MimeType): assert extracted is mimetype else: assert extracted == mimetype
def validate_request( self, req_url: str, req_headers: MutableMapping[str, str], req_params: MutableMapping[str, str], req_data: Optional[bytes], mock_response: MockResponse, ) -> None: self.req_url = req_url self.req_params = req_params self.req_headers = req_headers self.req_data = req_data self.req_data_decoded = None self.validate_url() self.validate_headers() self.validate_params() mimetype = MimeType.from_headers(req_headers) self.validate_media(mimetype=mimetype) self.validate_media_type(mimetype) if self.custom_hook is not None: self.custom_hook(self, mock_response)
async def handle_response_aio( response: ClientResponse, valid_status_codes: Union[int, Tuple[int, ...]] = 200, data_schema: Optional[Union[Schema, MimeType]] = None, api_errors_additional: Optional[Dict[int, Type[APIError]]] = None, current_data_object: Optional[ModelType] = None, data_object_updater: Optional[Callable[[ModelType, Any], None]] = None, decoders: DecoderIndexType = DEFAULT_DECODERS, ) -> ResponseData: """ Examines response from SpanReed service and raises reported errors. :param response: from aiohttp :param valid_status_codes: Valid return http code(s). :param data_schema: Schema object for loading responses. :param api_errors_additional: Code, Error Class Mapping of Additional APIError types the response may return. :param current_data_object: Current object which represents response payload. Will be updated in-place with response data. :param data_object_updater: Callable which takes args: (current_data_object, new_data_object). Used to update current_data_object in place of the default updater. :return: Loaded data, raw data mapping (dict or bson record). :raises ResponseStatusError: If status code does match. :raises ContentTypeUnknownError: If content-type is not a type that is known. :raises marshmallow.ValidationError: If data not consistent with schema. """ # Try to raise error, catch if it does not exist. try: raise Error.from_headers(response.headers).to_exception(api_errors_additional) except NoErrorReturnedError: pass _check_status_code( received_code=response.status, valid_status_codes=valid_status_codes, response=response, ) content = await response.read() if content or data_schema is not None: try: loaded_data, decoded_data = decode_content( content=content, mimetype=MimeType.from_headers(response.headers), data_schema=data_schema, allow_sniff=True, decoders=decoders, ) except ContentDecodeBase as error: raise ContentDecodeError(str(error), response=response) except ContentTypeUnknownBase as error: raise ContentTypeUnknownError(str(error), response=response) else: loaded_data, decoded_data = None, None if current_data_object is not None: _update_data( current_data_object=current_data_object, new_data_object=loaded_data, object_updater=data_object_updater, ) loaded_data = current_data_object return ResponseData(resp=response, loaded=loaded_data, decoded=decoded_data)