Skip to content

Commit 6c1783e

Browse files
authored
Merge pull request #156 from scrapy-plugins/more-tests
more tests, fix cookie handling
2 parents aa07ba0 + 38710f6 commit 6c1783e

File tree

9 files changed

+425
-32
lines changed

9 files changed

+425
-32
lines changed

.travis.yml

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
language: python
2-
sudo: false
2+
sudo: required
3+
4+
services:
5+
- docker
36

47
branches:
58
only:
69
- master
710
- /^\d\.\d+$/
11+
812
matrix:
9-
include:
10-
- python: 2.7
11-
env: TOXENV=py27
12-
- python: 3.4
13-
env: TOXENV=py34
14-
- python: 3.5
15-
env: TOXENV=py35
16-
- python: 3.6
17-
env: TOXENV=py36
18-
- python: 2.7
19-
env: TOXENV=py27-scrapy10
13+
include:
14+
- python: 2.7
15+
env: TOXENV=py27
16+
- python: 3.4
17+
env: TOXENV=py34
18+
- python: 3.5
19+
env: TOXENV=py35
20+
- python: 3.6
21+
env: TOXENV=py36
22+
- python: 2.7
23+
env: TOXENV=py27-scrapy11
24+
25+
before_install:
26+
- docker pull scrapinghub/splash
27+
- docker run --rm -d -p 8050:8050 --network host scrapinghub/splash
2028

2129
install:
2230
- pip install -U tox codecov
2331

24-
script: tox
32+
script: SPLASH_URL=http://127.0.0.1:8050 tox
2533

2634
after_success:
2735
- codecov

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,9 @@ https://github.com/scrapy-plugins/scrapy-splash
679679

680680
To run tests, install "tox" Python package and then run ``tox`` command
681681
from the source checkout.
682+
683+
To run integration tests, start Splash and set SPLASH_URL env variable
684+
to Splash address before running ``tox`` command::
685+
686+
docker run -d --rm -p8050:8050 scrapinghub/splash:3.0
687+
SPLASH_URL=http://127.0.0.1:8050 tox -e py36

requirements-test.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pytest >= 3.3.2
2+
pytest-cov >= 2.5.1
3+
pytest-twisted >= 1.6
4+
hypothesis >= 3.44.14
5+
hypothesis-pytest

scrapy_splash/middleware.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ class SlotPolicy(object):
3838

3939
class SplashCookiesMiddleware(object):
4040
"""
41-
This middleware maintains cookiejars for Splash requests.
41+
This downloader middleware maintains cookiejars for Splash requests.
4242
4343
It gets cookies from 'cookies' field in Splash JSON responses
44-
and sends current cookies in 'cookies' JSON POST argument.
44+
and sends current cookies in 'cookies' JSON POST argument instead of
45+
sending them in http headers.
4546
4647
It should process requests before SplashMiddleware, and process responses
4748
after SplashMiddleware.
@@ -57,12 +58,14 @@ def from_crawler(cls, crawler):
5758
def process_request(self, request, spider):
5859
"""
5960
For Splash requests add 'cookies' key with current
60-
cookies to request.meta['splash']['args']
61+
cookies to ``request.meta['splash']['args']`` and remove cookie
62+
headers sent to Splash itself.
6163
"""
6264
if 'splash' not in request.meta:
6365
return
6466

6567
if request.meta.get('_splash_processed'):
68+
request.headers.pop('Cookie', None)
6669
return
6770

6871
splash_options = request.meta['splash']

tests/conftest.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import os
2+
3+
import pytest
4+
from scrapy.settings import Settings
5+
6+
7+
@pytest.fixture()
8+
def settings(request):
9+
""" Default scrapy-splash settings """
10+
s = dict(
11+
# collect scraped items to .collected_items attribute
12+
ITEM_PIPELINES={
13+
'tests.utils.CollectorPipeline': 100,
14+
},
15+
16+
# scrapy-splash settings
17+
SPLASH_URL=os.environ.get('SPLASH_URL'),
18+
DOWNLOADER_MIDDLEWARES={
19+
# Engine side
20+
'scrapy_splash.SplashCookiesMiddleware': 723,
21+
'scrapy_splash.SplashMiddleware': 725,
22+
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
23+
# Downloader side
24+
},
25+
SPIDER_MIDDLEWARES={
26+
'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
27+
},
28+
DUPEFILTER_CLASS='scrapy_splash.SplashAwareDupeFilter',
29+
HTTPCACHE_STORAGE='scrapy_splash.SplashAwareFSCacheStorage',
30+
)
31+
return Settings(s)
32+
33+

tests/mockserver.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python
2+
import argparse, socket, sys, time
3+
from subprocess import Popen, PIPE
4+
from importlib import import_module
5+
6+
from twisted.internet import reactor
7+
from twisted.web.server import Site
8+
9+
10+
def get_ephemeral_port():
11+
s = socket.socket()
12+
s.bind(("", 0))
13+
return s.getsockname()[1]
14+
15+
16+
class MockServer():
17+
def __init__(self, resource, port=None):
18+
self.resource = '{}.{}'.format(resource.__module__, resource.__name__)
19+
self.proc = None
20+
host = socket.gethostbyname(socket.gethostname())
21+
self.port = port or get_ephemeral_port()
22+
self.root_url = 'http://%s:%d' % (host, self.port)
23+
24+
def __enter__(self):
25+
self.proc = Popen(
26+
[sys.executable, '-u', '-m', 'tests.mockserver',
27+
self.resource, '--port', str(self.port)],
28+
stdout=PIPE)
29+
self.proc.stdout.readline()
30+
return self
31+
32+
def __exit__(self, exc_type, exc_value, traceback):
33+
self.proc.kill()
34+
self.proc.wait()
35+
time.sleep(0.2)
36+
37+
38+
def main():
39+
parser = argparse.ArgumentParser()
40+
parser.add_argument('resource')
41+
parser.add_argument('--port', type=int)
42+
args = parser.parse_args()
43+
module_name, name = args.resource.rsplit('.', 1)
44+
sys.path.append('.')
45+
resource = getattr(import_module(module_name), name)()
46+
http_port = reactor.listenTCP(args.port, Site(resource))
47+
def print_listening():
48+
host = http_port.getHost()
49+
print('Mock server {} running at http://{}:{}'.format(
50+
resource, host.host, host.port))
51+
reactor.callWhenRunning(print_listening)
52+
reactor.run()
53+
54+
55+
if __name__ == "__main__":
56+
main()

0 commit comments

Comments
 (0)