web/test/unittest_http.py
author Julien Cristau <julien.cristau@logilab.fr>
Thu, 08 Oct 2015 13:51:54 +0200
changeset 10798 3f24adbf971a
parent 9736 e0b3fcf1c205
child 10849 79066409fdcf
permissions -rw-r--r--
[web/http_headers] use classmethod decorator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     1
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     3
#
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     4
# This file is part of CubicWeb.
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     5
#
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
     9
# any later version.
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    10
#
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    14
# details.
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    15
#
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
    18
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
    19
import contextlib
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
    20
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
from logilab.common.testlib import TestCase, unittest_main, tag, Tags
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from cubicweb.devtools.fake import FakeRequest
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
    24
from cubicweb.devtools.testlib import CubicWebTC
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
def _test_cache(hin, hout, method='GET'):
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    28
    """forge and process an HTTP request using given headers in/out and method,
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    29
    then return it once its .is_client_cache_valid() method has been called.
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    31
    req.status_out is None if the page should have been calculated.
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    """
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
    # forge request
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
    req = FakeRequest(method=method)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    for key, value in hin:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
        req._headers_in.addRawHeader(key, str(value))
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    for key, value in hout:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        req.headers_out.addRawHeader(key, str(value))
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    # process
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    40
    req.status_out = None
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    41
    req.is_client_cache_valid()
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    42
    return req
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
class HTTPCache(TestCase):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    """Check that the http cache logiac work as expected
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    (as far as we understood the RFC)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    """
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    tags = TestCase.tags | Tags('http', 'cache')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
    def assertCache(self, expected, status, situation=''):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        """simple assert for nicer message"""
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        if expected != status:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
            if expected is None:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
                expected = "MODIFIED"
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
            if status is None:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
                status = "MODIFIED"
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
            msg = 'expected %r got %r' % (expected, status)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
            if situation:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
                msg = "%s - when: %s" % (msg, situation)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
            self.fail(msg)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
    def test_IN_none_OUT_none(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        #: test that no caching is requested when not data is available
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        #: on any side
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    67
        req =_test_cache((), ())
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    68
        self.assertIsNone(req.status_out)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    def test_IN_Some_OUT_none(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
        #: test that no caching is requested when no data is available
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        #: server (origin) side
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        hin = [('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
              ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    75
        req = _test_cache(hin, ())
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    76
        self.assertIsNone(req.status_out)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        hin = [('if-none-match','babar/huitre'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
              ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    79
        req = _test_cache(hin, ())
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    80
        self.assertIsNone(req.status_out)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        hin = [('if-modified-since','Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
               ('if-none-match','babar/huitre'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
              ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    84
        req = _test_cache(hin, ())
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    85
        self.assertIsNone(req.status_out)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    def test_IN_none_OUT_Some(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
        #: test that no caching is requested when no data is provided
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
        #: by the client
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
        hout = [('last-modified','Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    92
        req = _test_cache((), hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    93
        self.assertIsNone(req.status_out)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        hout = [('etag','babar/huitre'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    96
        req = _test_cache((), hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
    97
        self.assertIsNone(req.status_out)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        hout = [('last-modified', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
                ('etag','babar/huitre'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   101
        req = _test_cache((), hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   102
        self.assertIsNone(req.status_out)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    @tag('last_modified')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
    def test_last_modified_newer(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        #: test the proper behavior of modification date only
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        # newer
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        hin  = [('if-modified-since', 'Sat, 13 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
        hout = [('last-modified', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   112
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   113
        self.assertCache(None, req.status_out, 'origin is newer than client')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
    @tag('last_modified')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    def test_last_modified_older(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        # older
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        hin  = [('if-modified-since', 'Sat, 15 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        hout = [('last-modified', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   122
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   123
        self.assertCache(304, req.status_out, 'origin is older than client')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
    @tag('last_modified')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    def test_last_modified_same(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
        # same
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        hin  = [('if-modified-since', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        hout = [('last-modified', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   132
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   133
        self.assertCache(304, req.status_out, 'origin is equal to client')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
    @tag('etag')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
    def test_etag_mismatch(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        #: test the proper behavior of etag only
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        # etag mismatch
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        hout = [('etag', 'celestine'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   143
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   144
        self.assertCache(None, req.status_out, 'etag mismatch')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
    @tag('etag')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
    def test_etag_match(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        # etag match
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   153
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   154
        self.assertCache(304, req.status_out, 'etag match')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
        # etag match in multiple
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
        hin  = [('if-none-match', 'loutre'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
                ('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   161
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   162
        self.assertCache(304, req.status_out, 'etag match in multiple')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
        # client use "*" as etag
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
        hin  = [('if-none-match', '*'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   168
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   169
        self.assertCache(304, req.status_out, 'client use "*" as etag')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
    @tag('etag', 'last_modified')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
    def test_both(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        #: test the proper behavior of etag only
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
        # both wrong
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
                ('if-modified-since', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
        hout = [('etag', 'loutre'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
                ('last-modified', 'Sat, 15 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   181
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   182
        self.assertCache(None, req.status_out, 'both wrong')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
    @tag('etag', 'last_modified')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
    def test_both_etag_mismatch(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
        # both etag mismatch
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   188
                ('if-modified-since', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
        hout = [('etag', 'loutre'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
                ('last-modified', 'Sat, 13 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   193
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   194
        self.assertCache(None, req.status_out, 'both  but etag mismatch')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    @tag('etag', 'last_modified')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
    def test_both_but_modified(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        # both but modified
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
                ('if-modified-since', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
                ('last-modified', 'Sat, 15 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   205
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   206
        self.assertCache(None, req.status_out, 'both  but modified')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
    @tag('etag', 'last_modified')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
    def test_both_ok(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        # both ok
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
                ('if-modified-since', 'Sat, 14 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
                ('last-modified', 'Sat, 13 Apr 2012 14:39:32 GM'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   217
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   218
        self.assertCache(304, req.status_out, 'both ok')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
    @tag('etag', 'HEAD')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
    def test_head_verb(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
        #: check than FOUND 200 is properly raise without content on HEAD request
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
        #: This logic does not really belong here :-/
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
        # modified
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
        hout = [('etag', 'rhino/really-not-babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   229
        req = _test_cache(hin, hout, method='HEAD')
9736
e0b3fcf1c205 [web] Fix HEAD request handling (closes #3677949)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9648
diff changeset
   230
        self.assertCache(None, req.status_out, 'modifier HEAD verb')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
        # not modified
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   236
        req = _test_cache(hin, hout, method='HEAD')
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   237
        self.assertCache(304, req.status_out, 'not modifier HEAD verb')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
    @tag('etag', 'POST')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
    def test_post_verb(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
        # modified
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
        hout = [('etag', 'rhino/really-not-babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   246
        req = _test_cache(hin, hout, method='POST')
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   247
        self.assertCache(None, req.status_out, 'modifier HEAD verb')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
        # not modified
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   253
        req = _test_cache(hin, hout, method='POST')
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   254
        self.assertCache(412, req.status_out, 'not modifier HEAD verb')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    @tag('expires')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
    def test_expires_added(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
        #: Check that Expires header is added:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   259
        #: - when the page is modified
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
        #: - when none was already present
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        hout = [('etag', 'rhino/really-not-babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   265
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   266
        self.assertCache(None, req.status_out, 'modifier HEAD verb')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
        value = req.headers_out.getHeader('expires')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
        self.assertIsNotNone(value)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
    @tag('expires')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   271
    def test_expires_not_added(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
        #: Check that Expires header is not added if NOT-MODIFIED
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   274
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
        hout = [('etag', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   277
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   278
        self.assertCache(304, req.status_out, 'not modifier HEAD verb')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
        value = req.headers_out.getHeader('expires')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
        self.assertIsNone(value)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
    @tag('expires')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
    def test_expires_no_overwrite(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
        #: Check that cache does not overwrite existing Expires header
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
        hin  = [('if-none-match', 'babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
               ]
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
        DATE = 'Sat, 13 Apr 2012 14:39:32 GM'
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
        hout = [('etag', 'rhino/really-not-babar'),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
                ('expires', DATE),
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
               ]
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   291
        req = _test_cache(hin, hout)
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8695
diff changeset
   292
        self.assertCache(None, req.status_out, 'not modifier HEAD verb')
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
        value = req.headers_out.getRawHeaders('expires')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
        self.assertEqual(value, [DATE])
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   297
alloworig = 'access-control-allow-origin'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   298
allowmethods = 'access-control-allow-methods'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   299
allowheaders = 'access-control-allow-headers'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   300
allowcreds = 'access-control-allow-credentials'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   301
exposeheaders = 'access-control-expose-headers'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   302
maxage = 'access-control-max-age'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   303
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   304
requestmethod = 'access-control-request-method'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   305
requestheaders = 'access-control-request-headers'
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   306
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   307
class _BaseAccessHeadersTC(CubicWebTC):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   308
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   309
    @contextlib.contextmanager
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   310
    def options(self, **options):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   311
        for k, values in options.items():
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   312
            self.config.set_option(k, values)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   313
        try:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   314
            yield
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   315
        finally:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   316
            for k in options:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   317
                self.config.set_option(k, '')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   318
    def check_no_cors(self, req):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   319
        self.assertEqual(None, req.get_response_header(alloworig))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   320
        self.assertEqual(None, req.get_response_header(allowmethods))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   321
        self.assertEqual(None, req.get_response_header(allowheaders))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   322
        self.assertEqual(None, req.get_response_header(allowcreds))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   323
        self.assertEqual(None, req.get_response_header(exposeheaders))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   324
        self.assertEqual(None, req.get_response_header(maxage))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   325
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   326
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   327
class SimpleAccessHeadersTC(_BaseAccessHeadersTC):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   328
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   329
    def test_noaccess(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   330
        with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   331
            data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   332
            self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   333
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   334
    def test_noorigin(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   335
        with self.options(**{alloworig: '*'}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   336
            with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   337
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   338
                self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   339
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   340
    def test_origin_noaccess(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   341
        with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   342
            req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   343
            data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   344
            self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   345
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   346
    def test_origin_noaccess_bad_host(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   347
        with self.options(**{alloworig: '*'}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   348
            with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   349
                req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   350
                # in these tests, base_url is http://testing.fr/cubicweb/
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   351
                req.set_request_header('Host', 'badhost.net')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   352
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   353
                self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   354
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   355
    def test_explicit_origin_noaccess(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   356
        with self.options(**{alloworig: ['http://www.toto.org', 'http://othersite.fr']}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   357
            with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   358
                req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   359
                # in these tests, base_url is http://testing.fr/cubicweb/
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   360
                req.set_request_header('Host', 'testing.fr')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   361
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   362
                self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   363
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   364
    def test_origin_access(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   365
        with self.options(**{alloworig: '*'}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   366
            with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   367
                req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   368
                # in these tests, base_url is http://testing.fr/cubicweb/
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   369
                req.set_request_header('Host', 'testing.fr')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   370
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   371
                self.assertEqual('http://www.cubicweb.org',
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   372
                                 req.get_response_header(alloworig))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   373
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   374
    def test_explicit_origin_access(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   375
        with self.options(**{alloworig: ['http://www.cubicweb.org', 'http://othersite.fr']}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   376
            with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   377
                req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   378
                # in these tests, base_url is http://testing.fr/cubicweb/
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   379
                req.set_request_header('Host', 'testing.fr')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   380
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   381
                self.assertEqual('http://www.cubicweb.org',
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   382
                                 req.get_response_header(alloworig))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   383
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   384
    def test_origin_access_headers(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   385
        with self.options(**{alloworig: '*',
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   386
                             exposeheaders: ['ExposeHead1', 'ExposeHead2'],
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   387
                             allowheaders: ['AllowHead1', 'AllowHead2'],
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   388
                             allowmethods: ['GET', 'POST', 'OPTIONS']}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   389
            with self.admin_access.web_request() as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   390
                req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   391
                # in these tests, base_url is http://testing.fr/cubicweb/
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   392
                req.set_request_header('Host', 'testing.fr')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   393
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   394
                self.assertEqual('http://www.cubicweb.org',
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   395
                                 req.get_response_header(alloworig))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   396
                self.assertEqual("true",
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   397
                                 req.get_response_header(allowcreds))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   398
                self.assertEqual(['ExposeHead1', 'ExposeHead2'],
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   399
                                 req.get_response_header(exposeheaders))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   400
                self.assertEqual(None, req.get_response_header(allowmethods))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   401
                self.assertEqual(None, req.get_response_header(allowheaders))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   402
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   403
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   404
class PreflightAccessHeadersTC(_BaseAccessHeadersTC):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   405
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   406
    def test_noaccess(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   407
        with self.admin_access.web_request(method='OPTIONS') as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   408
            data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   409
            self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   410
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   411
    def test_noorigin(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   412
        with self.options(**{alloworig: '*'}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   413
            with self.admin_access.web_request(method='OPTIONS') as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   414
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   415
                self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   416
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   417
    def test_origin_noaccess(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   418
        with self.admin_access.web_request(method='OPTIONS') as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   419
            req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   420
            data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   421
            self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   422
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   423
    def test_origin_noaccess_bad_host(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   424
        with self.options(**{alloworig: '*'}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   425
            with self.admin_access.web_request(method='OPTIONS') as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   426
                req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   427
                # in these tests, base_url is http://testing.fr/cubicweb/
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   428
                req.set_request_header('Host', 'badhost.net')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   429
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   430
                self.check_no_cors(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   431
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   432
    def test_origin_access(self):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   433
        with self.options(**{alloworig: '*',
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   434
                             exposeheaders: ['ExposeHead1', 'ExposeHead2'],
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   435
                             allowheaders: ['AllowHead1', 'AllowHead2'],
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   436
                             allowmethods: ['GET', 'POST', 'OPTIONS']}):
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   437
            with self.admin_access.web_request(method='OPTIONS') as req:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   438
                req.set_request_header('Origin', 'http://www.cubicweb.org')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   439
                # in these tests, base_url is http://testing.fr/cubicweb/
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   440
                req.set_request_header('Host', 'testing.fr')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   441
                req.set_request_header(requestmethod, 'GET')
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   442
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   443
                data = self.app_handle_request(req)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   444
                self.assertEqual(200, req.status_out)
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   445
                self.assertEqual('http://www.cubicweb.org',
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   446
                                 req.get_response_header(alloworig))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   447
                self.assertEqual("true",
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   448
                                 req.get_response_header(allowcreds))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   449
                self.assertEqual(set(['GET', 'POST', 'OPTIONS']),
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   450
                                 req.get_response_header(allowmethods))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   451
                self.assertEqual(set(['AllowHead1', 'AllowHead2']),
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   452
                                 req.get_response_header(allowheaders))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   453
                self.assertEqual(None,
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   454
                                 req.get_response_header(exposeheaders))
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   455
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9229
diff changeset
   456
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
if __name__ == '__main__':
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   458
    unittest_main()