1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- encoding: utf-8 -*- |
3
|
|
|
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: |
4
|
|
|
# Author: Binux<[email protected]> |
5
|
|
|
# http://binux.me |
6
|
|
|
# Created on 2015-01-18 11:10:27 |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
import os |
10
|
|
|
import copy |
11
|
|
|
import time |
12
|
|
|
import httpbin |
13
|
|
|
import unittest2 as unittest |
14
|
|
|
|
15
|
|
|
import logging |
16
|
|
|
import logging.config |
17
|
|
|
logging.config.fileConfig("pyspider/logging.conf") |
18
|
|
|
|
19
|
|
|
from pyspider.libs import utils |
20
|
|
|
from pyspider.libs.response import rebuild_response |
21
|
|
|
from pyspider.fetcher.tornado_fetcher import Fetcher |
22
|
|
|
|
23
|
|
|
class TestResponse(unittest.TestCase): |
24
|
|
|
sample_task_http = { |
25
|
|
|
'taskid': 'taskid', |
26
|
|
|
'project': 'project', |
27
|
|
|
'url': '', |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
@classmethod |
31
|
|
|
def setUpClass(self): |
32
|
|
|
self.fetcher = Fetcher(None, None, async=False) |
33
|
|
|
self.httpbin_thread = utils.run_in_subprocess(httpbin.app.run, port=14887, passthrough_errors=False) |
34
|
|
|
self.httpbin = 'http://127.0.0.1:14887' |
35
|
|
|
time.sleep(0.5) |
36
|
|
|
|
37
|
|
|
@classmethod |
38
|
|
|
def tearDownClass(self): |
39
|
|
|
self.httpbin_thread.terminate() |
40
|
|
|
|
41
|
|
|
def get(self, url, **kwargs): |
42
|
|
|
if not url.startswith('http://'): |
43
|
|
|
url = self.httpbin + url |
44
|
|
|
request = copy.deepcopy(self.sample_task_http) |
45
|
|
|
request['url'] = url |
46
|
|
|
request.update(kwargs) |
47
|
|
|
result = self.fetcher.fetch(request) |
48
|
|
|
response = rebuild_response(result) |
49
|
|
|
return response |
50
|
|
|
|
51
|
|
|
def test_10_html(self): |
52
|
|
|
response = self.get('/html') |
53
|
|
|
self.assertEqual(response.status_code, 200) |
54
|
|
|
self.assertIsNotNone(response.doc('h1')) |
55
|
|
|
|
56
|
|
|
def test_20_xml(self): |
57
|
|
|
response = self.get('/xml') |
58
|
|
|
self.assertEqual(response.status_code, 200) |
59
|
|
|
self.assertIsNotNone(response.doc('item')) |
60
|
|
|
|
61
|
|
|
def test_30_gzip(self): |
62
|
|
|
response = self.get('/gzip') |
63
|
|
|
self.assertEqual(response.status_code, 200) |
64
|
|
|
self.assertIn('gzipped', response.text) |
65
|
|
|
|
66
|
|
|
def test_40_deflate(self): |
67
|
|
|
response = self.get('/deflate') |
68
|
|
|
self.assertEqual(response.status_code, 200) |
69
|
|
|
self.assertIn('deflated', response.text) |
70
|
|
|
|
71
|
|
|
def test_50_ok(self): |
72
|
|
|
response = self.get('/status/200') |
73
|
|
|
self.assertTrue(response.ok) |
74
|
|
|
self.assertTrue(response) |
75
|
|
|
response = self.get('/status/302') |
76
|
|
|
self.assertTrue(response.ok) |
77
|
|
|
self.assertTrue(response) |
78
|
|
|
with self.assertRaises(Exception): |
79
|
|
|
self.raise_for_status(allow_redirects=False) |
80
|
|
|
|
81
|
|
|
def test_60_not_ok(self): |
82
|
|
|
response = self.get('/status/400') |
83
|
|
|
self.assertFalse(response.ok) |
84
|
|
|
self.assertFalse(response) |
85
|
|
|
response = self.get('/status/500') |
86
|
|
|
self.assertFalse(response.ok) |
87
|
|
|
self.assertFalse(response) |
88
|
|
|
response = self.get('/status/600') |
89
|
|
|
self.assertFalse(response.ok) |
90
|
|
|
self.assertFalse(response) |
91
|
|
|
|
92
|
|
|
def test_70_reraise_exception(self): |
93
|
|
|
response = self.get('file://abc') |
94
|
|
|
with self.assertRaisesRegexp(Exception, 'HTTP 599'): |
95
|
|
|
response.raise_for_status() |
96
|
|
|
|