コード例 #1
0
    def test_replace(self):
        r1 = Request('http://www.example.com', method='GET')
        headers = Headers(dict(r1.headers, key='value'))
        r2 = r1.replace(method='POST', body='New body', headers=headers)
        self.assertEqual(r1.url, r2.url)
        self.assertEqual((r1.method, r2.method), ('GET', 'POST'))
        self.assertEqual((r1.body, r2.body), ('', 'New body'))
        self.assertEqual((r1.headers, r2.headers), (Headers(), headers))

        r3 = Request('http://www.example.com', meta={'a': 1})
        r4 = r3.replace(url='http://www.example.com/2', body='', meta={})
        self.assertEqual(r4.url, 'http://www.example.com/2')
        self.assertEqual(r4.body, '')
        self.assertEqual(r4.meta, {})
コード例 #2
0
 def test_request_cacheability(self):
     res0 = Response(self.request.url, status=200,
                     headers={'Expires': self.tomorrow})
     req0 = Request('http://example.com')
     req1 = req0.replace(headers={'Cache-Control': 'no-store'})
     req2 = req0.replace(headers={'Cache-Control': 'no-cache'})
     with self._middleware() as mw:
         # response for a request with no-store must not be cached
         res1 = self._process_requestresponse(mw, req1, res0)
         self.assertEqualResponse(res1, res0)
         self.assertIsNone(mw.storage.retrieve_response(req1))
         # Re-do request without no-store and expect it to be cached
         res2 = self._process_requestresponse(mw, req0, res0)
         self.assertNotIn('cached', res2.flags)
         res3 = mw.process_request(req0)
         self.assertIn('cached', res3.flags)
         self.assertEqualResponse(res2, res3)
         # request with no-cache directive must not return cached response
         # but it allows new response to be stored
         res0b = res0.replace(body='foo')
         res4 = self._process_requestresponse(mw, req2, res0b)
         self.assertEqualResponse(res4, res0b)
         self.assertNotIn('cached', res4.flags)
         res5 = self._process_requestresponse(mw, req0, None)
         self.assertEqualResponse(res5, res0b)
         self.assertIn('cached', res5.flags)
コード例 #3
0
    def test_request_fingerprint(self):
        r1 = Request('http://www.example.com/query?id=111&cat=222')
        r2 = Request('http://www.example.com/query?cat=222&id=111')
        self.assertEqual(request_fingerprint(r1), request_fingerprint(r1))
        self.assertEqual(request_fingerprint(r1), request_fingerprint(r2))

        r1 = Request('http://www.example.com/hnnoticiaj1.aspx?78132,199')
        r2 = Request('http://www.example.com/hnnoticiaj1.aspx?78160,199')
        self.assertNotEqual(request_fingerprint(r1), request_fingerprint(r2))

        # make sure caching is working
        self.assertEqual(request_fingerprint(r1), _fingerprint_cache[r1][None])

        r1 = Request('http://www.example.com/members/offers.html')
        r2 = Request('http://www.example.com/members/offers.html')
        r2.headers['SESSIONID'] = 'somehash'
        self.assertEqual(request_fingerprint(r1), request_fingerprint(r2))

        r1 = Request('http://www.example.com/')
        r2 = Request('http://www.example.com/')
        r2.headers['Accept-Language'] = 'en'
        r3 = Request('http://www.example.com/')
        r3.headers['Accept-Language'] = 'en'
        r3.headers['SESSIONID'] = 'somehash'

        self.assertEqual(request_fingerprint(r1), request_fingerprint(r2),
                         request_fingerprint(r3))

        self.assertEqual(
            request_fingerprint(r1),
            request_fingerprint(r1, include_headers=['Accept-Language']))

        self.assertNotEqual(
            request_fingerprint(r1),
            request_fingerprint(r2, include_headers=['Accept-Language']))

        self.assertEqual(
            request_fingerprint(
                r3, include_headers=['accept-language', 'sessionid']),
            request_fingerprint(
                r3, include_headers=['SESSIONID', 'Accept-Language']))

        r1 = Request('http://www.example.com')
        r2 = Request('http://www.example.com', method='POST')
        r3 = Request('http://www.example.com',
                     method='POST',
                     body='request body')

        self.assertNotEqual(request_fingerprint(r1), request_fingerprint(r2))
        self.assertNotEqual(request_fingerprint(r2), request_fingerprint(r3))

        # cached fingerprint must be cleared on request copy
        r1 = Request('http://www.example.com')
        fp1 = request_fingerprint(r1)
        r2 = r1.replace(url='http://www.example.com/other')
        fp2 = request_fingerprint(r2)
        self.assertNotEqual(fp1, fp2)
コード例 #4
0
    def test_request_fingerprint(self):
        r1 = Request('http://www.example.com/query?id=111&cat=222')
        r2 = Request('http://www.example.com/query?cat=222&id=111')
        self.assertEqual(request_fingerprint(r1), request_fingerprint(r1))
        self.assertEqual(request_fingerprint(r1), request_fingerprint(r2))

        r1 = Request('http://www.example.com/hnnoticiaj1.aspx?78132,199')
        r2 = Request('http://www.example.com/hnnoticiaj1.aspx?78160,199')
        self.assertNotEqual(request_fingerprint(r1), request_fingerprint(r2))

        # make sure caching is working
        self.assertEqual(request_fingerprint(r1), _fingerprint_cache[r1][None])

        r1 = Request('http://www.example.com/members/offers.html')
        r2 = Request('http://www.example.com/members/offers.html')
        r2.headers['SESSIONID'] = 'somehash'
        self.assertEqual(request_fingerprint(r1), request_fingerprint(r2))

        r1 = Request('http://www.example.com/')
        r2 = Request('http://www.example.com/')
        r2.headers['Accept-Language'] = 'en'
        r3 = Request('http://www.example.com/')
        r3.headers['Accept-Language'] = 'en'
        r3.headers['SESSIONID'] = 'somehash'

        self.assertEqual(request_fingerprint(r1), request_fingerprint(r2), request_fingerprint(r3))

        self.assertEqual(request_fingerprint(r1),
                         request_fingerprint(r1, include_headers=['Accept-Language']))

        self.assertNotEqual(request_fingerprint(r1),
            request_fingerprint(r2, include_headers=['Accept-Language']))

        self.assertEqual(request_fingerprint(r3, include_headers=['accept-language', 'sessionid']),
                         request_fingerprint(r3, include_headers=['SESSIONID', 'Accept-Language']))

        r1 = Request('http://www.example.com')
        r2 = Request('http://www.example.com', method='POST')
        r3 = Request('http://www.example.com', method='POST', body='request body')

        self.assertNotEqual(request_fingerprint(r1), request_fingerprint(r2))
        self.assertNotEqual(request_fingerprint(r2), request_fingerprint(r3))

        # cached fingerprint must be cleared on request copy
        r1 = Request('http://www.example.com')
        fp1 = request_fingerprint(r1)
        r2 = r1.replace(url = 'http://www.example.com/other')
        fp2 = request_fingerprint(r2)
        self.assertNotEqual(fp1, fp2)