Skip to content

Commit e7defeb

Browse files
committed
[tests] use Flask as test server not python http.server
this will allow us to test more complex scenarios like HTTP error codes etc.
1 parent 8195e7a commit e7defeb

File tree

9 files changed

+58
-10
lines changed

9 files changed

+58
-10
lines changed

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ mock==1.3.0
55
pytest==2.9.1
66
pytest-cov==2.2.1
77
port-for==0.3.1
8+
Flask==1.1.1

tests/sample_data/testproject/testproject/spiders/testspider.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ class TestSpider(scrapy.Spider):
1111
def parse(self, response):
1212
name = response.xpath('//h1/text()').extract()
1313
return TestprojectItem(name=name)
14+
15+
def some_errback(self, err):
16+
self.logger.error("Logging some error {}".format(err))
17+
return

tests/servers.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# -*- coding: utf-8 -*-
2-
import six
3-
from subprocess import Popen, PIPE
4-
from six.moves.urllib.parse import urljoin
52
import fcntl
63
import os
74
import shutil
85
import socket
96
import sys
107
import tempfile
118
import time
9+
from subprocess import Popen, PIPE
1210

1311
import port_for
12+
from six.moves.urllib.parse import urljoin
1413

15-
from . import SAMPLE_DATA
14+
from . import TESTS_PATH
1615
from .utils import get_testenv, generate_project
1716

1817
DEVNULL = open(os.devnull, 'wb')
@@ -30,12 +29,9 @@ def __init__(self, host='localhost', port=None, cwd=None, shell=False,
3029
self.stdin = stdin
3130
self.stdout = stdout
3231
self.stderr = stderr
33-
if six.PY2:
34-
command = 'SimpleHTTPServer'
35-
else:
36-
command = 'http.server'
32+
3733
self.arguments = [
38-
sys.executable, '-u', '-m', command, str(self.port)
34+
'flask', 'run', '-p', str(self.port)
3935
]
4036

4137
def start(self):
@@ -126,4 +122,4 @@ class MockServer(BaseTestServer):
126122

127123
def __init__(self, *args, **kwargs):
128124
super(MockServer, self).__init__(*args, **kwargs)
129-
self.cwd = os.path.join(SAMPLE_DATA, 'testsite')
125+
self.cwd = os.path.join(TESTS_PATH, 'testsite')

tests/test_resource_crawl.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,16 @@ def test_invalid_json_in_post(self, server):
378378
assert res_json[k] == v
379379
msg = "Invalid JSON in POST body"
380380
assert msg in res_json['message']
381+
382+
@pytest.mark.parametrize("method", [
383+
perform_get, perform_post
384+
])
385+
def test_passing_errback(self, server, method):
386+
url = server.url("crawl.json")
387+
res = method(url,
388+
{"spider_name": "test"},
389+
{"url": server.target_site.url("page1.html"),
390+
'errback': 'some_errback',
391+
'callback': 'raise_some_error'})
392+
393+
res_json = res.json()

tests/testsite/app.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from flask import Flask
2+
3+
4+
app = Flask(__name__)
5+
6+
7+
def read_file(filename):
8+
with open(filename) as f:
9+
return f.read()
10+
11+
12+
@app.route('/')
13+
def base():
14+
return b'hello'
15+
16+
17+
@app.route('/index.html')
18+
def index():
19+
return read_file('index.html')
20+
21+
22+
@app.route('/page1.html')
23+
def page1():
24+
return read_file('page1.html')
25+
26+
27+
@app.route('/page2.html')
28+
def page2():
29+
return read_file('page2.html')
30+
31+
32+
@app.route('/page3.html')
33+
def page3():
34+
return read_file('page3.html')
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)