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 40961cb
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
import copy
import json
Expand Down Expand Up @@ -769,21 +770,54 @@ def test_adjust_timeout():

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

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

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

response = Response(req_url, request=req2)
response2 = mw.process_response(req2, response, spider)
assert response2 is not response


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(req_url, request=req1)
response2 = mw.process_response(req1, response, spider)
assert response2 is response


def test_spider_attribute_blank():
req_url = "http://scrapy.org"
spider = scrapy.Spider("example")
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)
assert req1.method == 'POST'

# spider attribute blank middleware disabled

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

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

response = Response(req_url, request=req1)
response2 = mw.process_response(req1, response, spider)
assert response2 is response

0 comments on commit 40961cb

Please sign in to comment.