def test_get_deserialization_method_json(): resp = FakeResponse("application/json") assert get_deserialization_method(resp) == 'json' resp = FakeResponse("application/json; charset=utf-8") assert get_deserialization_method(resp) == 'json' resp.headers["content-type"] = "application/jrd+json" assert get_deserialization_method(resp) == 'json'
def parse_request_response(self, service, reqresp, response_body_type='', state="", **kwargs): """ Deal with a self.http response. The response are expected to follow a special pattern, having the attributes: - headers (list of tuples with headers attributes and their values) - status_code (integer) - text (The text version of the response) - url (The calling URL) :param service: A :py:class:`oidcservice.service.Service` instance :param reqresp: The HTTP request response :param response_body_type: If response in body one of 'json', 'jwt' or 'urlencoded' :param state: Session identifier :param kwargs: Extra keyword arguments :return: """ # if not response_body_type: # response_body_type = self.response_body_type if reqresp.status_code in SUCCESSFUL: logger.debug('response_body_type: "{}"'.format(response_body_type)) _deser_method = get_deserialization_method(reqresp) if _deser_method != response_body_type: logger.warning('Not the body type I expected: {} != {}'.format( _deser_method, response_body_type)) if _deser_method in ['json', 'jwt', 'urlencoded']: value_type = _deser_method else: value_type = response_body_type logger.debug('Successful response: {}'.format(reqresp.text)) try: return service.parse_response(reqresp.text, value_type, state, **kwargs) except Exception as err: logger.error(err) raise elif reqresp.status_code in [302, 303]: # redirect return reqresp elif reqresp.status_code == 500: logger.error("(%d) %s" % (reqresp.status_code, reqresp.text)) raise ParseError("ERROR: Something went wrong: %s" % reqresp.text) elif 400 <= reqresp.status_code < 500: logger.error('Error response ({}): {}'.format( reqresp.status_code, reqresp.text)) # expecting an error response _deser_method = get_deserialization_method(reqresp) if not _deser_method: _deser_method = 'json' try: err_resp = service.parse_response(reqresp.text, _deser_method) except FormatError: if _deser_method != response_body_type: try: err_resp = service.parse_response( reqresp.text, response_body_type) except (OidcServiceError, FormatError): raise cherrypy.HTTPError( "HTTP ERROR: %s [%s] on %s" % (reqresp.text, reqresp.status_code, reqresp.url)) else: raise cherrypy.HTTPError( "HTTP ERROR: %s [%s] on %s" % (reqresp.text, reqresp.status_code, reqresp.url)) err_resp['status_code'] = reqresp.status_code return err_resp else: logger.error('Error response ({}): {}'.format( reqresp.status_code, reqresp.text)) raise cherrypy.HTTPError( "HTTP ERROR: %s [%s] on %s" % (reqresp.text, reqresp.status_code, reqresp.url))
def test_get_deserialization_method_text(): resp = FakeResponse('text/html') assert get_deserialization_method(resp) == '' resp = FakeResponse('text/plain') assert get_deserialization_method(resp) == ''
def test_get_deserialization_method_urlencoded(): resp = FakeResponse(URL_ENCODED) assert get_deserialization_method(resp) == 'urlencoded'
def test_get_deserialization_method_jwt(): resp = FakeResponse("application/jwt") assert get_deserialization_method(resp) == 'jwt'