def get_response_cache_key(http_response, clean_response=None, headers=None): """ Note: query.body has been cleaned by get_clean_body() :param http_response: The HTTP response we want to get a cache key for :param clean_response: The FourOhFourResponse associated with the HTTPResponse passed as parameter (optional, will be calculated if not provided) :param headers: A string containing the HTTP response headers that have to be used to calculate the hash :return: Hash of the HTTP response body """ headers = '' or headers # # Only some HTTP responses benefit from the XML-bones signature # if _should_use_xml_bones(http_response): body = get_xml_bones(http_response.get_body()) normalized_path = FourOhFourResponse.normalize_path( http_response.get_uri()) else: # # Get a clean_response if it was not provided # if clean_response is None: clean_response = FourOhFourResponse.from_http_response( http_response) body = clean_response.body normalized_path = clean_response.normalized_path # # Calculate the hash using all the captured information # key = ''.join([ str(http_response.get_code()), smart_str_ignore(normalized_path), str(headers), smart_str_ignore(body) ]) return quick_hash(key)
def test_just_text(self): self.assertEqual(get_xml_bones('hello world (); foobar'), 'htmlbodyp20pbodyhtml')
def test_nested(self): self.assertEqual(get_xml_bones('<a><b>hello</b></a>'), 'htmlbodyab0babodyhtml')
def test_broken_2(self): self.assertEqual(get_xml_bones('<xml>'), 'htmlbodyxmlxmlbodyhtml')
def test_attr(self): self.assertEqual(get_xml_bones('<xml id=1>hello</xml>'), 'htmlbodyxmlid00xmlbodyhtml')
def test_extra_large_2(self): # Just adding one more char to the end self.assertEqual(get_xml_bones('<xml>%s</xml>' % ('A' * 41, )), 'htmlbodyxml40xmlbodyhtml')
def test_extra_large_1(self): self.assertEqual(get_xml_bones('<xml>%s</xml>' % ('A' * 30, )), 'htmlbodyxml40xmlbodyhtml')
def test_large(self): self.assertEqual(get_xml_bones('<xml>hello world 123 123</xml>'), 'htmlbodyxml20xmlbodyhtml')
def test_simple(self): self.assertEqual(get_xml_bones('<xml>hello</xml>'), 'htmlbodyxml0xmlbodyhtml')
def test_empty(self): self.assertEqual(get_xml_bones(''), '')