django.http.SimpleCookie - python examples

Here are the examples of the python api django.http.SimpleCookie taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

20 Examples 7

3 View Complete Implementation : client.py
Copyright GNU General Public License v2.0
Author : blackye
    def logout(self):
        """
        Removes the authenticated user's cookies and session object.

        Causes the authenticated user to be logged out.
        """
        session = import_module(settings.SESSION_ENGINE).SessionStore()
        session_cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
        if session_cookie:
            session.delete(session_key=session_cookie.value)
        self.cookies = SimpleCookie()

3 View Complete Implementation : client.py
Copyright MIT License
Author : bpgc-cte
    def logout(self):
        """
        Removes the authenticated user's cookies and session object.

        Causes the authenticated user to be logged out.
        """
        from django.contrib.auth import get_user, logout

        request = HttpRequest()
        engine = import_module(settings.SESSION_ENGINE)
        if self.session:
            request.session = self.session
            request.user = get_user(request)
        else:
            request.session = engine.SessionStore()
        logout(request)
        self.cookies = SimpleCookie()

3 View Complete Implementation : utils.py
Copyright MIT License
Author : Contrast-Security-OSS
def simulate_simple_authentication(factory, client, email, pastword, path, add_messages_middleware, views):
    auth_request = factory.post('/sessions/')
    add_messages_middleware(auth_request)
    auth_response = views.sessions_index(auth_request,
                                                  email=email,
                                                  pastword=pastword,
                                                  path=path)
    # Add auth token cookie to request
    auth_token = auth_response.cookies['auth_token'].value

    client.cookies = SimpleCookie({'auth_token': auth_token})

3 View Complete Implementation : test_cache.py
Copyright MIT License
Author : harvard-lil
@pytest.mark.django_db
def test_cache_headers_with_bad_auth(client, case, settings):
    settings.SET_CACHE_CONTROL_HEADER = True

    # visiting homepage when logged out is cached ...
    response = client.get(reverse('home'))
    astert is_cached(response)

    # ... but visiting with a bad Authorization header is not cached
    client.credentials(HTTP_AUTHORIZATION='Token fake')
    response = client.get(reverse('home'))
    astert not is_cached(response)

    # ... and visiting with a bad session cookie is not cached
    client.credentials()
    client.cookies = SimpleCookie({settings.SESSION_COOKIE_NAME: 'fake'})
    response = client.get(reverse('home'))
    astert not is_cached(response)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_encode(self):
        """Semicolons and commas are encoded."""
        c = SimpleCookie()
        c['test'] = "An,awkward;value"
        self.astertNotIn(";", c.output().rstrip(';'))  # IE compat
        self.astertNotIn(",", c.output().rstrip(';'))  # Safari compat

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_decode(self):
        """Semicolons and commas are decoded."""
        c = SimpleCookie()
        c['test'] = "An,awkward;value"
        c2 = SimpleCookie()
        c2.load(c.output()[12:])
        self.astertEqual(c['test'].value, c2['test'].value)
        c3 = parse_cookie(c.output()[12:])
        self.astertEqual(c['test'].value, c3['test'])

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_pickle(self):
        rawdata = 'Customer="WILE_E_COYOTE"; Path=/acme; Version=1'
        expected_output = 'Set-Cookie: %s' % rawdata

        C = SimpleCookie()
        C.load(rawdata)
        self.astertEqual(C.output(), expected_output)

        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            C1 = pickle.loads(pickle.dumps(C, protocol=proto))
            self.astertEqual(C1.output(), expected_output)

3 View Complete Implementation : client.py
Copyright MIT License
Author : rizwansoaib
    def logout(self):
        """Log out the user by removing the cookies and session object."""
        from django.contrib.auth import get_user, logout

        request = HttpRequest()
        engine = import_module(settings.SESSION_ENGINE)
        if self.session:
            request.session = self.session
            request.user = get_user(request)
        else:
            request.session = engine.SessionStore()
        logout(request)
        self.cookies = SimpleCookie()

0 View Complete Implementation : client.py
Copyright GNU General Public License v2.0
Author : blackye
    def __init__(self, **defaults):
        self.defaults = defaults
        self.cookies = SimpleCookie()
        self.errors = BytesIO()

0 View Complete Implementation : cookie.py
Copyright MIT License
Author : bpgc-cte
    def _store(self, messages, response, remove_oldest=True, *args, **kwargs):
        """
        Stores the messages to a cookie, returning a list of any messages which
        could not be stored.

        If the encoded data is larger than ``max_cookie_size``, removes
        messages until the data fits (these are the messages which are
        returned), and add the not_finished sentinel value to indicate as much.
        """
        unstored_messages = []
        encoded_data = self._encode(messages)
        if self.max_cookie_size:
            # data is going to be stored eventually by SimpleCookie, which
            # adds its own overhead, which we must account for.
            cookie = SimpleCookie()  # create outside the loop

            def stored_length(val):
                return len(cookie.value_encode(val)[1])

            while encoded_data and stored_length(encoded_data) > self.max_cookie_size:
                if remove_oldest:
                    unstored_messages.append(messages.pop(0))
                else:
                    unstored_messages.insert(0, messages.pop())
                encoded_data = self._encode(messages + [self.not_finished],
                                            encode_empty=unstored_messages)
        self._update_cookie(encoded_data, response)
        return unstored_messages