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