2021-04-08 03:12:46 +03:00
|
|
|
module http
|
2021-02-28 00:54:27 +03:00
|
|
|
|
|
|
|
fn test_header_new() {
|
2021-07-24 11:31:33 +03:00
|
|
|
h := new_header(HeaderConfig{ key: .accept, value: 'nothing' },
|
2021-06-14 10:08:41 +03:00
|
|
|
key: .expires
|
|
|
|
value: 'yesterday'
|
2021-02-28 00:54:27 +03:00
|
|
|
)
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.contains(.accept)
|
2021-02-28 00:54:27 +03:00
|
|
|
assert h.contains(.expires)
|
|
|
|
accept := h.get(.accept) or { '' }
|
|
|
|
expires := h.get(.expires) or { '' }
|
|
|
|
assert accept == 'nothing'
|
|
|
|
assert expires == 'yesterday'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_header_invalid_key() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add_custom('space is invalid', ':(') or { return }
|
2021-02-28 00:54:27 +03:00
|
|
|
panic('should have returned')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_header_adds_multiple() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2021-02-28 00:54:27 +03:00
|
|
|
h.add(.accept, 'one')
|
|
|
|
h.add(.accept, 'two')
|
|
|
|
|
2021-06-14 10:08:41 +03:00
|
|
|
assert h.values(.accept) == ['one', 'two']
|
2021-02-28 00:54:27 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_header_get() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header(key: .dnt, value: 'one')
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('dnt', 'two')!
|
2021-04-08 03:12:46 +03:00
|
|
|
dnt := h.get_custom('dnt') or { '' }
|
|
|
|
exact := h.get_custom('dnt', exact: true) or { '' }
|
|
|
|
assert dnt == 'one'
|
|
|
|
assert exact == 'two'
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_header_set() {
|
2021-07-24 11:31:33 +03:00
|
|
|
mut h := new_header(HeaderConfig{ key: .dnt, value: 'one' },
|
2021-06-14 10:08:41 +03:00
|
|
|
key: .dnt
|
|
|
|
value: 'two'
|
2021-02-28 00:54:27 +03:00
|
|
|
)
|
2021-06-14 10:08:41 +03:00
|
|
|
assert h.values(.dnt) == ['one', 'two']
|
2023-01-10 00:47:03 +03:00
|
|
|
h.set_custom('DNT', 'three')!
|
2021-02-28 00:54:27 +03:00
|
|
|
assert h.values(.dnt) == ['three']
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_header_delete() {
|
2021-07-24 11:31:33 +03:00
|
|
|
mut h := new_header(HeaderConfig{ key: .dnt, value: 'one' },
|
2021-06-14 10:08:41 +03:00
|
|
|
key: .dnt
|
|
|
|
value: 'two'
|
2021-02-28 00:54:27 +03:00
|
|
|
)
|
2021-06-14 10:08:41 +03:00
|
|
|
assert h.values(.dnt) == ['one', 'two']
|
2021-04-08 03:12:46 +03:00
|
|
|
h.delete(.dnt)
|
2021-02-28 00:54:27 +03:00
|
|
|
assert h.values(.dnt) == []
|
|
|
|
}
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
fn test_header_delete_not_existing() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.data.len == 0
|
|
|
|
assert h.keys.len == 0
|
|
|
|
h.delete(.dnt)
|
|
|
|
assert h.data.len == 0
|
|
|
|
assert h.keys.len == 0
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_custom_header() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('AbC', 'dEf')!
|
|
|
|
h.add_custom('aBc', 'GhI')!
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.custom_values('AbC', exact: true) == ['dEf']
|
|
|
|
assert h.custom_values('aBc', exact: true) == ['GhI']
|
|
|
|
assert h.custom_values('ABC') == ['dEf', 'GhI']
|
|
|
|
assert h.custom_values('abc') == ['dEf', 'GhI']
|
|
|
|
assert h.keys() == ['AbC', 'aBc']
|
|
|
|
h.delete_custom('AbC')
|
|
|
|
h.delete_custom('aBc')
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('abc', 'def')!
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.custom_values('abc') == ['def']
|
|
|
|
assert h.custom_values('ABC') == ['def']
|
|
|
|
assert h.keys() == ['abc']
|
|
|
|
h.delete_custom('abc')
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('accEPT', '*/*')!
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.custom_values('ACCept') == ['*/*']
|
|
|
|
assert h.values(.accept) == ['*/*']
|
|
|
|
assert h.keys() == ['accEPT']
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_contains_custom() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('Hello', 'world')!
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.contains_custom('hello')
|
|
|
|
assert h.contains_custom('HELLO')
|
|
|
|
assert h.contains_custom('Hello', exact: true)
|
|
|
|
assert h.contains_custom('hello', exact: true) == false
|
|
|
|
assert h.contains_custom('HELLO', exact: true) == false
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_get_custom() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('Hello', 'world')!
|
2022-05-13 06:56:21 +03:00
|
|
|
assert h.get_custom('hello')? == 'world'
|
|
|
|
assert h.get_custom('HELLO')? == 'world'
|
|
|
|
assert h.get_custom('Hello', exact: true)? == 'world'
|
2021-04-08 03:12:46 +03:00
|
|
|
if _ := h.get_custom('hello', exact: true) {
|
|
|
|
// should be none
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
if _ := h.get_custom('HELLO', exact: true) {
|
|
|
|
// should be none
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_starting_with() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('Hello-1', 'world')!
|
|
|
|
h.add_custom('Hello-21', 'world')!
|
2022-05-13 06:56:21 +03:00
|
|
|
assert h.starting_with('Hello-')? == 'Hello-1'
|
|
|
|
assert h.starting_with('Hello-2')? == 'Hello-21'
|
2021-06-07 11:18:40 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_custom_values() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('Hello', 'world')!
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.custom_values('hello') == ['world']
|
|
|
|
assert h.custom_values('HELLO') == ['world']
|
|
|
|
assert h.custom_values('Hello', exact: true) == ['world']
|
|
|
|
assert h.custom_values('hello', exact: true) == []
|
|
|
|
assert h.custom_values('HELLO', exact: true) == []
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_coerce() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('accept', 'foo')!
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add(.accept, 'bar')
|
|
|
|
assert h.values(.accept) == ['foo', 'bar']
|
|
|
|
assert h.keys().len == 2
|
|
|
|
|
|
|
|
h.coerce()
|
|
|
|
assert h.values(.accept) == ['foo', 'bar']
|
|
|
|
assert h.keys() == ['accept'] // takes the first occurrence
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_coerce_canonicalize() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('accept', 'foo')!
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add(.accept, 'bar')
|
|
|
|
assert h.values(.accept) == ['foo', 'bar']
|
|
|
|
assert h.keys().len == 2
|
|
|
|
|
|
|
|
h.coerce(canonicalize: true)
|
|
|
|
assert h.values(.accept) == ['foo', 'bar']
|
|
|
|
assert h.keys() == ['Accept'] // canonicalize header
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_coerce_custom() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('Hello', 'foo')!
|
|
|
|
h.add_custom('hello', 'bar')!
|
|
|
|
h.add_custom('HELLO', 'baz')!
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.custom_values('hello') == ['foo', 'bar', 'baz']
|
|
|
|
assert h.keys().len == 3
|
|
|
|
|
|
|
|
h.coerce()
|
|
|
|
assert h.custom_values('hello') == ['foo', 'bar', 'baz']
|
|
|
|
assert h.keys() == ['Hello'] // takes the first occurrence
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_coerce_canonicalize_custom() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('foo-BAR', 'foo')!
|
|
|
|
h.add_custom('FOO-bar', 'bar')!
|
2021-04-08 03:12:46 +03:00
|
|
|
assert h.custom_values('foo-bar') == ['foo', 'bar']
|
|
|
|
assert h.keys().len == 2
|
|
|
|
|
|
|
|
h.coerce(canonicalize: true)
|
|
|
|
assert h.custom_values('foo-bar') == ['foo', 'bar']
|
|
|
|
assert h.keys() == ['Foo-Bar'] // capitalizes the header
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_render_version() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('accept', 'foo')!
|
|
|
|
h.add_custom('Accept', 'bar')!
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add(.accept, 'baz')
|
|
|
|
|
|
|
|
s1_0 := h.render(version: .v1_0)
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_0.contains('accept: foo\r\n')
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_0.contains('Accept: bar\r\n')
|
|
|
|
assert s1_0.contains('Accept: baz\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s1_1 := h.render(version: .v1_1)
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_1.contains('accept: foo\r\n')
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_1.contains('Accept: bar\r\n')
|
|
|
|
assert s1_1.contains('Accept: baz\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s2_0 := h.render(version: .v2_0)
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s2_0.contains('accept: foo\r\n')
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s2_0.contains('accept: bar\r\n')
|
|
|
|
assert s2_0.contains('accept: baz\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_render_coerce() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('accept', 'foo')!
|
|
|
|
h.add_custom('Accept', 'bar')!
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add(.accept, 'baz')
|
|
|
|
h.add(.host, 'host')
|
|
|
|
|
|
|
|
s1_0 := h.render(version: .v1_1, coerce: true)
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_0.contains('accept: foo\r\n')
|
|
|
|
assert s1_0.contains('accept: bar\r\n')
|
|
|
|
assert s1_0.contains('accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_0.contains('Host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s1_1 := h.render(version: .v1_1, coerce: true)
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_1.contains('accept: foo\r\n')
|
|
|
|
assert s1_1.contains('accept: bar\r\n')
|
|
|
|
assert s1_1.contains('accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_1.contains('Host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s2_0 := h.render(version: .v2_0, coerce: true)
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s2_0.contains('accept: foo\r\n')
|
|
|
|
assert s2_0.contains('accept: bar\r\n')
|
|
|
|
assert s2_0.contains('accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s2_0.contains('host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_render_canonicalize() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('accept', 'foo')!
|
|
|
|
h.add_custom('Accept', 'bar')!
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add(.accept, 'baz')
|
|
|
|
h.add(.host, 'host')
|
|
|
|
|
|
|
|
s1_0 := h.render(version: .v1_1, canonicalize: true)
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_0.contains('Accept: foo\r\n')
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_0.contains('Accept: bar\r\n')
|
|
|
|
assert s1_0.contains('Accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_0.contains('Host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s1_1 := h.render(version: .v1_1, canonicalize: true)
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_1.contains('Accept: foo\r\n')
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_1.contains('Accept: bar\r\n')
|
|
|
|
assert s1_1.contains('Accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_1.contains('Host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s2_0 := h.render(version: .v2_0, canonicalize: true)
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s2_0.contains('accept: foo\r\n')
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s2_0.contains('accept: bar\r\n')
|
|
|
|
assert s2_0.contains('accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s2_0.contains('host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_render_coerce_canonicalize() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('accept', 'foo')!
|
|
|
|
h.add_custom('Accept', 'bar')!
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add(.accept, 'baz')
|
|
|
|
h.add(.host, 'host')
|
|
|
|
|
|
|
|
s1_0 := h.render(version: .v1_1, coerce: true, canonicalize: true)
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_0.contains('Accept: foo\r\n')
|
|
|
|
assert s1_0.contains('Accept: bar\r\n')
|
|
|
|
assert s1_0.contains('Accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_0.contains('Host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s1_1 := h.render(version: .v1_1, coerce: true, canonicalize: true)
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s1_1.contains('Accept: foo\r\n')
|
|
|
|
assert s1_1.contains('Accept: bar\r\n')
|
|
|
|
assert s1_1.contains('Accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s1_1.contains('Host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
s2_0 := h.render(version: .v2_0, coerce: true, canonicalize: true)
|
2021-08-23 00:38:09 +03:00
|
|
|
assert s2_0.contains('accept: foo\r\n')
|
|
|
|
assert s2_0.contains('accept: bar\r\n')
|
|
|
|
assert s2_0.contains('accept: baz\r\n')
|
2021-06-06 00:43:14 +03:00
|
|
|
assert s2_0.contains('host: host\r\n')
|
2021-04-08 03:12:46 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_str() {
|
2021-06-14 10:08:41 +03:00
|
|
|
mut h := new_header()
|
2021-04-08 03:12:46 +03:00
|
|
|
h.add(.accept, 'text/html')
|
2023-01-10 00:47:03 +03:00
|
|
|
h.add_custom('Accept', 'image/jpeg')!
|
|
|
|
h.add_custom('X-custom', 'Hello')!
|
2021-04-08 03:12:46 +03:00
|
|
|
|
|
|
|
// key order is not guaranteed
|
2021-08-23 00:38:09 +03:00
|
|
|
assert h.str() == 'Accept: text/html\r\nAccept: image/jpeg\r\nX-custom: Hello\r\n'
|
|
|
|
|| h.str() == 'X-custom: Hello\r\nAccept:text/html\r\nAccept: image/jpeg\r\n'
|
2021-04-08 03:12:46 +03:00
|
|
|
}
|
2021-07-12 12:16:41 +03:00
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_header_from_map() {
|
2021-08-04 12:44:41 +03:00
|
|
|
h := new_header_from_map({
|
2021-07-12 12:16:41 +03:00
|
|
|
CommonHeader.accept: 'nothing'
|
|
|
|
CommonHeader.expires: 'yesterday'
|
|
|
|
})
|
|
|
|
assert h.contains(.accept)
|
|
|
|
assert h.contains(.expires)
|
|
|
|
assert h.get(.accept) or { '' } == 'nothing'
|
|
|
|
assert h.get(.expires) or { '' } == 'yesterday'
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_custom_header_from_map() {
|
2021-08-04 12:44:41 +03:00
|
|
|
h := new_custom_header_from_map({
|
2021-07-12 12:16:41 +03:00
|
|
|
'Server': 'VWeb'
|
|
|
|
'foo': 'bar'
|
2023-01-10 00:47:03 +03:00
|
|
|
})!
|
2021-07-12 12:16:41 +03:00
|
|
|
assert h.contains_custom('server')
|
|
|
|
assert h.contains_custom('foo')
|
|
|
|
assert h.get_custom('server') or { '' } == 'VWeb'
|
|
|
|
assert h.get_custom('foo') or { '' } == 'bar'
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_header_join() {
|
2021-08-04 12:44:41 +03:00
|
|
|
h1 := new_header_from_map({
|
2021-07-12 12:16:41 +03:00
|
|
|
CommonHeader.accept: 'nothing'
|
|
|
|
CommonHeader.expires: 'yesterday'
|
|
|
|
})
|
2021-08-04 12:44:41 +03:00
|
|
|
h2 := new_custom_header_from_map({
|
2021-07-12 12:16:41 +03:00
|
|
|
'Server': 'VWeb'
|
|
|
|
'foo': 'bar'
|
2023-01-10 00:47:03 +03:00
|
|
|
})!
|
2021-07-12 12:16:41 +03:00
|
|
|
h3 := h1.join(h2)
|
|
|
|
// h1 is unchanged
|
|
|
|
assert h1.contains(.accept)
|
|
|
|
assert h1.contains(.expires)
|
|
|
|
assert !h1.contains_custom('Server')
|
|
|
|
assert !h1.contains_custom('foo')
|
|
|
|
// h2 is unchanged
|
|
|
|
assert !h2.contains(.accept)
|
|
|
|
assert !h2.contains(.expires)
|
|
|
|
assert h2.contains_custom('Server')
|
|
|
|
assert h2.contains_custom('foo')
|
|
|
|
// h3 has all four headers
|
|
|
|
assert h3.contains(.accept)
|
|
|
|
assert h3.contains(.expires)
|
|
|
|
assert h3.contains_custom('Server')
|
|
|
|
assert h3.contains_custom('foo')
|
|
|
|
}
|
2021-07-24 11:31:33 +03:00
|
|
|
|
|
|
|
fn parse_headers_test(s string, expected map[string]string) ? {
|
2022-05-13 06:56:21 +03:00
|
|
|
assert parse_headers(s)? == new_custom_header_from_map(expected)?
|
2021-07-24 11:31:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_parse_headers() ? {
|
2021-08-04 12:44:41 +03:00
|
|
|
parse_headers_test('foo: bar', {
|
2021-07-24 11:31:33 +03:00
|
|
|
'foo': 'bar'
|
2022-05-13 06:56:21 +03:00
|
|
|
})?
|
2021-08-04 12:44:41 +03:00
|
|
|
parse_headers_test('foo: \t bar', {
|
2021-07-24 11:31:33 +03:00
|
|
|
'foo': 'bar'
|
2022-05-13 06:56:21 +03:00
|
|
|
})?
|
2021-08-04 12:44:41 +03:00
|
|
|
parse_headers_test('foo: bar\r\n\tbaz', {
|
2021-07-24 11:31:33 +03:00
|
|
|
'foo': 'bar baz'
|
2022-05-13 06:56:21 +03:00
|
|
|
})?
|
2021-08-04 12:44:41 +03:00
|
|
|
parse_headers_test('foo: bar \r\n\tbaz\r\n buzz', {
|
2021-07-24 11:31:33 +03:00
|
|
|
'foo': 'bar baz buzz'
|
2022-05-13 06:56:21 +03:00
|
|
|
})?
|
2021-08-04 12:44:41 +03:00
|
|
|
parse_headers_test('foo: bar\r\nbar:baz', {
|
2021-07-24 11:31:33 +03:00
|
|
|
'foo': 'bar'
|
|
|
|
'bar': 'baz'
|
2022-05-13 06:56:21 +03:00
|
|
|
})?
|
2021-08-04 12:44:41 +03:00
|
|
|
parse_headers_test('foo: bar\r\nbar:baz\r\n', {
|
2021-07-24 11:31:33 +03:00
|
|
|
'foo': 'bar'
|
|
|
|
'bar': 'baz'
|
2022-05-13 06:56:21 +03:00
|
|
|
})?
|
2021-08-04 12:44:41 +03:00
|
|
|
parse_headers_test('foo: bar\r\nbar:baz\r\n\r\n', {
|
2021-07-24 11:31:33 +03:00
|
|
|
'foo': 'bar'
|
|
|
|
'bar': 'baz'
|
2022-05-13 06:56:21 +03:00
|
|
|
})?
|
|
|
|
assert parse_headers('foo: bar\r\nfoo:baz')?.custom_values('foo') == ['bar', 'baz']
|
2021-07-24 11:31:33 +03:00
|
|
|
|
|
|
|
if x := parse_headers(' oops: oh no') {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('should have errored, but got ${x}')
|
2021-07-24 11:31:33 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-23 00:38:09 +03:00
|
|
|
|
|
|
|
fn test_set_cookie() {
|
|
|
|
// multiple Set-Cookie headers should be sent when rendered
|
|
|
|
mut h := new_header()
|
|
|
|
h.add(.set_cookie, 'foo')
|
|
|
|
h.add(.set_cookie, 'bar')
|
|
|
|
assert h.render() == 'Set-Cookie: foo\r\nSet-Cookie: bar\r\n'
|
|
|
|
}
|