Example #1
0
    def process_response(self, request: HttpRequest,
                         response: HttpResponseBase) -> HttpResponseBase:

        # This is the same as the default LocaleMiddleware, minus the
        # logic that redirects 404's that lack a prefixed language in
        # the path into having a language.  See
        # https://code.djangoproject.com/ticket/32005
        language = translation.get_language()
        language_from_path = translation.get_language_from_path(
            request.path_info)
        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
        i18n_patterns_used, _ = is_language_prefix_patterns_used(urlconf)
        if not (i18n_patterns_used and language_from_path):
            patch_vary_headers(response, ("Accept-Language", ))
        assert language is not None
        response.setdefault("Content-Language", language)

        # An additional responsibility of our override of this middleware is to save the user's language
        # preference in a cookie. That determination is made by code handling the request
        # and saved in the set_language flag so that it can be used here.
        set_language = get_request_notes(request).set_language
        if set_language is not None:
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, set_language)

        return response
def test_it_rejects_invalid_samesite_values():
    res = HttpResponseBase()
    with pytest.raises(ValueError, match="samesite must be"):
        res.set_cookie("boop", "hi", samesite="BOOP")
def test_it_accepts_valid_samesite_values(samesite):
    res = HttpResponseBase()
    res.set_cookie("boop", "hallo there", samesite=samesite)
    assert "hallo there" in str(res.cookies["boop"])
    assert res.cookies["boop"]["samesite"] == samesite