1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/net/http/cookie_test.v

443 lines
9.6 KiB
V
Raw Normal View History

2020-04-26 14:49:31 +03:00
import net.http
2020-04-14 04:34:05 +03:00
struct SetCookieTestCase {
cookie &http.Cookie
2021-06-14 10:08:41 +03:00
raw string
2020-04-14 04:34:05 +03:00
}
struct ReadSetCookiesTestCase {
2021-06-14 10:08:41 +03:00
header map[string][]string
2020-04-14 04:34:05 +03:00
cookies []&http.Cookie
}
struct AddCookieTestCase {
cookie []&http.Cookie
2021-06-14 10:08:41 +03:00
raw string
2020-04-14 04:34:05 +03:00
}
const (
write_set_cookie_tests = [
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-1'
value: 'v1'
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-1=v1'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-2'
value: 'two'
max_age: 3600
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-2=two; Max-Age=3600'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-3'
value: 'three'
domain: '.example.com'
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-3=three; domain=example.com'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-4'
value: 'four'
path: '/restricted/'
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-4=four; path=/restricted/'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-5'
value: 'five'
domain: 'wrong;bad.abc'
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-5=five'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-6'
value: 'six'
domain: 'bad-.abc'
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-6=six'
},
// SetCookieTestCase{
// cookie: &http.Cookie{name: 'cookie-7', value: 'seven', domain: '127.0.0.1'},
// raw: 'cookie-7=seven; domain=127.0.0.1'
// },
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-8'
value: 'eight'
domain: '::1'
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-8=eight'
},
// {
// cookie: &http.Cookie{name: 'cookie-9', value: 'expiring', expires: time.unix(1257894000, 0)},
// 'cookie-9=expiring; Expires=Tue, 10 Nov 2009 23:00:00 GMT',
// },
// According to IETF 6265 Section 5.1.1.5, the year cannot be less than 1601
// SetCookieTestCase{
// cookie: &http.Cookie{name: 'cookie-10', value: 'expiring-1601', expires: time.parse('Mon, 01 Jan 1601 01:01:01 GMT')},
// raw: 'cookie-10=expiring-1601; Expires=Mon, 01 Jan 1601 01:01:01 GMT'
// },
// SetCookieTestCase{
// cookie: &http.Cookie{name: 'cookie-11', value: 'invalid-expiry', expires: time.parse('Mon, 01 Jan 1600 01:01:01 GMT')},
// raw: 'cookie-11=invalid-expiry'
// },
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-12'
value: 'samesite-default'
same_site: .same_site_default_mode
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-12=samesite-default; SameSite'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-13'
value: 'samesite-lax'
same_site: .same_site_lax_mode
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-13=samesite-lax; SameSite=Lax'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-14'
value: 'samesite-strict'
same_site: .same_site_strict_mode
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-14=samesite-strict; SameSite=Strict'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'cookie-15'
value: 'samesite-none'
same_site: .same_site_none_mode
}
2020-04-14 04:34:05 +03:00
raw: 'cookie-15=samesite-none; SameSite=None'
},
// The 'special' cookies have values containing commas or spaces which
// are disallowed by RFC 6265 but are common in the wild.
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-1'
value: 'a z'
}
2020-04-14 04:34:05 +03:00
raw: 'special-1=a z'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-2'
value: ' z'
}
2020-04-14 04:34:05 +03:00
raw: 'special-2=" z"'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-3'
value: 'a '
}
2020-04-14 04:34:05 +03:00
raw: 'special-3="a "'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-4'
value: ' '
}
2020-04-14 04:34:05 +03:00
raw: 'special-4=" "'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-5'
value: 'a,z'
}
2020-04-14 04:34:05 +03:00
raw: 'special-5=a,z'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-6'
value: ',z'
}
2020-04-14 04:34:05 +03:00
raw: 'special-6=",z"'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-7'
value: 'a,'
}
2020-04-14 04:34:05 +03:00
raw: 'special-7="a,"'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'special-8'
value: ','
}
2020-04-14 04:34:05 +03:00
raw: 'special-8=","'
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'empty-value'
value: ''
}
2020-04-14 04:34:05 +03:00
raw: 'empty-value='
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: ''
}
2020-04-14 04:34:05 +03:00
raw: ''
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: '\t'
}
2020-04-14 04:34:05 +03:00
raw: ''
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: '\r'
}
2020-04-14 04:34:05 +03:00
raw: ''
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'a\nb'
value: 'v'
}
2020-04-14 04:34:05 +03:00
raw: ''
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'a\nb'
value: 'v'
}
2020-04-14 04:34:05 +03:00
raw: ''
},
SetCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: &http.Cookie{
name: 'a\rb'
value: 'v'
}
2020-04-14 04:34:05 +03:00
raw: ''
},
]
add_cookies_tests = [
AddCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: []
raw: ''
2020-04-14 04:34:05 +03:00
},
AddCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: [&http.Cookie{
name: 'cookie-1'
value: 'v1'
}]
raw: 'cookie-1=v1'
2020-04-14 04:34:05 +03:00
},
AddCookieTestCase{
2021-06-14 10:08:41 +03:00
cookie: [&http.Cookie{
name: 'cookie-1'
value: 'v1'
}, &http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'cookie-2'
value: 'v2'
},
&http.Cookie{
name: 'cookie-3'
value: 'v3'
}]
2021-06-14 10:08:41 +03:00
raw: 'cookie-1=v1; cookie-2=v2; cookie-3=v3'
},
2020-04-14 04:34:05 +03:00
]
read_set_cookies_tests = [
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['Cookie-1=v1']
}
cookies: [&http.Cookie{
name: 'Cookie-1'
value: 'v1'
raw: 'Cookie-1=v1'
}]
2020-04-14 04:34:05 +03:00
},
// ReadSetCookiesTestCase{
// header: {"Set-Cookie": ["NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"]},
// cookies: [&http.Cookie{
// name: "NID",
// value: "99=YsDT5i3E-CXax-",
// path: "/",
// domain: ".google.ch",
// http_only: true,
// expires: time.parse_iso('Wed, 23-Nov-2011 01:05:03 GMT'),
// raw_expires: "Wed, 23-Nov-2011 01:05:03 GMT",
// raw: "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"
// }]
// },
// ReadSetCookiesTestCase{
// header: {"Set-Cookie": [".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"]},
// cookies: [&http.Cookie{
// name: ".ASPXAUTH",
// value: "7E3AA",
// path: "/",
// expires: time.parse_iso('Wed, 07-Mar-2012 14:25:06 GMT'),
// raw_expires: "Wed, 07-Mar-2012 14:25:06 GMT",
// http_only: true,
// raw: ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"
// }]
// },
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['ASP.NET_SessionId=foo; path=/; HttpOnly']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'ASP.NET_SessionId'
value: 'foo'
path: '/'
http_only: true
raw: 'ASP.NET_SessionId=foo; path=/; HttpOnly'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['samesitedefault=foo; SameSite']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'samesitedefault'
value: 'foo'
same_site: .same_site_default_mode
raw: 'samesitedefault=foo; SameSite'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['samesitelax=foo; SameSite=Lax']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'samesitelax'
value: 'foo'
same_site: .same_site_lax_mode
raw: 'samesitelax=foo; SameSite=Lax'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['samesitestrict=foo; SameSite=Strict']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'samesitestrict'
value: 'foo'
same_site: .same_site_strict_mode
raw: 'samesitestrict=foo; SameSite=Strict'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['samesitenone=foo; SameSite=None']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'samesitenone'
value: 'foo'
same_site: .same_site_none_mode
raw: 'samesitenone=foo; SameSite=None'
}]
2020-04-14 04:34:05 +03:00
},
// Make sure we can properly read back the Set-Cookie headers we create
// for values containing spaces or commas:
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['special-1=a z']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'special-1'
value: 'a z'
raw: 'special-1=a z'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['special-2=" z"']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'special-2'
value: ' z'
raw: 'special-2=" z"'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['special-3="a "']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'special-3'
value: 'a '
raw: 'special-3="a "'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['special-4=" "']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'special-4'
value: ' '
raw: 'special-4=" "'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['special-5=a,z']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'special-5'
value: 'a,z'
raw: 'special-5=a,z'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['special-6=",z"']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'special-6'
value: ',z'
raw: 'special-6=",z"'
}]
2020-04-14 04:34:05 +03:00
},
ReadSetCookiesTestCase{
header: {
2021-06-14 10:08:41 +03:00
'Set-Cookie': ['special-7=","']
}
cookies: [&http.Cookie{
2021-06-14 10:08:41 +03:00
name: 'special-7'
value: ','
raw: 'special-8=","'
}]
},
// TODO(bradfitz): users have reported seeing this in the
2020-04-14 04:34:05 +03:00
// wild, but do browsers handle it? RFC 6265 just says "don't
// do that" (section 3) and then never mentions header folding
// again.
// Header{"Set-Cookie": ["ASP.NET_SessionId=foo; path=/; HttpOnly, .ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"]},
]
)
fn test_write_set_cookies() {
for _, tt in write_set_cookie_tests {
assert tt.cookie.str() == tt.raw
}
}
fn test_read_set_cookies() {
for _, tt in read_set_cookies_tests {
h := tt.header['Set-Cookie'][0]
c := http.read_set_cookies(tt.header)
println(h)
println(c[0].str())
assert c[0].str() == h
}
}