def test_delete_cookie(self): response = BaseResponse() response.set_cookie('name', 'value') response.delete_cookie('name') cookies = [value for name, value in response.headerlist if name.title() == 'Set-Cookie'] self.assertTrue('name=;' in cookies[0])
def test_clone_cookie(self): r = BaseResponse() r.set_cookie('name1', 'value', same_site="strict") r2 = r.copy(BaseResponse) cookies = sorted([value for name, value in r2.headerlist if name.title() == 'Set-Cookie']) self.assertEqual('name1=value; samesite=strict', cookies[0].lower())
def test_charset(self): rs = BaseResponse() self.assertEqual(rs.charset, 'UTF-8') rs.content_type = 'text/html; charset=latin9' self.assertEqual(rs.charset, 'latin9') rs.content_type = 'text/html' self.assertEqual(rs.charset, 'UTF-8')
def test_charset(self): rs = BaseResponse() self.assertEqual(rs.charset, "UTF-8") rs.content_type = "text/html; charset=latin9" self.assertEqual(rs.charset, "latin9") rs.content_type = "text/html" self.assertEqual(rs.charset, "UTF-8")
def test_set_cookie_secure(self): r = BaseResponse() r.set_cookie('name1', 'value', secure=True) r.set_cookie('name2', 'value', secure=False) cookies = sorted([value for name, value in r.headerlist if name.title() == 'Set-Cookie']) self.assertEqual(cookies[0].lower(), 'name1=value; secure') self.assertEqual(cookies[1], 'name2=value')
def test_set_cookie(self): r = BaseResponse() r.set_cookie("name1", "value", max_age=5) r.set_cookie("name2", "value 2", path="/foo") cookies = [value for name, value in r.headerlist if name.title() == "Set-Cookie"] cookies.sort() self.assertEqual(cookies[0], "name1=value; Max-Age=5") self.assertEqual(cookies[1], 'name2="value 2"; Path=/foo')
def test_set_cookie_maxage(self): import datetime r = BaseResponse() r.set_cookie("name1", "value", max_age=5) r.set_cookie("name2", "value", max_age=datetime.timedelta(days=1)) cookies = sorted([value for name, value in r.headerlist if name.title() == "Set-Cookie"]) self.assertEqual(cookies[0], "name1=value; Max-Age=5") self.assertEqual(cookies[1], "name2=value; Max-Age=86400")
def test_set_cookie(self): r = BaseResponse() r.set_cookie('name1', 'value', max_age=5) r.set_cookie('name2', 'value 2', path='/foo') cookies = [value for name, value in r.headerlist if name.title() == 'Set-Cookie'] cookies.sort() self.assertEqual(cookies[0], 'name1=value; Max-Age=5') self.assertEqual(cookies[1], 'name2="value 2"; Path=/foo')
def test_set_cookie_maxage(self): import datetime r = BaseResponse() r.set_cookie('name1', 'value', max_age=5) r.set_cookie('name2', 'value', max_age=datetime.timedelta(days=1)) cookies = sorted([value for name, value in r.headerlist if name.title() == 'Set-Cookie']) self.assertEqual(cookies[0], 'name1=value; Max-Age=5') self.assertEqual(cookies[1], 'name2=value; Max-Age=86400')
def test_set_cookie_expires(self): import datetime r = BaseResponse() r.set_cookie('name1', 'value', expires=42) r.set_cookie('name2', 'value', expires=datetime.datetime(1970,1,1,0,0,43)) cookies = sorted([value for name, value in r.headerlist if name.title() == 'Set-Cookie']) self.assertEqual(cookies[0], 'name1=value; expires=Thu, 01 Jan 1970 00:00:42 GMT') self.assertEqual(cookies[1], 'name2=value; expires=Thu, 01 Jan 1970 00:00:43 GMT')
def test_expires_header(self): import datetime response = BaseResponse() now = datetime.datetime.now() response.expires = now self.assertEqual(0, int((response.expires - now).total_seconds())) now2 = datetime.datetime.utcfromtimestamp( parse_date(response.headers['Expires'])) self.assertEqual(0, int((now - now2).total_seconds()))
def test_set_cookie_httponly(self): if sys.version_info < (2,6,0): return r = BaseResponse() r.set_cookie('name1', 'value', httponly=True) r.set_cookie('name2', 'value', httponly=False) cookies = sorted([value for name, value in r.headerlist if name.title() == 'Set-Cookie']) self.assertEqual(cookies[0].lower(), 'name1=value; httponly') self.assertEqual(cookies[1], 'name2=value')
def test_append_header(self): response = BaseResponse() response.set_header('x-test', 'foo') headers = [value for name, value in response.headerlist if name.title() == 'X-Test'] self.assertEqual(['foo'], headers) self.assertEqual('foo', response['x-test']) response.add_header('X-Test', 'bar') headers = [value for name, value in response.headerlist if name.title() == 'X-Test'] self.assertEqual(['foo', 'bar'], headers) self.assertEqual('bar', response['x-test'])
def test_expires_header(self): import datetime response = BaseResponse() now = datetime.datetime.now() response.expires = now def seconds(a, b): td = max(a,b) - min(a,b) return td.days*360*24 + td.seconds self.assertEqual(0, seconds(response.expires, now)) now2 = datetime.datetime.utcfromtimestamp( parse_date(response.headers['Expires'])) self.assertEqual(0, seconds(now, now2))
def test_set_cookie_samesite(self): r = BaseResponse() r.set_cookie('name1', 'value', same_site="lax") r.set_cookie('name2', 'value', same_site="strict") try: r.set_cookie('name3', 'value', same_site='invalid') self.fail("Should raise CookieError") except CookieError: pass cookies = sorted([value for name, value in r.headerlist if name.title() == 'Set-Cookie']) self.assertEqual('name1=value; samesite=lax', cookies[0].lower()) self.assertEqual('name2=value; samesite=strict', cookies[1].lower())
def callback(): update = request.json if update['event'] == 'message': updates.append(request.json) return BaseResponse(status=200)
def test_set_cookie_name_long_string(self): r = BaseResponse() self.assertRaises(ValueError, r.set_cookie, name='x' * 4097, value='simple_value')
def test_delete_header(self): response = BaseResponse() response['x-test'] = 'foo' self.assertEqual('foo', response['x-test']) del response['X-tESt'] self.assertRaises(KeyError, lambda: response['x-test'])
def test_content_type(self): rs = BaseResponse() rs.content_type = 'test/some' self.assertEquals('test/some', rs.headers.get('Content-Type'))
def test_constructor_body(self): self.assertEqual('', BaseResponse('').body) self.assertEqual('YAY', BaseResponse('YAY').body)
def test_non_string_header(self): response = BaseResponse() response['x-test'] = 5 self.assertEqual('5', response['x-test']) response['x-test'] = None self.assertEqual('None', response['x-test'])
def test_set_status(self): rs = BaseResponse() rs.status = 200 self.assertEqual(rs.status, rs.status_line) self.assertEqual(rs.status_code, 200) self.assertEqual(rs.status_line, '200 OK') rs.status = 999 self.assertEqual(rs.status, rs.status_line) self.assertEqual(rs.status_code, 999) self.assertEqual(rs.status_line, '999 Unknown') rs.status = 404 self.assertEqual(rs.status, rs.status_line) self.assertEqual(rs.status_code, 404) self.assertEqual(rs.status_line, '404 Not Found') def test(): rs.status = -200 self.assertRaises(ValueError, test) self.assertEqual(rs.status, rs.status_line) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, '404 Not Found') # last value def test(): rs.status = 5 self.assertRaises(ValueError, test) self.assertEqual(rs.status, rs.status_line) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, '404 Not Found') # last value rs.status = '999 Who knows?' # Illegal, but acceptable three digit code self.assertEqual(rs.status, rs.status_line) self.assertEqual(rs.status_code, 999) self.assertEqual(rs.status_line, '999 Who knows?') rs.status = 555 # Strange code self.assertEqual(rs.status, rs.status_line) self.assertEqual(rs.status_code, 555) self.assertEqual(rs.status_line, '555 Unknown') rs.status = '404 Brain not Found' # Custom reason self.assertEqual(rs.status, rs.status_line) self.assertEqual(rs.status_code, 404) self.assertEqual(rs.status_line, '404 Brain not Found') def test(): rs.status = '5 Illegal Code' self.assertRaises(ValueError, test) self.assertEqual(rs.status, rs.status_line) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, '404 Brain not Found') # last value def test(): rs.status = '-99 Illegal Code' self.assertRaises(ValueError, test) self.assertEqual(rs.status, rs.status_line) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, '404 Brain not Found') # last value def test(): rs.status = '1000 Illegal Code' self.assertRaises(ValueError, test) self.assertEqual(rs.status, rs.status_line) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, '404 Brain not Found') # last value def test(): rs.status = '555' # No reason self.assertRaises(ValueError, test) self.assertEqual(rs.status, rs.status_line) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, '404 Brain not Found') # last value
def cmp(app, wire): rs = BaseResponse() rs.set_header('x-test', app) result = [v for (h, v) in rs.headerlist if h.lower() == 'x-test'][0] self.assertEquals(wire, result)
def cmp(app, wire): rs = BaseResponse() rs.set_header('x-test', app) result = [v for (h, v) in rs.headerlist if h.lower()=='x-test'][0] self.assertEqual(wire, result)
def test_set_status(self): rs = BaseResponse() rs.status = 200 self.assertEqual(rs.status, 200) self.assertEqual(rs.status_code, 200) self.assertEqual(rs.status_line, "200 OK") rs.status = 999 self.assertEqual(rs.status, 999) self.assertEqual(rs.status_code, 999) self.assertEqual(rs.status_line, "999 Unknown") rs.status = 404 self.assertEqual(rs.status, 404) self.assertEqual(rs.status_code, 404) self.assertEqual(rs.status_line, "404 Not Found") def test(): rs.status = -200 self.assertRaises(ValueError, test) self.assertEqual(rs.status, 404) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, "404 Not Found") # last value def test(): rs.status = 5 self.assertRaises(ValueError, test) self.assertEqual(rs.status, 404) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, "404 Not Found") # last value rs.status = "999 Who knows?" # Illegal, but acceptable three digit code self.assertEqual(rs.status, 999) self.assertEqual(rs.status_code, 999) self.assertEqual(rs.status_line, "999 Who knows?") rs.status = 555 # Strange code self.assertEqual(rs.status, 555) self.assertEqual(rs.status_code, 555) self.assertEqual(rs.status_line, "555 Unknown") rs.status = "404 Brain not Found" # Custom reason self.assertEqual(rs.status, 404) self.assertEqual(rs.status_code, 404) self.assertEqual(rs.status_line, "404 Brain not Found") def test(): rs.status = "5 Illegal Code" self.assertRaises(ValueError, test) self.assertEqual(rs.status, 404) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, "404 Brain not Found") # last value def test(): rs.status = "-99 Illegal Code" self.assertRaises(ValueError, test) self.assertEqual(rs.status, 404) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, "404 Brain not Found") # last value def test(): rs.status = "1000 Illegal Code" self.assertRaises(ValueError, test) self.assertEqual(rs.status, 404) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, "404 Brain not Found") # last value def test(): rs.status = "555" # No reason self.assertRaises(ValueError, test) self.assertEqual(rs.status, 404) # last value self.assertEqual(rs.status_code, 404) # last value self.assertEqual(rs.status_line, "404 Brain not Found") # last value
def test_content_type(self): rs = BaseResponse() rs.content_type = "test/some" self.assertEquals("test/some", rs.headers.get("Content-Type"))