def test_set_cookie_expires_is_datetime_tz_and_max_age_is_None(): import datetime res = Response() class FixedOffset(datetime.tzinfo): def __init__(self, offset, name): self.__offset = datetime.timedelta(minutes=offset) self.__name = name def utcoffset(self, dt): return self.__offset def tzname(self, dt): return self.__name def dst(self, dt): return datetime.timedelta(0) then = datetime.datetime.now(FixedOffset(60, 'UTC+1')) + datetime.timedelta(days=1) res.set_cookie('a', '1', expires=then) assert res.headerlist[-1][0] == 'Set-Cookie' val = [x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() assert val[0] in ('Max-Age=86399', 'Max-Age=86400') assert val[1] == 'Path=/' assert val[2] == 'a=1' assert val[3].startswith('expires')
def test_set_cookie_expires_is_datetime_tz_and_max_age_is_None(): import datetime res = Response() class FixedOffset(datetime.tzinfo): def __init__(self, offset, name): self.__offset = datetime.timedelta(minutes=offset) self.__name = name def utcoffset(self, dt): return self.__offset def tzname(self, dt): return self.__name def dst(self, dt): return datetime.timedelta(0) then = datetime.datetime.now(FixedOffset(60, 'UTC+1')) + datetime.timedelta(days=1) res.set_cookie('a', '1', expires=then) assert res.headerlist[-1][0] == 'Set-Cookie' val = [x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() assert val[0] in ('Max-Age=86399', 'Max-Age=86400') assert val[1] == 'Path=/' assert val[2] == 'a=1' assert val[3].startswith('expires')
def test_unicode_cookies_error_raised(): res = Response() with pytest.raises(ValueError): Response.set_cookie( res, 'x', text_(b'\N{BLACK SQUARE}', 'unicode_escape'))
def test_set_cookie_expires_is_datetime_tz_and_max_age_is_None(): import datetime res = Response() class FixedOffset(datetime.tzinfo): def __init__(self, offset, name): self.__offset = datetime.timedelta(minutes=offset) self.__name = name def utcoffset(self, dt): return self.__offset def tzname(self, dt): return self.__name def dst(self, dt): return datetime.timedelta(0) then = datetime.datetime.now(FixedOffset(60, "UTC+1")) + datetime.timedelta(days=1) res.set_cookie("a", "1", expires=then) assert res.headerlist[-1][0] == "Set-Cookie" val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() assert val[0] in ("Max-Age=86399", "Max-Age=86400") assert val[1] == "Path=/" assert val[2] == "a=1" assert val[3].startswith("expires")
def test_set_cookie_expires_is_datetime_tz_and_max_age_is_None(): import datetime res = Response() class FixedOffset(datetime.tzinfo): def __init__(self, offset, name): self.__offset = datetime.timedelta(minutes=offset) self.__name = name def utcoffset(self, dt): return self.__offset def tzname(self, dt): return self.__name def dst(self, dt): return datetime.timedelta(0) then = datetime.datetime.now(FixedOffset( 60, "UTC+1")) + datetime.timedelta(days=1) res.set_cookie("a", "1", expires=then) assert res.headerlist[-1][0] == "Set-Cookie" val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() assert val[0] in ("Max-Age=86399", "Max-Age=86400") assert val[1] == "Path=/" assert val[2] == "a=1" assert val[3].startswith("expires")
def test_cookies(): res = Response() # test unicode value res.set_cookie("x", "test") # utf8 encoded eq_(res.headers.getall("set-cookie"), ["x=test; Path=/"]) r2 = res.merge_cookies(simple_app) r2 = BaseRequest.blank("/").get_response(r2) eq_(r2.headerlist, [("Content-Type", "text/html; charset=utf8"), ("Set-Cookie", "x=test; Path=/")])
def test_cookies(): res = Response() # test unicode value res.set_cookie("x", text_(b"\N{BLACK SQUARE}", "unicode_escape")) # utf8 encoded eq_(res.headers.getall("set-cookie"), ['x="\\342\\226\\240"; Path=/']) r2 = res.merge_cookies(simple_app) r2 = BaseRequest.blank("/").get_response(r2) eq_(r2.headerlist, [("Content-Type", "text/html; charset=utf8"), ("Set-Cookie", 'x="\\342\\226\\240"; Path=/')])
def test_set_cookie_value_is_None(): res = Response() res.set_cookie("a", None) eq_(res.headerlist[-1][0], "Set-Cookie") val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() eq_(val[0], "Max-Age=0") eq_(val[1], "Path=/") eq_(val[2], "a=") assert val[3].startswith("expires")
def test_set_cookie_expires_is_None_and_max_age_is_int(): res = Response() res.set_cookie('a', '1', max_age=100) assert res.headerlist[-1][0] == 'Set-Cookie' val = [x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() assert val[0] == 'Max-Age=100' assert val[1] == 'Path=/' assert val[2] == 'a=1' assert val[3].startswith('expires')
def test_set_cookie_value_is_None(): res = Response() res.set_cookie('a', None) eq_(res.headerlist[-1][0], 'Set-Cookie') val = [ x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() eq_(val[0], 'Max-Age=0') eq_(val[1], 'Path=/') eq_(val[2], 'a=') assert val[3].startswith('expires')
def test_set_cookie_value_is_None(): res = Response() res.set_cookie('a', None) eq_(res.headerlist[-1][0], 'Set-Cookie') val = [x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() eq_(val[0], 'Max-Age=0') eq_(val[1], 'Path=/') eq_(val[2], 'a=') assert val[3].startswith('expires')
def test_set_cookie_expires_is_None_and_max_age_is_int(): res = Response() res.set_cookie("a", "1", max_age=100) assert res.headerlist[-1][0] == "Set-Cookie" val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() assert val[0] == "Max-Age=100" assert val[1] == "Path=/" assert val[2] == "a=1" assert val[3].startswith("expires")
def test_set_cookie_expires_is_None_and_max_age_is_int(): res = Response() res.set_cookie("a", "1", max_age=100) assert res.headerlist[-1][0] == "Set-Cookie" val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() assert val[0] == "Max-Age=100" assert val[1] == "Path=/" assert val[2] == "a=1" assert val[3].startswith("expires")
def test_cookies(): res = Response() # test unicode value res.set_cookie('x', "test") # utf8 encoded assert res.headers.getall('set-cookie') == ['x=test; Path=/'] r2 = res.merge_cookies(simple_app) r2 = BaseRequest.blank('/').get_response(r2) assert r2.headerlist == [ ('Content-Type', 'text/html; charset=utf8'), ('Set-Cookie', 'x=test; Path=/'), ]
def test_set_cookie_expires_is_None_and_max_age_is_timedelta(): from datetime import timedelta res = Response() res.set_cookie('a', '1', max_age=timedelta(seconds=100)) eq_(res.headerlist[-1][0], 'Set-Cookie') val = [ x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() eq_(val[0], 'Max-Age=100') eq_(val[1], 'Path=/') eq_(val[2], 'a=1') assert val[3].startswith('expires')
def test_set_cookie_expires_is_None_and_max_age_is_timedelta(): from datetime import timedelta res = Response() res.set_cookie('a', '1', max_age=timedelta(seconds=100)) eq_(res.headerlist[-1][0], 'Set-Cookie') val = [x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() eq_(val[0], 'Max-Age=100') eq_(val[1], 'Path=/') eq_(val[2], 'a=1') assert val[3].startswith('expires')
def test_cookies(): res = Response() # test unicode value res.set_cookie("x", "test") # utf8 encoded assert res.headers.getall("set-cookie") == ["x=test; Path=/"] r2 = res.merge_cookies(simple_app) r2 = BaseRequest.blank("/").get_response(r2) assert r2.headerlist == [ ("Content-Type", "text/html; charset=UTF-8"), ("Set-Cookie", "x=test; Path=/"), ]
def test_cookies(): res = Response() # test unicode value res.set_cookie('x', text_(b'\N{BLACK SQUARE}', 'unicode_escape')) # utf8 encoded eq_(res.headers.getall('set-cookie'), ['x="\\342\\226\\240"; Path=/']) r2 = res.merge_cookies(simple_app) r2 = BaseRequest.blank('/').get_response(r2) eq_(r2.headerlist, [ ('Content-Type', 'text/html; charset=utf8'), ('Set-Cookie', 'x="\\342\\226\\240"; Path=/'), ])
def test_set_cookie_expires_is_None_and_max_age_is_timedelta(): from datetime import timedelta res = Response() res.set_cookie("a", "1", max_age=timedelta(seconds=100)) eq_(res.headerlist[-1][0], "Set-Cookie") val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() eq_(val[0], "Max-Age=100") eq_(val[1], "Path=/") eq_(val[2], "a=1") assert val[3].startswith("expires")
def test_response_cookie_ok(self): '''Test case that ensures cookies can be correctly set to a response instance.''' response = Response() response.set_cookie("fantastico.sid", "12345", max_age=360, path="/", secure=True) cookie = response.headers["Set-Cookie"] self.assertGreater(cookie.find("fantastico.sid=12345"), -1) self.assertGreater(cookie.find("Max-Age=360"), -1) self.assertGreater(cookie.find("secure"), -1) self.assertGreater(cookie.find("Path=/"), -1)
def test_set_cookie_expires_is_not_None_and_max_age_is_None(): import datetime res = Response() then = datetime.datetime.utcnow() + datetime.timedelta(days=1) res.set_cookie('a', '1', expires=then) eq_(res.headerlist[-1][0], 'Set-Cookie') val = [ x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() ok_(val[0] in ('Max-Age=86399', 'Max-Age=86400')) eq_(val[1], 'Path=/') eq_(val[2], 'a=1') assert val[3].startswith('expires')
def test_set_cookie_expires_is_not_None_and_max_age_is_None(): import datetime res = Response() then = datetime.datetime.utcnow() + datetime.timedelta(days=1) res.set_cookie('a', '1', expires=then) eq_(res.headerlist[-1][0], 'Set-Cookie') val = [x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() ok_(val[0] in ('Max-Age=86399', 'Max-Age=86400')) eq_(val[1], 'Path=/') eq_(val[2], 'a=1') assert val[3].startswith('expires')
def test_set_cookie_expires_is_timedelta_and_max_age_is_None(): import datetime res = Response() then = datetime.timedelta(days=1) res.set_cookie('a', '1', expires=then) assert res.headerlist[-1][0] == 'Set-Cookie' val = [x.strip() for x in res.headerlist[-1][1].split(';')] assert len(val) == 4 val.sort() assert val[0] in ('Max-Age=86399', 'Max-Age=86400') assert val[1] == 'Path=/' assert val[2] == 'a=1' assert val[3].startswith('expires')
def test_cookies(): res = Response() # test unicode value res.set_cookie('x', text_(b'\N{BLACK SQUARE}', 'unicode_escape')) # utf8 encoded eq_(res.headers.getall('set-cookie'), ['x="\\342\\226\\240"; Path=/']) r2 = res.merge_cookies(simple_app) r2 = BaseRequest.blank('/').get_response(r2) eq_(r2.headerlist, [('Content-Type', 'text/html; charset=utf8'), ('Set-Cookie', 'x="\\342\\226\\240"; Path=/'), ] )
def test_set_cookie_expires_is_not_None_and_max_age_is_None(): import datetime res = Response() then = datetime.datetime.utcnow() + datetime.timedelta(days=1) res.set_cookie("a", "1", expires=then) eq_(res.headerlist[-1][0], "Set-Cookie") val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() ok_(val[0] in ("Max-Age=86399", "Max-Age=86400")) eq_(val[1], "Path=/") eq_(val[2], "a=1") assert val[3].startswith("expires")
def test_set_cookie_expires_is_timedelta_and_max_age_is_None(): import datetime res = Response() then = datetime.timedelta(days=1) res.set_cookie("a", "1", expires=then) assert res.headerlist[-1][0] == "Set-Cookie" val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() assert val[0] in ("Max-Age=86399", "Max-Age=86400") assert val[1] == "Path=/" assert val[2] == "a=1" assert val[3].startswith("expires")
def test_set_cookie_expires_is_timedelta_and_max_age_is_None(): import datetime res = Response() then = datetime.timedelta(days=1) res.set_cookie("a", "1", expires=then) assert res.headerlist[-1][0] == "Set-Cookie" val = [x.strip() for x in res.headerlist[-1][1].split(";")] assert len(val) == 4 val.sort() assert val[0] in ("Max-Age=86399", "Max-Age=86400") assert val[1] == "Path=/" assert val[2] == "a=1" assert val[3].startswith("expires")
def test_response(): req = BaseRequest.blank("/") res = req.get_response(simple_app) assert res.status == "200 OK" assert res.status_code == 200 assert res.body == "OK" assert res.charset == "UTF-8" assert res.content_type == "text/html" res.status = 404 assert res.status == "404 Not Found" assert res.status_code == 404 res.body = b"Not OK" assert b"".join(res.app_iter) == b"Not OK" res.charset = "iso8859-1" assert "text/html; charset=iso8859-1" == res.headers["content-type"] res.content_type = "text/xml" assert "text/xml; charset=UTF-8" == res.headers["content-type"] res.content_type = "text/xml; charset=UTF-8" assert "text/xml; charset=UTF-8" == res.headers["content-type"] res.headers = {"content-type": "text/html"} assert res.headers["content-type"] == "text/html" assert res.headerlist == [("content-type", "text/html")] res.set_cookie("x", "y") assert res.headers["set-cookie"].strip(";") == "x=y; Path=/" res.set_cookie(text_("x"), text_("y")) assert res.headers["set-cookie"].strip(";") == "x=y; Path=/" res = Response("a body", "200 OK", content_type="text/html") res.encode_content() assert res.content_encoding == "gzip" assert ( res.body == b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00" ) res.decode_content() assert res.content_encoding is None assert res.body == b"a body" res.set_cookie("x", text_(b"foo")) # test unicode value with pytest.raises(TypeError): Response(app_iter=iter(["a"]), body="somebody") del req.environ with pytest.raises(TypeError): Response(charset=None, content_type="image/jpeg", body=text_(b"unicode body")) with pytest.raises(TypeError): Response(wrong_key="dummy") with pytest.raises(TypeError): resp = Response() resp.body = text_(b"unicode body")
def test_merge_cookies_resp_is_wsgi_callable(): L = [] def dummy_wsgi_callable(environ, start_response): L.append((environ, start_response)) return 'abc' res = Response() res.set_cookie('a', '1') wsgiapp = res.merge_cookies(dummy_wsgi_callable) environ = {} def dummy_start_response(status, headers, exc_info=None): assert headers, [('Set-Cookie' == 'a=1; Path=/')] result = wsgiapp(environ, dummy_start_response) assert result == 'abc' assert len(L) == 1 L[0][1]('200 OK', []) # invoke dummy_start_response assertion
def test_cookies_warning_issued_backwards_compat(): import warnings with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. res = Response() res.set_cookie(key='x', value='test') assert len(w) == 1 assert issubclass(w[-1].category, DeprecationWarning) is True assert 'Argument "key" was renamed to "name".' in str(w[-1].message) cookies._should_raise = True
def test_response(): req = BaseRequest.blank("/") res = req.get_response(simple_app) assert res.status == "200 OK" assert res.status_code == 200 assert res.body == "OK" assert res.charset == "UTF-8" assert res.content_type == "text/html" res.status = 404 assert res.status == "404 Not Found" assert res.status_code == 404 res.body = b"Not OK" assert b"".join(res.app_iter) == b"Not OK" res.charset = "iso8859-1" assert "text/html; charset=iso8859-1" == res.headers["content-type"] res.content_type = "text/xml" assert "text/xml; charset=UTF-8" == res.headers["content-type"] res.content_type = "text/xml; charset=UTF-8" assert "text/xml; charset=UTF-8" == res.headers["content-type"] res.headers = {"content-type": "text/html"} assert res.headers["content-type"] == "text/html" assert res.headerlist == [("content-type", "text/html")] res.set_cookie("x", "y") assert res.headers["set-cookie"].strip(";") == "x=y; Path=/" res.set_cookie(text_("x"), text_("y")) assert res.headers["set-cookie"].strip(";") == "x=y; Path=/" res = Response("a body", "200 OK", content_type="text/html") res.encode_content() assert res.content_encoding == "gzip" assert ( res.body == b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00" ) res.decode_content() assert res.content_encoding is None assert res.body == b"a body" res.set_cookie("x", text_(b"foo")) # test unicode value with pytest.raises(TypeError): Response(app_iter=iter(["a"]), body="somebody") del req.environ with pytest.raises(TypeError): Response(charset=None, content_type="image/jpeg", body=text_(b"unicode body")) with pytest.raises(TypeError): Response(wrong_key="dummy") with pytest.raises(TypeError): resp = Response() resp.body = text_(b"unicode body")
def test_response(): req = BaseRequest.blank('/') res = req.get_response(simple_app) assert res.status == '200 OK' assert res.status_code == 200 assert res.body == "OK" assert res.charset == "UTF-8" assert res.content_type == 'text/html' res.status = 404 assert res.status == '404 Not Found' assert res.status_code == 404 res.body = b'Not OK' assert b''.join(res.app_iter) == b'Not OK' res.charset = 'iso8859-1' assert 'text/html; charset=iso8859-1' == res.headers['content-type'] res.content_type = 'text/xml' assert 'text/xml; charset=UTF-8' == res.headers['content-type'] res.content_type = 'text/xml; charset=UTF-8' assert 'text/xml; charset=UTF-8' == res.headers['content-type'] res.headers = {'content-type': 'text/html'} assert res.headers['content-type'] == 'text/html' assert res.headerlist == [('content-type', 'text/html')] res.set_cookie('x', 'y') assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/' res.set_cookie(text_('x'), text_('y')) assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/' res = Response('a body', '200 OK', content_type='text/html') res.encode_content() assert res.content_encoding == 'gzip' assert res.body == b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00' res.decode_content() assert res.content_encoding is None assert res.body == b'a body' res.set_cookie('x', text_(b'foo')) # test unicode value with pytest.raises(TypeError): Response(app_iter=iter(['a']), body="somebody") del req.environ with pytest.raises(TypeError): Response(charset=None, content_type='image/jpeg', body=text_(b"unicode body")) with pytest.raises(TypeError): Response(wrong_key='dummy') with pytest.raises(TypeError): resp = Response() resp.body = text_(b"unicode body")
def test_response(): req = BaseRequest.blank('/') res = req.get_response(simple_app) assert res.status == '200 OK' assert res.status_code == 200 assert res.body == "OK" assert res.charset == "UTF-8" assert res.content_type == 'text/html' res.status = 404 assert res.status == '404 Not Found' assert res.status_code == 404 res.body = b'Not OK' assert b''.join(res.app_iter) == b'Not OK' res.charset = 'iso8859-1' assert 'text/html; charset=iso8859-1' == res.headers['content-type'] res.content_type = 'text/xml' assert 'text/xml; charset=UTF-8' == res.headers['content-type'] res.content_type = 'text/xml; charset=UTF-8' assert 'text/xml; charset=UTF-8' == res.headers['content-type'] res.headers = {'content-type': 'text/html'} assert res.headers['content-type'] == 'text/html' assert res.headerlist == [('content-type', 'text/html')] res.set_cookie('x', 'y') assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/' res.set_cookie(text_('x'), text_('y')) assert res.headers['set-cookie'].strip(';') == 'x=y; Path=/' res = Response('a body', '200 OK', content_type='text/html') res.encode_content() assert res.content_encoding == 'gzip' assert res.body == b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xffKTH\xcaO\xa9\x04\x00\xf6\x86GI\x06\x00\x00\x00' res.decode_content() assert res.content_encoding is None assert res.body == b'a body' res.set_cookie('x', text_(b'foo')) # test unicode value with pytest.raises(TypeError): Response(app_iter=iter(['a']), body="somebody") del req.environ with pytest.raises(TypeError): Response(charset=None, content_type='image/jpeg', body=text_(b"unicode body")) with pytest.raises(TypeError): Response(wrong_key='dummy') with pytest.raises(TypeError): resp = Response() resp.body = text_(b"unicode body")
def test_response_cookie_ok(self): '''Test case that ensures cookies can be correctly set to a response instance.''' response = Response() response.set_cookie("fantastico.sid", "12345", max_age=360, path="/", secure=True) cookie = response.headers["Set-Cookie"] self.assertGreater(cookie.find("fantastico.sid=12345"), -1) self.assertGreater(cookie.find("Max-Age=360"), -1) self.assertGreater(cookie.find("secure"), -1) self.assertGreater(cookie.find("Path=/"), -1)
def get_index(self, request): """This method returns the index of todo ui application.""" append_cookie = False userid = request.cookies.get(self._USERID_COOKIE) if not userid: append_cookie = True userid = uuid.uuid4() content = self.load_template("listing.html") response = Response(content) if append_cookie: response.set_cookie(self._USERID_COOKIE, value=str(userid), max_age=self._MAX_AGE, path="/", secure=False) return response
def test_unicode_cookies_warning_issued(): import warnings cookies._should_raise = False with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. res = Response() res.set_cookie('x', text_(b'\N{BLACK SQUARE}', 'unicode_escape')) assert len(w) == 1 assert issubclass(w[-1].category, RuntimeWarning) is True assert "ValueError" in str(w[-1].message) cookies._should_raise = True
def test_unicode_cookies_warning_issued(): import warnings cookies._should_raise = False with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. res = Response() res.set_cookie("x", text_(b"\N{BLACK SQUARE}", "unicode_escape")) eq_(len(w), 1) eq_(issubclass(w[-1].category, RuntimeWarning), True) eq_("ValueError" in str(w[-1].message), True) cookies._should_raise = True
def test_merge_cookies_resp_is_wsgi_callable(): L = [] def dummy_wsgi_callable(environ, start_response): L.append((environ, start_response)) return "abc" res = Response() res.set_cookie("a", "1") wsgiapp = res.merge_cookies(dummy_wsgi_callable) environ = {} def dummy_start_response(status, headers, exc_info=None): assert headers, [("Set-Cookie" == "a=1; Path=/")] result = wsgiapp(environ, dummy_start_response) assert result == "abc" assert len(L) == 1 L[0][1]("200 OK", []) # invoke dummy_start_response assertion
def test_merge_cookies_resp_is_wsgi_callable(): L = [] def dummy_wsgi_callable(environ, start_response): L.append((environ, start_response)) return "abc" res = Response() res.set_cookie("a", "1") wsgiapp = res.merge_cookies(dummy_wsgi_callable) environ = {} def dummy_start_response(status, headers, exc_info=None): assert headers, [("Set-Cookie" == "a=1; Path=/")] result = wsgiapp(environ, dummy_start_response) assert result == "abc" assert len(L) == 1 L[0][1]("200 OK", []) # invoke dummy_start_response assertion
def test_set_cookie_overwrite(): res = Response() res.set_cookie("a", "1") res.set_cookie("a", "2", overwrite=True) assert res.headerlist[-1] == ("Set-Cookie", "a=2; Path=/")
def test_merge_cookies_resp_is_Response(): inner_res = Response() res = Response() res.set_cookie('a', '1') result = res.merge_cookies(inner_res) assert result.headers.getall('Set-Cookie') == ['a=1; Path=/']
def test_set_cookie_overwrite(): res = Response() res.set_cookie("a", "1") res.set_cookie("a", "2", overwrite=True) assert res.headerlist[-1] == ("Set-Cookie", "a=2; Path=/")
def test_unicode_cookies_error_raised(): res = Response() with pytest.raises(ValueError): Response.set_cookie(res, "x", text_(b"\\N{BLACK SQUARE}", "unicode_escape"))
def test_set_cookie_overwrite(): res = Response() res.set_cookie('a', '1') res.set_cookie('a', '2', overwrite=True) assert res.headerlist[-1] == ('Set-Cookie', 'a=2; Path=/')
def test_cookies_raises_typeerror(): res = Response() with pytest.raises(TypeError): res.set_cookie()
def test_set_cookie_value_is_unicode(): res = Response() val = text_(b'La Pe\xc3\xb1a', 'utf-8') res.set_cookie('a', val) eq_(res.headerlist[-1], ('Set-Cookie', 'a="La Pe\\303\\261a"; Path=/'))
def test_merge_cookies_resp_is_Response(): inner_res = Response() res = Response() res.set_cookie("a", "1") result = res.merge_cookies(inner_res) assert result.headers.getall("Set-Cookie") == ["a=1; Path=/"]