vweb: add test for new cookie fields

pull/10450/head
bettafish 2021-06-16 15:35:29 +02:00
parent e4065ed8d3
commit bee337877c
1 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,52 @@
import vweb
import net.http
const (
port = 8000
)
struct App {
vweb.Context
}
fn (mut app App) index() vweb.Result {
app.set_cookie(vweb.Cookie{
name: 'darkmode'
value: '0'
path: '/' // <------ new
domain: '127.0.0.1' // <------ new
})
if cookie := app.get_cookie('darkmode') {
println(cookie)
}
return app.html('<h1>Hello world</h1>')
}
fn main() {
go vweb.run(&App{}, port)
resp := http.get('http://127.0.0.1:$port/') or { panic('server not running') }
darkmode := resp.cookies['darkmode'] or { panic('cookie not set') }
value := darkmode[0].ascii_str().int()
mut path := ''
mut domain := ''
path_index := darkmode.index('path=') or { panic("path doesn't exist") } + 5
domain_index := darkmode.index('domain=') or { panic("domain doesn't exist") } + 7
for ch in darkmode[path_index..] {
if ch == `;` {
break
}
path += ch.ascii_str()
}
for ch in darkmode[domain_index..] {
if ch == `;` {
break
}
domain += ch.ascii_str()
}
assert value == 0
assert path == '/'
assert domain == '127.0.0.1'
println(resp.cookies)
}