Esempio n. 1
0
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)
Esempio n. 2
0
 def test_just_text(self):
     self.assertEqual(get_xml_bones('hello world (); foobar'),
                      'htmlbodyp20pbodyhtml')
Esempio n. 3
0
 def test_nested(self):
     self.assertEqual(get_xml_bones('<a><b>hello</b></a>'),
                      'htmlbodyab0babodyhtml')
Esempio n. 4
0
 def test_broken_2(self):
     self.assertEqual(get_xml_bones('<xml>'), 'htmlbodyxmlxmlbodyhtml')
Esempio n. 5
0
 def test_attr(self):
     self.assertEqual(get_xml_bones('<xml id=1>hello</xml>'),
                      'htmlbodyxmlid00xmlbodyhtml')
Esempio n. 6
0
 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')
Esempio n. 7
0
 def test_extra_large_1(self):
     self.assertEqual(get_xml_bones('<xml>%s</xml>' % ('A' * 30, )),
                      'htmlbodyxml40xmlbodyhtml')
Esempio n. 8
0
 def test_large(self):
     self.assertEqual(get_xml_bones('<xml>hello world 123 123</xml>'),
                      'htmlbodyxml20xmlbodyhtml')
Esempio n. 9
0
 def test_simple(self):
     self.assertEqual(get_xml_bones('<xml>hello</xml>'),
                      'htmlbodyxml0xmlbodyhtml')
Esempio n. 10
0
 def test_empty(self):
     self.assertEqual(get_xml_bones(''), '')