232
232
class TestAccept(unittest.TestCase):
233
233
def test_accept_json(self):
234
234
for accept in ('application/json', 'application/json;q=1.0,*/*;q=0.9',
235
'*/*;q=0.9,application/json;q=1.0', 'application/*'):
235
'*/*;q=0.9,application/json;q=1.0', 'application/*',
236
'text/*,application/json', 'application/*,text/*',
237
'application/json,text/xml'):
236
238
acc = swift.common.swob.Accept(accept)
237
239
match = acc.best_match(['text/plain', 'application/json',
238
'application/xml', 'text/xml'],
239
default_match='text/plain')
240
'application/xml', 'text/xml'])
240
241
self.assertEquals(match, 'application/json')
242
243
def test_accept_plain(self):
245
246
'text/plain,application/xml'):
246
247
acc = swift.common.swob.Accept(accept)
247
248
match = acc.best_match(['text/plain', 'application/json',
248
'application/xml', 'text/xml'],
249
default_match='text/plain')
249
'application/xml', 'text/xml'])
250
250
self.assertEquals(match, 'text/plain')
252
252
def test_accept_xml(self):
254
254
'*/*;q=0.9,application/xml;q=1.0'):
255
255
acc = swift.common.swob.Accept(accept)
256
256
match = acc.best_match(['text/plain', 'application/xml',
257
'text/xml'], default_match='text/plain')
258
258
self.assertEquals(match, 'application/xml')
260
def test_accept_invalid(self):
261
for accept in ('*', 'text/plain,,', 'some stuff',
262
'application/xml;q=1.0;q=1.1', 'text/plain,*',
263
'text /plain', 'text\x7f/plain'):
264
acc = swift.common.swob.Accept(accept)
265
match = acc.best_match(['text/plain', 'application/xml',
267
self.assertEquals(match, None)
261
270
class TestRequest(unittest.TestCase):
262
271
def test_blank(self):
278
287
self.assertEquals(req.headers['Content-Type'], 'text/plain')
279
288
self.assertEquals(req.method, 'POST')
290
def test_blank_parsing(self):
291
req = swift.common.swob.Request.blank('http://test.com/')
292
self.assertEquals(req.environ['wsgi.url_scheme'], 'http')
293
self.assertEquals(req.environ['SERVER_PORT'], '80')
294
self.assertEquals(req.environ['SERVER_NAME'], 'test.com')
296
req = swift.common.swob.Request.blank('https://test.com:456/')
297
self.assertEquals(req.environ['wsgi.url_scheme'], 'https')
298
self.assertEquals(req.environ['SERVER_PORT'], '456')
300
req = swift.common.swob.Request.blank('test.com/')
301
self.assertEquals(req.environ['wsgi.url_scheme'], 'http')
302
self.assertEquals(req.environ['SERVER_PORT'], '80')
303
self.assertEquals(req.environ['PATH_INFO'], 'test.com/')
305
self.assertRaises(TypeError, swift.common.swob.Request.blank,
281
308
def test_params(self):
282
309
req = swift.common.swob.Request.blank('/?a=b&c=d')
283
310
self.assertEquals(req.params['a'], 'b')
290
317
'/', environ={'SCRIPT_NAME': '/hi', 'PATH_INFO': '/there'})
291
318
self.assertEquals(req.path, '/hi/there')
320
def test_path_question_mark(self):
321
req = swift.common.swob.Request.blank('/test%3Ffile')
322
# This tests that .blank unquotes the path when setting PATH_INFO
323
self.assertEquals(req.environ['PATH_INFO'], '/test?file')
324
# This tests that .path requotes it
325
self.assertEquals(req.path, '/test%3Ffile')
293
327
def test_path_info_pop(self):
294
328
req = swift.common.swob.Request.blank('/hi/there')
295
329
self.assertEquals(req.path_info_pop(), 'hi')
300
334
req = swift.common.swob.Request.blank('blahblah')
301
335
self.assertEquals(req.path_info_pop(), None)
337
def test_path_info_pop_last(self):
338
req = swift.common.swob.Request.blank('/last')
339
self.assertEquals(req.path_info_pop(), 'last')
340
self.assertEquals(req.path_info, '')
341
self.assertEquals(req.script_name, '/last')
343
def test_path_info_pop_none(self):
344
req = swift.common.swob.Request.blank('/')
345
self.assertEquals(req.path_info_pop(), '')
346
self.assertEquals(req.path_info, '')
347
self.assertEquals(req.script_name, '/')
303
349
def test_copy_get(self):
304
350
req = swift.common.swob.Request.blank(
305
351
'/hi/there', environ={'REQUEST_METHOD': 'POST'})
434
480
resp.body = u'\N{SNOWMAN}'
435
481
self.assertEquals(resp.body, u'\N{SNOWMAN}'.encode('utf-8'))
483
def test_call_reifies_request_if_necessary(self):
485
The actual bug was a HEAD response coming out with a body because the
486
Request object wasn't passed into the Response object's constructor.
487
The Response object's __call__ method should be able to reify a
488
Request object from the env it gets passed.
490
def test_app(environ, start_response):
491
start_response('200 OK', [])
493
req = swift.common.swob.Request.blank('/')
495
status, headers, app_iter = req.call_application(test_app)
496
resp = swift.common.swob.Response(status=status, headers=dict(headers),
498
output_iter = resp(req.environ, lambda *_: None)
499
self.assertEquals(list(output_iter), [''])
437
501
def test_location_rewrite(self):
438
502
def start_response(env, headers):
682
746
self.assert_('etag' not in resp.headers)
748
def test_host_url_default(self):
749
resp = self._get_response()
751
env['wsgi.url_scheme'] = 'http'
752
env['SERVER_NAME'] = 'bob'
753
env['SERVER_PORT'] = '1234'
755
self.assertEquals(resp.host_url, 'http://bob:1234')
757
def test_host_url_default_port_squelched(self):
758
resp = self._get_response()
760
env['wsgi.url_scheme'] = 'http'
761
env['SERVER_NAME'] = 'bob'
762
env['SERVER_PORT'] = '80'
764
self.assertEquals(resp.host_url, 'http://bob')
766
def test_host_url_https(self):
767
resp = self._get_response()
769
env['wsgi.url_scheme'] = 'https'
770
env['SERVER_NAME'] = 'bob'
771
env['SERVER_PORT'] = '1234'
773
self.assertEquals(resp.host_url, 'https://bob:1234')
775
def test_host_url_https_port_squelched(self):
776
resp = self._get_response()
778
env['wsgi.url_scheme'] = 'https'
779
env['SERVER_NAME'] = 'bob'
780
env['SERVER_PORT'] = '443'
782
self.assertEquals(resp.host_url, 'https://bob')
784
def test_host_url_host_override(self):
785
resp = self._get_response()
787
env['wsgi.url_scheme'] = 'http'
788
env['SERVER_NAME'] = 'bob'
789
env['SERVER_PORT'] = '1234'
790
env['HTTP_HOST'] = 'someother'
791
self.assertEquals(resp.host_url, 'http://someother')
793
def test_host_url_host_port_override(self):
794
resp = self._get_response()
796
env['wsgi.url_scheme'] = 'http'
797
env['SERVER_NAME'] = 'bob'
798
env['SERVER_PORT'] = '1234'
799
env['HTTP_HOST'] = 'someother:5678'
800
self.assertEquals(resp.host_url, 'http://someother:5678')
802
def test_host_url_host_https(self):
803
resp = self._get_response()
805
env['wsgi.url_scheme'] = 'https'
806
env['SERVER_NAME'] = 'bob'
807
env['SERVER_PORT'] = '1234'
808
env['HTTP_HOST'] = 'someother:5678'
809
self.assertEquals(resp.host_url, 'https://someother:5678')
812
resp = swift.common.swob.HTTPInsufficientStorage()
813
content = ''.join(resp._response_iter(resp.app_iter, resp._body))
816
'<html><h1>Insufficient Storage</h1><p>There was not enough space '
817
'to save the resource. Drive: unknown</p></html>')
818
resp = swift.common.swob.HTTPInsufficientStorage(drive='sda1')
819
content = ''.join(resp._response_iter(resp.app_iter, resp._body))
822
'<html><h1>Insufficient Storage</h1><p>There was not enough space '
823
'to save the resource. Drive: sda1</p></html>')
685
826
class TestUTC(unittest.TestCase):
686
827
def test_tzname(self):