mirror of
https://github.com/scrapy/scrapy.git
synced 2025-03-14 06:48:31 +00:00
Remove backslash (tests)
This commit is contained in:
parent
9e99be982a
commit
9aea1f0961
@ -5,8 +5,7 @@ from gzip import GzipFile
|
|||||||
|
|
||||||
from scrapy.spiders import Spider
|
from scrapy.spiders import Spider
|
||||||
from scrapy.http import Response, Request, HtmlResponse
|
from scrapy.http import Response, Request, HtmlResponse
|
||||||
from scrapy.downloadermiddlewares.httpcompression import HttpCompressionMiddleware, \
|
from scrapy.downloadermiddlewares.httpcompression import HttpCompressionMiddleware, ACCEPTED_ENCODINGS
|
||||||
ACCEPTED_ENCODINGS
|
|
||||||
from scrapy.responsetypes import responsetypes
|
from scrapy.responsetypes import responsetypes
|
||||||
from scrapy.utils.gz import gunzip
|
from scrapy.utils.gz import gunzip
|
||||||
from tests import tests_datadir
|
from tests import tests_datadir
|
||||||
|
@ -77,12 +77,9 @@ class RedirectMiddlewareTest(unittest.TestCase):
|
|||||||
assert isinstance(req2, Request)
|
assert isinstance(req2, Request)
|
||||||
self.assertEqual(req2.url, url2)
|
self.assertEqual(req2.url, url2)
|
||||||
self.assertEqual(req2.method, 'GET')
|
self.assertEqual(req2.method, 'GET')
|
||||||
assert 'Content-Type' not in req2.headers, \
|
assert 'Content-Type' not in req2.headers, "Content-Type header must not be present in redirected request"
|
||||||
"Content-Type header must not be present in redirected request"
|
assert 'Content-Length' not in req2.headers, "Content-Length header must not be present in redirected request"
|
||||||
assert 'Content-Length' not in req2.headers, \
|
assert not req2.body, "Redirected body must be empty, not '%s'" % req2.body
|
||||||
"Content-Length header must not be present in redirected request"
|
|
||||||
assert not req2.body, \
|
|
||||||
"Redirected body must be empty, not '%s'" % req2.body
|
|
||||||
|
|
||||||
# response without Location header but with status code is 3XX should be ignored
|
# response without Location header but with status code is 3XX should be ignored
|
||||||
del rsp.headers['Location']
|
del rsp.headers['Location']
|
||||||
@ -244,12 +241,9 @@ class MetaRefreshMiddlewareTest(unittest.TestCase):
|
|||||||
assert isinstance(req2, Request)
|
assert isinstance(req2, Request)
|
||||||
self.assertEqual(req2.url, 'http://example.org/newpage')
|
self.assertEqual(req2.url, 'http://example.org/newpage')
|
||||||
self.assertEqual(req2.method, 'GET')
|
self.assertEqual(req2.method, 'GET')
|
||||||
assert 'Content-Type' not in req2.headers, \
|
assert 'Content-Type' not in req2.headers, "Content-Type header must not be present in redirected request"
|
||||||
"Content-Type header must not be present in redirected request"
|
assert 'Content-Length' not in req2.headers, "Content-Length header must not be present in redirected request"
|
||||||
assert 'Content-Length' not in req2.headers, \
|
assert not req2.body, "Redirected body must be empty, not '%s'" % req2.body
|
||||||
"Content-Length header must not be present in redirected request"
|
|
||||||
assert not req2.body, \
|
|
||||||
"Redirected body must be empty, not '%s'" % req2.body
|
|
||||||
|
|
||||||
def test_max_redirect_times(self):
|
def test_max_redirect_times(self):
|
||||||
self.mw.max_redirect_times = 1
|
self.mw.max_redirect_times = 1
|
||||||
|
@ -88,8 +88,7 @@ class SelectorTestCase(unittest.TestCase):
|
|||||||
"""Check that classes are using slots and are weak-referenceable"""
|
"""Check that classes are using slots and are weak-referenceable"""
|
||||||
x = Selector(text='')
|
x = Selector(text='')
|
||||||
weakref.ref(x)
|
weakref.ref(x)
|
||||||
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
|
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % x.__class__.__name__
|
||||||
x.__class__.__name__
|
|
||||||
|
|
||||||
def test_selector_bad_args(self):
|
def test_selector_bad_args(self):
|
||||||
with self.assertRaisesRegex(ValueError, 'received both response and text'):
|
with self.assertRaisesRegex(ValueError, 'received both response and text'):
|
||||||
|
@ -86,8 +86,7 @@ class BaseSettingsTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_set_calls_settings_attributes_methods_on_update(self):
|
def test_set_calls_settings_attributes_methods_on_update(self):
|
||||||
attr = SettingsAttribute('value', 10)
|
attr = SettingsAttribute('value', 10)
|
||||||
with mock.patch.object(attr, '__setattr__') as mock_setattr, \
|
with mock.patch.object(attr, '__setattr__') as mock_setattr, mock.patch.object(attr, 'set') as mock_set:
|
||||||
mock.patch.object(attr, 'set') as mock_set:
|
|
||||||
|
|
||||||
self.settings.attributes = {'TEST_OPTION': attr}
|
self.settings.attributes = {'TEST_OPTION': attr}
|
||||||
|
|
||||||
|
@ -11,8 +11,14 @@ from scrapy import signals
|
|||||||
from scrapy.settings import Settings
|
from scrapy.settings import Settings
|
||||||
from scrapy.http import Request, Response, TextResponse, XmlResponse, HtmlResponse
|
from scrapy.http import Request, Response, TextResponse, XmlResponse, HtmlResponse
|
||||||
from scrapy.spiders.init import InitSpider
|
from scrapy.spiders.init import InitSpider
|
||||||
from scrapy.spiders import Spider, CrawlSpider, Rule, XMLFeedSpider, \
|
from scrapy.spiders import (
|
||||||
CSVFeedSpider, SitemapSpider
|
CSVFeedSpider,
|
||||||
|
CrawlSpider,
|
||||||
|
Rule,
|
||||||
|
SitemapSpider,
|
||||||
|
Spider,
|
||||||
|
XMLFeedSpider,
|
||||||
|
)
|
||||||
from scrapy.linkextractors import LinkExtractor
|
from scrapy.linkextractors import LinkExtractor
|
||||||
from scrapy.exceptions import ScrapyDeprecationWarning
|
from scrapy.exceptions import ScrapyDeprecationWarning
|
||||||
from scrapy.utils.test import get_crawler
|
from scrapy.utils.test import get_crawler
|
||||||
|
@ -6,16 +6,28 @@ from scrapy.http import Response, Request
|
|||||||
from scrapy.settings import Settings
|
from scrapy.settings import Settings
|
||||||
from scrapy.spiders import Spider
|
from scrapy.spiders import Spider
|
||||||
from scrapy.downloadermiddlewares.redirect import RedirectMiddleware
|
from scrapy.downloadermiddlewares.redirect import RedirectMiddleware
|
||||||
from scrapy.spidermiddlewares.referer import RefererMiddleware, \
|
from scrapy.spidermiddlewares.referer import (
|
||||||
POLICY_NO_REFERRER, POLICY_NO_REFERRER_WHEN_DOWNGRADE, \
|
DefaultReferrerPolicy,
|
||||||
POLICY_SAME_ORIGIN, POLICY_ORIGIN, POLICY_ORIGIN_WHEN_CROSS_ORIGIN, \
|
NoReferrerPolicy,
|
||||||
POLICY_SCRAPY_DEFAULT, POLICY_UNSAFE_URL, \
|
NoReferrerWhenDowngradePolicy,
|
||||||
POLICY_STRICT_ORIGIN, POLICY_STRICT_ORIGIN_WHEN_CROSS_ORIGIN, \
|
OriginPolicy,
|
||||||
DefaultReferrerPolicy, \
|
OriginWhenCrossOriginPolicy,
|
||||||
NoReferrerPolicy, NoReferrerWhenDowngradePolicy, \
|
POLICY_NO_REFERRER,
|
||||||
OriginWhenCrossOriginPolicy, OriginPolicy, \
|
POLICY_NO_REFERRER_WHEN_DOWNGRADE,
|
||||||
StrictOriginWhenCrossOriginPolicy, StrictOriginPolicy, \
|
POLICY_ORIGIN,
|
||||||
SameOriginPolicy, UnsafeUrlPolicy, ReferrerPolicy
|
POLICY_ORIGIN_WHEN_CROSS_ORIGIN,
|
||||||
|
POLICY_SAME_ORIGIN,
|
||||||
|
POLICY_SCRAPY_DEFAULT,
|
||||||
|
POLICY_STRICT_ORIGIN,
|
||||||
|
POLICY_STRICT_ORIGIN_WHEN_CROSS_ORIGIN,
|
||||||
|
POLICY_UNSAFE_URL,
|
||||||
|
RefererMiddleware,
|
||||||
|
ReferrerPolicy,
|
||||||
|
SameOriginPolicy,
|
||||||
|
StrictOriginPolicy,
|
||||||
|
StrictOriginWhenCrossOriginPolicy,
|
||||||
|
UnsafeUrlPolicy,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestRefererMiddleware(TestCase):
|
class TestRefererMiddleware(TestCase):
|
||||||
|
@ -29,8 +29,7 @@ class CurlToRequestKwargsTest(unittest.TestCase):
|
|||||||
self._test_command(curl_command, expected_result)
|
self._test_command(curl_command, expected_result)
|
||||||
|
|
||||||
def test_get_basic_auth(self):
|
def test_get_basic_auth(self):
|
||||||
curl_command = 'curl "https://api.test.com/" -u ' \
|
curl_command = 'curl "https://api.test.com/" -u "some_username:some_password"'
|
||||||
'"some_username:some_password"'
|
|
||||||
expected_result = {
|
expected_result = {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"url": "https://api.test.com/",
|
"url": "https://api.test.com/",
|
||||||
@ -212,8 +211,7 @@ class CurlToRequestKwargsTest(unittest.TestCase):
|
|||||||
with warnings.catch_warnings(): # avoid warning when executing tests
|
with warnings.catch_warnings(): # avoid warning when executing tests
|
||||||
warnings.simplefilter('ignore')
|
warnings.simplefilter('ignore')
|
||||||
curl_command = 'curl --bar --baz http://www.example.com'
|
curl_command = 'curl --bar --baz http://www.example.com'
|
||||||
expected_result = \
|
expected_result = {"method": "GET", "url": "http://www.example.com"}
|
||||||
{"method": "GET", "url": "http://www.example.com"}
|
|
||||||
self.assertEqual(curl_to_request_kwargs(curl_command), expected_result)
|
self.assertEqual(curl_to_request_kwargs(curl_command), expected_result)
|
||||||
|
|
||||||
# case 2: ignore_unknown_options=False (raise exception):
|
# case 2: ignore_unknown_options=False (raise exception):
|
||||||
|
@ -2,8 +2,13 @@ from twisted.trial import unittest
|
|||||||
from twisted.internet import reactor, defer
|
from twisted.internet import reactor, defer
|
||||||
from twisted.python.failure import Failure
|
from twisted.python.failure import Failure
|
||||||
|
|
||||||
from scrapy.utils.defer import mustbe_deferred, process_chain, \
|
from scrapy.utils.defer import (
|
||||||
process_chain_both, process_parallel, iter_errback
|
iter_errback,
|
||||||
|
mustbe_deferred,
|
||||||
|
process_chain,
|
||||||
|
process_chain_both,
|
||||||
|
process_parallel,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class MustbeDeferredTest(unittest.TestCase):
|
class MustbeDeferredTest(unittest.TestCase):
|
||||||
|
@ -15,18 +15,20 @@ class XmliterTestCase(unittest.TestCase):
|
|||||||
xmliter = staticmethod(xmliter)
|
xmliter = staticmethod(xmliter)
|
||||||
|
|
||||||
def test_xmliter(self):
|
def test_xmliter(self):
|
||||||
body = b"""<?xml version="1.0" encoding="UTF-8"?>\
|
body = b"""
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:noNamespaceSchemaLocation="someschmea.xsd">\
|
xsi:noNamespaceSchemaLocation="someschmea.xsd">
|
||||||
<product id="001">\
|
<product id="001">
|
||||||
<type>Type 1</type>\
|
<type>Type 1</type>
|
||||||
<name>Name 1</name>\
|
<name>Name 1</name>
|
||||||
</product>\
|
</product>
|
||||||
<product id="002">\
|
<product id="002">
|
||||||
<type>Type 2</type>\
|
<type>Type 2</type>
|
||||||
<name>Name 2</name>\
|
<name>Name 2</name>
|
||||||
</product>\
|
</product>
|
||||||
</products>"""
|
</products>
|
||||||
|
"""
|
||||||
|
|
||||||
response = XmlResponse(url="http://example.com", body=body)
|
response = XmlResponse(url="http://example.com", body=body)
|
||||||
attrs = []
|
attrs = []
|
||||||
@ -115,7 +117,7 @@ class XmliterTestCase(unittest.TestCase):
|
|||||||
[[u'one'], [u'two']])
|
[[u'one'], [u'two']])
|
||||||
|
|
||||||
def test_xmliter_namespaces(self):
|
def test_xmliter_namespaces(self):
|
||||||
body = b"""\
|
body = b"""
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
|
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
|
||||||
<channel>
|
<channel>
|
||||||
@ -185,7 +187,7 @@ class LxmlXmliterTestCase(XmliterTestCase):
|
|||||||
xmliter = staticmethod(xmliter_lxml)
|
xmliter = staticmethod(xmliter_lxml)
|
||||||
|
|
||||||
def test_xmliter_iterate_namespace(self):
|
def test_xmliter_iterate_namespace(self):
|
||||||
body = b"""\
|
body = b"""
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<rss version="2.0" xmlns="http://base.google.com/ns/1.0">
|
<rss version="2.0" xmlns="http://base.google.com/ns/1.0">
|
||||||
<channel>
|
<channel>
|
||||||
@ -214,7 +216,7 @@ class LxmlXmliterTestCase(XmliterTestCase):
|
|||||||
self.assertEqual(node.xpath('text()').getall(), ['http://www.mydummycompany.com/images/item2.jpg'])
|
self.assertEqual(node.xpath('text()').getall(), ['http://www.mydummycompany.com/images/item2.jpg'])
|
||||||
|
|
||||||
def test_xmliter_namespaces_prefix(self):
|
def test_xmliter_namespaces_prefix(self):
|
||||||
body = b"""\
|
body = b"""
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<root>
|
<root>
|
||||||
<h:table xmlns:h="http://www.w3.org/TR/html4/">
|
<h:table xmlns:h="http://www.w3.org/TR/html4/">
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
import unittest
|
import unittest
|
||||||
from scrapy.http import Request
|
from scrapy.http import Request
|
||||||
from scrapy.utils.request import request_fingerprint, _fingerprint_cache, \
|
from scrapy.utils.request import (
|
||||||
request_authenticate, request_httprepr
|
_fingerprint_cache,
|
||||||
|
request_authenticate,
|
||||||
|
request_fingerprint,
|
||||||
|
request_httprepr,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class UtilsRequestTest(unittest.TestCase):
|
class UtilsRequestTest(unittest.TestCase):
|
||||||
|
@ -37,8 +37,7 @@ class ResponseUtilsTest(unittest.TestCase):
|
|||||||
self.assertIn(b'<base href="' + to_bytes(url) + b'">', bbody)
|
self.assertIn(b'<base href="' + to_bytes(url) + b'">', bbody)
|
||||||
return True
|
return True
|
||||||
response = HtmlResponse(url, body=body)
|
response = HtmlResponse(url, body=body)
|
||||||
assert open_in_browser(response, _openfunc=browser_open), \
|
assert open_in_browser(response, _openfunc=browser_open), "Browser not called"
|
||||||
"Browser not called"
|
|
||||||
|
|
||||||
resp = Response(url, body=body)
|
resp = Response(url, body=body)
|
||||||
self.assertRaises(TypeError, open_in_browser, resp, debug=True)
|
self.assertRaises(TypeError, open_in_browser, resp, debug=True)
|
||||||
|
@ -156,8 +156,7 @@ Disallow: /forum/active/
|
|||||||
|
|
||||||
def test_sitemap_blanklines(self):
|
def test_sitemap_blanklines(self):
|
||||||
"""Assert we can deal with starting blank lines before <xml> tag"""
|
"""Assert we can deal with starting blank lines before <xml> tag"""
|
||||||
s = Sitemap(b"""\
|
s = Sitemap(b"""
|
||||||
|
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
|
|
||||||
|
@ -207,8 +207,7 @@ def create_guess_scheme_t(args):
|
|||||||
def do_expected(self):
|
def do_expected(self):
|
||||||
url = guess_scheme(args[0])
|
url = guess_scheme(args[0])
|
||||||
assert url.startswith(args[1]), \
|
assert url.startswith(args[1]), \
|
||||||
'Wrong scheme guessed: for `%s` got `%s`, expected `%s...`' % (
|
'Wrong scheme guessed: for `%s` got `%s`, expected `%s...`' % (args[0], url, args[1])
|
||||||
args[0], url, args[1])
|
|
||||||
return do_expected
|
return do_expected
|
||||||
|
|
||||||
|
|
||||||
|
@ -356,9 +356,8 @@ class WebClientTestCase(unittest.TestCase):
|
|||||||
""" Test that non-standart body encoding matches
|
""" Test that non-standart body encoding matches
|
||||||
Content-Encoding header """
|
Content-Encoding header """
|
||||||
body = b'\xd0\x81\xd1\x8e\xd0\xaf'
|
body = b'\xd0\x81\xd1\x8e\xd0\xaf'
|
||||||
return getPage(
|
dfd = getPage(self.getURL('encoding'), body=body, response_transform=lambda r: r)
|
||||||
self.getURL('encoding'), body=body, response_transform=lambda r: r)\
|
return dfd.addCallback(self._check_Encoding, body)
|
||||||
.addCallback(self._check_Encoding, body)
|
|
||||||
|
|
||||||
def _check_Encoding(self, response, original_body):
|
def _check_Encoding(self, response, original_body):
|
||||||
content_encoding = to_unicode(response.headers[b'Content-Encoding'])
|
content_encoding = to_unicode(response.headers[b'Content-Encoding'])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user