forked from deanishe/alfred-workflow
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_web.py
237 lines (201 loc) · 7.6 KB
/
test_web.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2014 Dean Jackson <deanishe@deanishe.net>
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2014-03-01
#
"""
test_web.py
"""
from __future__ import print_function, unicode_literals
import os
import unittest
import urllib2
import json
import socket
from base64 import b64decode
from pprint import pprint
from workflow import web
BASE_URL = 'http://eu.httpbin.org/'
def setUp():
pass
def tearDown():
pass
class WebTests(unittest.TestCase):
def setUp(self):
self.data = {'name': 'My name is Jürgen!',
'address': 'Hürterstr. 42\nEssen'}
self.test_file = os.path.join(os.path.dirname(__file__),
b'cönfüsed.gif')
self.fubar_url = 'http://deanishe.net/fubar.txt'
self.fubar_bytes = b'fübar'
self.fubar_unicode = 'fübar'
def tearDown(self):
pass
def test_404(self):
"""Non-existant URL raises HTTPError w/ 404"""
url = BASE_URL + 'status/404'
r = web.get(url)
self.assertRaises(urllib2.HTTPError, r.raise_for_status)
self.assert_(r.status_code == 404)
def test_follow_redirect(self):
"""Redirects are followed"""
newurl = 'http://www.kulturliste-duesseldorf.de/'
url = BASE_URL + 'redirect-to?url=' + newurl
r = web.get(url)
self.assertEqual(r.url, newurl)
def test_no_follow_redirect(self):
"""Redirects are not followed"""
newurl = 'http://www.kulturliste-duesseldorf.de/'
url = BASE_URL + 'redirect-to?url=' + newurl
r = web.get(url, allow_redirects=False)
self.assertNotEquals(r.url, newurl)
self.assertRaises(urllib2.HTTPError, r.raise_for_status)
self.assertEqual(r.status_code, 302)
def test_post_form(self):
"""POST Form data"""
url = BASE_URL + 'post'
r = web.post(url, data=self.data)
self.assert_(r.status_code == 200)
r.raise_for_status()
form = r.json()['form']
for key in self.data:
self.assert_(form[key] == self.data[key])
def test_post_json(self):
"""POST request with JSON body"""
url = BASE_URL + 'post'
headers = {'content-type': 'application/json'}
print('Posting JSON ...')
r = web.post(url, headers=headers, data=json.dumps(self.data))
self.assert_(r.status_code == 200)
data = r.json()
pprint(data)
self.assertEqual(data['headers']['Content-Type'],
'application/json')
for key in self.data:
self.assert_(data['json'][key] == self.data[key])
return
def test_timeout(self):
"""Request times out"""
url = BASE_URL + 'delay/10'
self.assertRaises(socket.timeout, web.get, url, timeout=5)
def test_encoding(self):
"""HTML is decoded"""
url = BASE_URL + 'html'
r = web.get(url)
self.assertEqual(r.encoding, 'utf-8')
self.assert_(isinstance(r.text, unicode))
def test_no_encoding(self):
"""No encoding"""
# Is an image
url = 'https://avatars.githubusercontent.com/u/747913'
r = web.get(url)
self.assertEqual(r.encoding, None)
self.assert_(isinstance(r.text, str))
def test_xml_encoding(self):
"""XML is decoded"""
url = 'http://feeds.theguardian.com/theguardian/technology/rss'
r = web.get(url)
self.assertEqual(r.encoding, 'utf-8')
self.assert_(isinstance(r.text, unicode))
def test_get_vars(self):
"""GET vars"""
url = BASE_URL + 'get'
r = web.get(url, params=self.data)
self.assertEqual(r.status_code, 200)
args = r.json()['args']
for key in self.data:
self.assertEqual(args[key], self.data[key])
def test_auth_succeeds(self):
"""Basic AUTH succeeds"""
url = BASE_URL + '/basic-auth/bobsmith/password1'
r = web.get(url, auth=('bobsmith', 'password1'))
self.assertEqual(r.status_code, 200)
data = r.json()
self.assertEqual(data['user'], 'bobsmith')
self.assertTrue(data['authenticated'])
def test_auth_fails(self):
"""Basic AUTH fails"""
url = BASE_URL + '/basic-auth/bobsmith/password1'
r = web.get(url, auth=('bobsmith', 'password2'))
self.assertEqual(r.status_code, 401)
self.assertRaises(urllib2.HTTPError, r.raise_for_status)
def test_file_upload(self):
"""File upload"""
url = BASE_URL + 'post'
files = {'file': {'filename': 'cönfüsed.gif',
'content': open(self.test_file, 'rb').read(),
'mimetype': 'image/gif',
}}
r = web.post(url, data=self.data, files=files)
self.assertEqual(r.status_code, 200)
data = r.json()
form = data['form']
for key in self.data:
self.assertEqual(self.data[key], form[key])
# image
bindata = data['files']['file']
preamble = 'data:image/gif;base64,'
self.assert_(bindata.startswith(preamble))
bindata = b64decode(bindata[len(preamble):])
self.assertEqual(bindata, open(self.test_file, 'rb').read())
def test_file_upload_without_form_data(self):
"""File upload w/o form data"""
url = BASE_URL + 'post'
files = {'file': {'filename': 'cönfüsed.gif',
'content': open(self.test_file, 'rb').read()
}}
r = web.post(url, files=files)
self.assertEqual(r.status_code, 200)
data = r.json()
# image
bindata = data['files']['file']
preamble = 'data:image/gif;base64,'
self.assert_(bindata.startswith(preamble))
bindata = b64decode(bindata[len(preamble):])
self.assertEqual(bindata, open(self.test_file, 'rb').read())
def test_json_encoding(self):
"""JSON decoded correctly"""
url = 'https://suggestqueries.google.com/complete/search?client=firefox&q=münchen'
r = web.get(url)
self.assertEqual(r.status_code, 200)
data = r.json()
self.assertEqual(data[0], 'münchen')
def test_iter_content(self):
"""iter_content returns content"""
r = web.get(self.fubar_url)
self.assertEqual(r.status_code, 200)
contents = b''
for s in r.iter_content(chunk_size=1):
contents += s
self.assertEqual(contents, self.fubar_bytes)
def test_iter_content_decoded(self):
"""iter_content returns content"""
r = web.get(self.fubar_url)
self.assertEqual(r.status_code, 200)
contents = ''
for u in r.iter_content(chunk_size=1, decode_unicode=True):
contents += u
self.assertEqual(contents, self.fubar_unicode)
def test_encoded_content(self):
"""Encoded content"""
r = web.get(self.fubar_url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, self.fubar_bytes)
self.assertEqual(r.text, self.fubar_unicode)
def test_decoded_content(self):
"""Decoded content"""
r = web.get(self.fubar_url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, self.fubar_unicode)
def test_encoded_decode_content(self):
"""Encoded and decoded content"""
r = web.get(self.fubar_url)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, self.fubar_bytes)
self.assertEqual(r.text, self.fubar_unicode)
if __name__ == '__main__': # pragma: no cover
unittest.main()