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

net: add parse_headers function and handle header line folding (#10936)

Closes https://github.com/vlang/v/issues/10930
This commit is contained in:
Miccah
2021-07-24 03:31:33 -05:00
committed by GitHub
parent 304f26edeb
commit 0acb84d5a5
4 changed files with 102 additions and 35 deletions

View File

@ -1,7 +1,7 @@
module http
fn test_header_new() {
h := new_header({ key: .accept, value: 'nothing' },
h := new_header(HeaderConfig{ key: .accept, value: 'nothing' },
key: .expires
value: 'yesterday'
)
@ -37,7 +37,7 @@ fn test_header_get() ? {
}
fn test_header_set() ? {
mut h := new_header({ key: .dnt, value: 'one' },
mut h := new_header(HeaderConfig{ key: .dnt, value: 'one' },
key: .dnt
value: 'two'
)
@ -47,7 +47,7 @@ fn test_header_set() ? {
}
fn test_header_delete() {
mut h := new_header({ key: .dnt, value: 'one' },
mut h := new_header(HeaderConfig{ key: .dnt, value: 'one' },
key: .dnt
value: 'two'
)
@ -323,3 +323,39 @@ fn test_header_join() ? {
assert h3.contains_custom('Server')
assert h3.contains_custom('foo')
}
fn parse_headers_test(s string, expected map[string]string) ? {
assert parse_headers(s) ? == new_custom_header_from_map(expected) ?
}
fn test_parse_headers() ? {
parse_headers_test('foo: bar', map{
'foo': 'bar'
}) ?
parse_headers_test('foo: \t bar', map{
'foo': 'bar'
}) ?
parse_headers_test('foo: bar\r\n\tbaz', map{
'foo': 'bar baz'
}) ?
parse_headers_test('foo: bar \r\n\tbaz\r\n buzz', map{
'foo': 'bar baz buzz'
}) ?
parse_headers_test('foo: bar\r\nbar:baz', map{
'foo': 'bar'
'bar': 'baz'
}) ?
parse_headers_test('foo: bar\r\nbar:baz\r\n', map{
'foo': 'bar'
'bar': 'baz'
}) ?
parse_headers_test('foo: bar\r\nbar:baz\r\n\r\n', map{
'foo': 'bar'
'bar': 'baz'
}) ?
assert parse_headers('foo: bar\r\nfoo:baz') ?.custom_values('foo') == ['bar', 'baz']
if x := parse_headers(' oops: oh no') {
return error('should have errored, but got $x')
}
}