1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-25 03:43:58 +00:00

cookies: now really add unittests

--HG--
extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%401079
This commit is contained in:
Daniel Grana 2009-04-21 13:33:42 +00:00
parent 1ce1481eab
commit 4a6f7af739
2 changed files with 1221 additions and 0 deletions

View File

@ -0,0 +1,89 @@
from __future__ import with_statement
from unittest import TestCase
from scrapy.spider import spiders
from scrapy.core.exceptions import HttpException
from scrapy.http import Response, Request
from scrapy.contrib_exp.downloadermiddleware.cookies import CookiesMiddleware
class CookiesMiddlewareTest(TestCase):
def setUp(self):
spiders.spider_modules = ['scrapy.tests.test_spiders']
spiders.reload()
self.spider = spiders.fromdomain('scrapytest.org')
self.mw = CookiesMiddleware()
def tearDown(self):
self.mw.domain_closed('scrapytest.org')
del self.mw
def test_basic(self):
headers = {'Set-Cookie': 'C1=value1; path=/'}
req = Request('http://scrapytest.org/')
self.mw.process_request(req, self.spider)
assert 'Cookie' not in req.headers
res = Response('http://scrapytest.org/', headers=headers)
self.mw.process_response(req, res, self.spider)
#assert res.cookies
req2 = Request('http://scrapytest.org/sub1/')
self.mw.process_request(req2, self.spider)
self.assertEquals(req2.headers.get('Cookie'), "C1=value1")
def test_http_exception(self):
headers = {'Set-Cookie': 'C1=value1; path=/'}
res = Response('http://scrapytest.org/', headers=headers)
exc = HttpException(302, 'Redirect', res)
req = Request('http://scrapytest.org/')
self.mw.process_request(req, self.spider)
assert 'Cookie' not in req.headers
self.mw.process_exception(req, exc, self.spider)
#assert exc.response.cookies
req2 = Request('http://scrapytest.org/sub1/')
self.mw.process_request(req2, self.spider)
self.assertEquals(req2.headers.get('Cookie'), "C1=value1")
def test_dont_merge_cookies(self):
# merge some cookies into jar
headers = {'Set-Cookie': 'C1=value1; path=/'}
req = Request('http://scrapytest.org/')
res = Response('http://scrapytest.org/', headers=headers)
self.mw.process_response(req, res, self.spider)
# test Cookie header is not seted to request
req = Request('http://scrapytest.org/dontmerge', meta={'dont_merge_cookies': 1})
self.mw.process_request(req, self.spider)
assert 'Cookie' not in req.headers
# check that returned cookies are not merged back to jar
res = Response('http://scrapytest.org/dontmerge', headers={'Set-Cookie': 'dont=mergeme; path=/'})
self.mw.process_response(req, res, self.spider)
req = Request('http://scrapytest.org/mergeme')
self.mw.process_request(req, self.spider)
self.assertEquals(req.headers.get('Cookie'), 'C1=value1')
def test_merge_request_cookies(self):
headers = {'Set-Cookie': 'C1=value1; path=/'}
req = Request('http://scrapytest.org/', cookies={'galleta': 'salada'})
self.mw.process_request(req, self.spider)
self.assertEquals(req.headers.get('Cookie'), 'galleta=salada')
res = Response('http://scrapytest.org/', headers=headers)
self.mw.process_response(req, res, self.spider)
req2 = Request('http://scrapytest.org/sub1/')
self.mw.process_request(req2, self.spider)
self.assertEquals(req2.headers.get('Cookie'), "C1=value1")

File diff suppressed because it is too large Load Diff