-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtests.py
186 lines (155 loc) · 5.76 KB
/
tests.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
import os
import pycurl
import shutil
from unittest import TestCase
from homura import download, get_resource_name, utf8_encode
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
TEST_DATA_DIR = os.path.join(PROJECT_PATH, 'test_data')
TEST_DATA_SUBDIR = os.path.join(TEST_DATA_DIR, 'sub')
TEST_DATA_ASCII = TEST_DATA_SUBDIR
TEST_DATA_UNICODE = os.path.join(TEST_DATA_DIR, u'下载')
TEST_DATA_UTF8 = utf8_encode(os.path.join(TEST_DATA_DIR, u'离线'))
SUBDIR_RELPATH = os.path.basename(TEST_DATA_SUBDIR)
FILE_SMALL = 'http://download.thinkbroadband.com/MD5SUMS'
FILE_1MB = 'http://download.thinkbroadband.com/1MB.zip'
FILE_5MB = 'http://download.thinkbroadband.com/5MB.zip'
FILE_301_SMALL = 'https://dev.moleculea.com/homura/301/SMD5SUMS'
FILE_301_1MB = 'https://dev.moleculea.com/homura/301/S1MB.zip'
FILE_301_5MB = 'https://dev.moleculea.com/homura/301/S5MB.zip'
FILE_UNICODE = u'https://dev.moleculea.com/离线下载.txt'
FILE_UTF8 = utf8_encode(u'http://dev.moleculea.com/离线下载.txt')
def cleanup_data():
os.chdir(PROJECT_PATH)
if os.path.exists(TEST_DATA_DIR):
shutil.rmtree(TEST_DATA_DIR)
class TestDownload(TestCase):
"""Test homura.download"""
def setUp(self):
cleanup_data()
os.mkdir(TEST_DATA_DIR)
os.mkdir(TEST_DATA_SUBDIR)
os.mkdir(TEST_DATA_UNICODE)
os.mkdir(TEST_DATA_UTF8)
os.chdir(TEST_DATA_DIR)
def test_simple(self):
download(FILE_1MB)
f = os.path.join(TEST_DATA_DIR, get_resource_name(FILE_1MB))
assert os.path.exists(f)
os.remove(f)
def test_path(self):
url = FILE_SMALL
# path=''
download(url=url, path='')
f = os.path.join(TEST_DATA_DIR, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# path='.'
download(url=url, path='.')
f = os.path.join(TEST_DATA_DIR, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# path=TEST_DATA_SUBDIR
download(url=url, path=TEST_DATA_SUBDIR)
f = os.path.join(TEST_DATA_SUBDIR, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# path='foobar'
download(url=url, path='foobar')
f = os.path.join(TEST_DATA_DIR, 'foobar')
assert os.path.exists(f)
os.remove(f)
# path='foo/bar'
with self.assertRaises(IOError):
download(url=url, path='foo/bar')
f = os.path.join(TEST_DATA_DIR, 'foo', 'bar')
assert not os.path.exists(f)
def test_redirect(self):
url = FILE_301_SMALL
eurl = FILE_SMALL
# No path
download(url=url)
f = os.path.join(TEST_DATA_DIR, get_resource_name(url))
ef = os.path.join(TEST_DATA_DIR, get_resource_name(eurl))
assert not os.path.exists(f)
assert os.path.exists(ef)
os.remove(ef)
# path='foobar'
download(url=url, path='foobar')
f = os.path.join(TEST_DATA_DIR, 'foobar')
assert os.path.exists(f)
os.remove(f)
def test_unicode(self):
url = FILE_UNICODE
path_ascii = TEST_DATA_ASCII
path_unicode = TEST_DATA_UNICODE
path_utf8 = TEST_DATA_UTF8
# No path
download(url=url)
f = os.path.join(TEST_DATA_DIR, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# ASCII path
download(url=url, path=path_ascii)
f = os.path.join(TEST_DATA_DIR, path_ascii, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# Unicode path
download(url=url, path=path_unicode)
f = os.path.join(TEST_DATA_DIR, path_unicode, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# UTF-8 path
download(url=url, path=path_utf8)
f = os.path.join(utf8_encode(TEST_DATA_DIR), path_utf8,
utf8_encode(get_resource_name(url)))
assert os.path.exists(f)
os.remove(f)
def test_utf8(self):
url = FILE_UTF8
path_ascii = TEST_DATA_ASCII
path_unicode = TEST_DATA_UNICODE
path_utf8 = TEST_DATA_UTF8
# No path
download(url=url)
f = os.path.join(TEST_DATA_DIR, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# ASCII path
download(url=url, path=path_ascii)
f = os.path.join(TEST_DATA_DIR, path_ascii, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# Unicode path
download(url=url, path=path_unicode)
f = os.path.join(TEST_DATA_DIR, path_unicode, get_resource_name(url))
assert os.path.exists(f)
os.remove(f)
# UTF-8 path
download(url=url, path=path_utf8)
f = os.path.join(utf8_encode(TEST_DATA_DIR), path_utf8,
utf8_encode(get_resource_name(url)))
assert os.path.exists(f)
os.remove(f)
def test_pass_through_opts(self):
url = FILE_5MB
opts_url = FILE_1MB
download(url=url, pass_through_opts={pycurl.URL: opts_url})
f = os.path.join(TEST_DATA_DIR, get_resource_name(url))
opts_f = os.path.join(TEST_DATA_DIR, get_resource_name(opts_url))
assert os.path.exists(opts_f)
assert not os.path.exists(f)
os.remove(opts_f)
def test_auth(self):
url = "http://httpbin.org/basic-auth/aaa/bbb"
auth = ("aaa", "bbb")
download(url=url, auth=auth)
f = os.path.join(TEST_DATA_DIR, get_resource_name(url))
with open(f) as handle:
txt = handle.read()
assert '"authenticated": true' in txt
assert '"user": "aaa"' in txt
assert os.path.exists(f)
os.remove(f)
def tearDown(self):
cleanup_data()