2021-08-20 01:13:36 +03:00
|
|
|
module http
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_response_bytestr() {
|
2021-08-20 01:13:36 +03:00
|
|
|
{
|
|
|
|
resp := new_response(
|
|
|
|
status: .ok
|
2022-05-29 20:27:18 +03:00
|
|
|
text: 'Foo' // TODO: replace with `body` once deprecaped
|
2021-08-20 01:13:36 +03:00
|
|
|
)
|
|
|
|
assert resp.bytestr() == 'HTTP/1.1 200 OK\r\n' + 'Content-Length: 3\r\n' + '\r\n' + 'Foo'
|
|
|
|
}
|
|
|
|
{
|
|
|
|
resp := new_response(
|
|
|
|
status: .found
|
2022-05-29 20:27:18 +03:00
|
|
|
body: 'Foo'
|
2021-08-20 01:13:36 +03:00
|
|
|
header: new_header(key: .location, value: '/')
|
|
|
|
)
|
|
|
|
lines := resp.bytestr().split_into_lines()
|
|
|
|
assert lines[0] == 'HTTP/1.1 302 Found'
|
|
|
|
// header order is not guaranteed
|
2022-05-13 06:56:21 +03:00
|
|
|
check_headers(['Location: /', 'Content-Length: 3'], lines[1..3])?
|
2021-08-20 01:13:36 +03:00
|
|
|
assert lines[3] == ''
|
|
|
|
assert lines[4] == 'Foo'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check_headers is a helper function for asserting all expected headers
|
|
|
|
// are found because rendered header order is not guaranteed. The check
|
|
|
|
// is O(n^2) which is fine for small lists.
|
|
|
|
fn check_headers(expected []string, found []string) ? {
|
|
|
|
assert expected.len == found.len
|
|
|
|
for header in expected {
|
|
|
|
if !found.contains(header) {
|
|
|
|
return error('expected header "$header" not in $found')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|