~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to test/unit/common/test_swob.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-01-28 09:40:30 UTC
  • mfrom: (1.2.16)
  • Revision ID: package-import@ubuntu.com-20130128094030-aetz57x2qz9ye2d4
Tags: 1.7.6-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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')
241
242
 
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')
251
251
 
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')
 
257
                                   'text/xml'])
258
258
            self.assertEquals(match, 'application/xml')
259
259
 
 
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',
 
266
                                   'text/xml'])
 
267
            self.assertEquals(match, None)
 
268
 
260
269
 
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')
280
289
 
 
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')
 
295
 
 
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')
 
299
 
 
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/')
 
304
 
 
305
        self.assertRaises(TypeError, swift.common.swob.Request.blank,
 
306
                          'ftp://test.com/')
 
307
 
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')
292
319
 
 
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')
 
326
 
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)
302
336
 
 
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')
 
342
 
 
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, '/')
 
348
 
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'))
436
482
 
 
483
    def test_call_reifies_request_if_necessary(self):
 
484
        """
 
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.
 
489
        """
 
490
        def test_app(environ, start_response):
 
491
            start_response('200 OK', [])
 
492
            return ['hi']
 
493
        req = swift.common.swob.Request.blank('/')
 
494
        req.method = 'HEAD'
 
495
        status, headers, app_iter = req.call_application(test_app)
 
496
        resp = swift.common.swob.Response(status=status, headers=dict(headers),
 
497
                                          app_iter=app_iter)
 
498
        output_iter = resp(req.environ, lambda *_: None)
 
499
        self.assertEquals(list(output_iter), [''])
 
500
 
437
501
    def test_location_rewrite(self):
438
502
        def start_response(env, headers):
439
503
            pass
681
745
        resp.etag = None
682
746
        self.assert_('etag' not in resp.headers)
683
747
 
 
748
    def test_host_url_default(self):
 
749
        resp = self._get_response()
 
750
        env = resp.environ
 
751
        env['wsgi.url_scheme'] = 'http'
 
752
        env['SERVER_NAME'] = 'bob'
 
753
        env['SERVER_PORT'] = '1234'
 
754
        del env['HTTP_HOST']
 
755
        self.assertEquals(resp.host_url, 'http://bob:1234')
 
756
 
 
757
    def test_host_url_default_port_squelched(self):
 
758
        resp = self._get_response()
 
759
        env = resp.environ
 
760
        env['wsgi.url_scheme'] = 'http'
 
761
        env['SERVER_NAME'] = 'bob'
 
762
        env['SERVER_PORT'] = '80'
 
763
        del env['HTTP_HOST']
 
764
        self.assertEquals(resp.host_url, 'http://bob')
 
765
 
 
766
    def test_host_url_https(self):
 
767
        resp = self._get_response()
 
768
        env = resp.environ
 
769
        env['wsgi.url_scheme'] = 'https'
 
770
        env['SERVER_NAME'] = 'bob'
 
771
        env['SERVER_PORT'] = '1234'
 
772
        del env['HTTP_HOST']
 
773
        self.assertEquals(resp.host_url, 'https://bob:1234')
 
774
 
 
775
    def test_host_url_https_port_squelched(self):
 
776
        resp = self._get_response()
 
777
        env = resp.environ
 
778
        env['wsgi.url_scheme'] = 'https'
 
779
        env['SERVER_NAME'] = 'bob'
 
780
        env['SERVER_PORT'] = '443'
 
781
        del env['HTTP_HOST']
 
782
        self.assertEquals(resp.host_url, 'https://bob')
 
783
 
 
784
    def test_host_url_host_override(self):
 
785
        resp = self._get_response()
 
786
        env = resp.environ
 
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')
 
792
 
 
793
    def test_host_url_host_port_override(self):
 
794
        resp = self._get_response()
 
795
        env = resp.environ
 
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')
 
801
 
 
802
    def test_host_url_host_https(self):
 
803
        resp = self._get_response()
 
804
        env = resp.environ
 
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')
 
810
 
 
811
    def test_507(self):
 
812
        resp = swift.common.swob.HTTPInsufficientStorage()
 
813
        content = ''.join(resp._response_iter(resp.app_iter, resp._body))
 
814
        self.assertEquals(
 
815
            content,
 
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))
 
820
        self.assertEquals(
 
821
            content,
 
822
            '<html><h1>Insufficient Storage</h1><p>There was not enough space '
 
823
            'to save the resource. Drive: sda1</p></html>')
 
824
 
684
825
 
685
826
class TestUTC(unittest.TestCase):
686
827
    def test_tzname(self):