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