Skip to content

Commit

Permalink
Improve test coverage for the splash spider attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Aug 12, 2019
1 parent 03aaab9 commit 47f162d
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,21 +769,55 @@ def test_adjust_timeout():

def test_spider_attribute():
req_url = "http://scrapy.org"
req1 = scrapy.Request(req_url)

spider = scrapy.Spider("example")
mw = _get_mw()

req1 = scrapy.Request(req_url)
spider.splash = {"args": {"images": 0}}

mw = _get_mw()
req1 = mw.process_request(req1, spider)
assert "_splash_processed" in req1.meta
assert "render.json" in req1.url
assert "url" in json.loads(req1.body)
assert json.loads(req1.body).get("url") == req_url
assert "images" in json.loads(req1.body)
request_data = json.loads(req1.body.decode('utf8'))
assert "url" in request_data
assert request_data.get("url") == req_url
assert "images" in request_data
assert req1.method == 'POST'

# spider attribute blank middleware disabled
spider.splash = {}
req3 = mw.process_request(req1, spider)
assert req3 is None


def test_spider_attribute_dont_splash():
req_url = "http://scrapy.org"
spider = scrapy.Spider("example")
mw = _get_mw()

req1 = scrapy.Request(req_url, meta={'dont_splash': True})
spider.splash = {"args": {"images": 0}}

req2 = mw.process_request(req1, spider)
assert req2 is None

response = Response("http://example.com", request=req1)
response2 = mw.process_response(req1, response, None)
assert response2 is response


def test_spider_attribute_blank():
req_url = "http://scrapy.org"
spider = scrapy.Spider("example")
mw = _get_mw()

req1 = scrapy.Request(req_url)
spider.splash = {}

req2 = mw.process_request(req1, spider)
assert req2 is None

response = Response("http://example.com", request=req1)
response2 = mw.process_response(req1, response, None)
assert response2 is response

0 comments on commit 47f162d

Please sign in to comment.