示例#1
0
    def test_etag_response_mixin(self):
        response = Response('Hello World')
        self.assertEqual(response.get_etag(), (None, None))
        response.add_etag()
        self.assertEqual(
            response.get_etag(), ('b10a8db164e0754105b7a99be72e3fe5', False)
        )
        self.assertFalse(response.cache_control)
        response.cache_control.must_revalidate = True
        response.cache_control.max_age = 60
        response.headers['Content-Length'] = len(response.get_data())
        self.assertIn(
            response.headers['Cache-Control'],
            ('must-revalidate, max-age=60', 'max-age=60, must-revalidate')
        )

        self.assertNotIn('date', response.headers)
        env = create_environ()
        env.update({
            'REQUEST_METHOD': 'GET',
            'HTTP_IF_NONE_MATCH': response.get_etag()[0]
        })
        response.make_conditional(env)
        self.assertIn('date', response.headers)

        # after the thing is invoked by the server as wsgi application
        # (we're emulating this here), there must not be any entity
        # headers left and the status code would have to be 304
        resp = Response.from_app(response, env)
        self.assertEqual(resp.status_code, 304)
        self.assertNotIn('content-length', resp.headers)

        # make sure date is not overriden
        response = Response('Hello World')
        response.date = 1337
        d = response.date
        response.make_conditional(env)
        self.assertEqual(response.date, d)

        # make sure content length is only set if missing
        response = Response('Hello World')
        response.content_length = 999
        response.make_conditional(env)
        self.assertEqual(response.content_length, 999)
示例#2
0
    def test_etag_response_mixin(self):
        response = Response('Hello World')
        self.assertEqual(response.get_etag(), (None, None))
        response.add_etag()
        self.assertEqual(
            response.get_etag(), ('b10a8db164e0754105b7a99be72e3fe5', False)
        )
        self.assertFalse(response.cache_control)
        response.cache_control.must_revalidate = True
        response.cache_control.max_age = 60
        response.headers['Content-Length'] = len(response.get_data())
        self.assertIn(
            response.headers['Cache-Control'],
            ('must-revalidate, max-age=60', 'max-age=60, must-revalidate')
        )

        self.assertNotIn('date', response.headers)
        env = create_environ()
        env.update({
            'REQUEST_METHOD':       'GET',
            'HTTP_IF_NONE_MATCH':   response.get_etag()[0]
        })
        response.make_conditional(env)
        self.assertIn('date', response.headers)

        # after the thing is invoked by the server as wsgi application
        # (we're emulating this here), there must not be any entity
        # headers left and the status code would have to be 304
        resp = Response.from_app(response, env)
        self.assertEqual(resp.status_code, 304)
        self.assertNotIn('content-length', resp.headers)

        # make sure date is not overriden
        response = Response('Hello World')
        response.date = 1337
        d = response.date
        response.make_conditional(env)
        self.assertEqual(response.date, d)

        # make sure content length is only set if missing
        response = Response('Hello World')
        response.content_length = 999
        response.make_conditional(env)
        self.assertEqual(response.content_length, 999)
示例#3
0
    def test_common_response_descriptors_mixin(self):
        response = Response()
        response.mimetype = 'text/html'
        self.assertEqual(response.mimetype, 'text/html')
        self.assertEqual(response.content_type, 'text/html; charset=utf-8')
        self.assertEqual(response.mimetype_params, {'charset': 'utf-8'})
        response.mimetype_params['x-foo'] = 'yep'
        del response.mimetype_params['charset']
        self.assertEqual(response.content_type, 'text/html; x-foo=yep')

        now = datetime.utcnow().replace(microsecond=0)

        self.assertIsNone(response.content_length)
        response.content_length = '42'
        self.assertEqual(response.content_length, 42)

        for attr in 'date', 'age', 'expires':
            self.assertIsNone(getattr(response, attr))
            setattr(response, attr, now)
            self.assertEqual(getattr(response, attr), now)

        self.assertIsNone(response.retry_after)
        response.retry_after = now
        self.assertEqual(response.retry_after, now)

        self.assertFalse(response.vary)
        response.vary.add('Cookie')
        response.vary.add('Content-Language')
        self.assertTrue('cookie' in response.vary)
        self.assertEqual(response.vary.to_header(), 'Cookie, Content-Language')
        response.headers['Vary'] = 'Content-Encoding'
        self.assertEqual(response.vary.as_set(), set(['content-encoding']))

        response.allow.update(['GET', 'POST'])
        self.assertEqual(response.headers['Allow'], 'GET, POST')

        response.content_language.add('en-US')
        response.content_language.add('fr')
        self.assertEqual(response.headers['Content-Language'], 'en-US, fr')
示例#4
0
    def test_common_response_descriptors_mixin(self):
        response = Response()
        response.mimetype = 'text/html'
        self.assertEqual(response.mimetype, 'text/html')
        self.assertEqual(response.content_type, 'text/html; charset=utf-8')
        self.assertEqual(response.mimetype_params, {'charset': 'utf-8'})
        response.mimetype_params['x-foo'] = 'yep'
        del response.mimetype_params['charset']
        self.assertEqual(response.content_type, 'text/html; x-foo=yep')

        now = datetime.utcnow().replace(microsecond=0)

        self.assertIsNone(response.content_length)
        response.content_length = '42'
        self.assertEqual(response.content_length, 42)

        for attr in 'date', 'age', 'expires':
            self.assertIsNone(getattr(response, attr))
            setattr(response, attr, now)
            self.assertEqual(getattr(response, attr), now)

        self.assertIsNone(response.retry_after)
        response.retry_after = now
        self.assertEqual(response.retry_after, now)

        self.assertFalse(response.vary)
        response.vary.add('Cookie')
        response.vary.add('Content-Language')
        self.assertTrue('cookie' in response.vary)
        self.assertEqual(response.vary.to_header(), 'Cookie, Content-Language')
        response.headers['Vary'] = 'Content-Encoding'
        self.assertEqual(response.vary.as_set(), set(['content-encoding']))

        response.allow.update(['GET', 'POST'])
        self.assertEqual(response.headers['Allow'], 'GET, POST')

        response.content_language.add('en-US')
        response.content_language.add('fr')
        self.assertEqual(response.headers['Content-Language'], 'en-US, fr')