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

vweb: refactor form parsing and add tests (#9051)

This commit is contained in:
Miccah
2021-03-04 10:00:03 -06:00
committed by GitHub
parent 7f830fac86
commit 709d7460de
3 changed files with 157 additions and 101 deletions

View File

@ -76,3 +76,62 @@ fn test_parse_request_line() {
assert target.str() == '/target'
assert version == .v1_1
}
fn test_parse_form() {
assert parse_form('foo=bar&bar=baz') == map{
'foo': 'bar'
'bar': 'baz'
}
assert parse_form('foo=bar=&bar=baz') == map{
'foo': 'bar='
'bar': 'baz'
}
assert parse_form('foo=bar%3D&bar=baz') == map{
'foo': 'bar='
'bar': 'baz'
}
assert parse_form('foo=b%26ar&bar=baz') == map{
'foo': 'b&ar'
'bar': 'baz'
}
assert parse_form('a=b& c=d') == map{
'a': 'b'
' c': 'd'
}
assert parse_form('a=b&c= d ') == map{
'a': 'b'
'c': ' d '
}
}
fn test_parse_multipart_form() {
boundary := '6844a625b1f0b299'
names := ['foo', 'fooz']
file := 'bar.v'
ct := 'application/octet-stream'
contents := ['baz', 'buzz']
data := '--------------------------$boundary
Content-Disposition: form-data; name=\"${names[0]}\"; filename=\"$file\"
Content-Type: $ct
${contents[0]}
--------------------------$boundary
Content-Disposition: form-data; name=\"${names[1]}\"
${contents[1]}
--------------------------$boundary--
'
form, files := parse_multipart_form(data, boundary)
// TODO: remove newlines
assert files == map{
names[0]: [FileData{
filename: file
content_type: ct
data: contents[0] + '\n'
}]
}
assert form == map{
names[1]: contents[1] + '\n'
}
}