mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
net.http: remove ending \r from response.status_msg, add more tests
This commit is contained in:
parent
aae63f5eb5
commit
cb336c7dc7
@ -36,7 +36,7 @@ pub fn (resp Response) bytestr() string {
|
||||
|
||||
// Parse a raw HTTP response into a Response object
|
||||
pub fn parse_response(resp string) !Response {
|
||||
version, status_code, status_msg := parse_status_line(resp.all_before('\n'))!
|
||||
version, status_code, status_msg := parse_status_line(resp.all_before('\r\n'))!
|
||||
// Build resp header map and separate the body
|
||||
start_idx, end_idx := find_headers_range(resp)!
|
||||
header := parse_headers(resp.substr(start_idx, end_idx))!
|
||||
|
@ -1,26 +1,25 @@
|
||||
module http
|
||||
|
||||
fn test_response_bytestr() {
|
||||
{
|
||||
resp := new_response(
|
||||
status: .ok
|
||||
text: 'Foo' // TODO: replace with `body` once deprecaped
|
||||
)
|
||||
assert resp.bytestr() == 'HTTP/1.1 200 OK\r\n' + 'Content-Length: 3\r\n' + '\r\n' + 'Foo'
|
||||
}
|
||||
{
|
||||
resp := new_response(
|
||||
status: .found
|
||||
body: 'Foo'
|
||||
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
|
||||
check_headers(['Location: /', 'Content-Length: 3'], lines[1..3])?
|
||||
assert lines[3] == ''
|
||||
assert lines[4] == 'Foo'
|
||||
}
|
||||
fn test_response_bytestr_1() {
|
||||
resp := new_response(
|
||||
status: .ok
|
||||
text: 'Foo' // TODO: replace with `body` once deprecaped
|
||||
)
|
||||
assert resp.bytestr() == 'HTTP/1.1 200 OK\r\n' + 'Content-Length: 3\r\n' + '\r\n' + 'Foo'
|
||||
}
|
||||
|
||||
fn test_response_bytestr_2() {
|
||||
resp := new_response(
|
||||
status: .found
|
||||
body: 'Foo'
|
||||
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
|
||||
check_headers(['Location: /', 'Content-Length: 3'], lines[1..3])!
|
||||
assert lines[3] == ''
|
||||
assert lines[4] == 'Foo'
|
||||
}
|
||||
|
||||
// check_headers is a helper function for asserting all expected headers
|
||||
@ -34,3 +33,15 @@ fn check_headers(expected []string, found []string) ! {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_parse_response() {
|
||||
content := 'HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nFoo'
|
||||
x := parse_response(content)!
|
||||
assert x.http_version == '1.1'
|
||||
assert x.status_code == 200
|
||||
assert x.status_msg == 'OK'
|
||||
assert x.header.contains(.content_length)
|
||||
assert x.header.get(.content_length)! == '3'
|
||||
assert x.body == 'Foo'
|
||||
assert x.text == 'Foo'
|
||||
}
|
||||
|
@ -74,10 +74,12 @@ fn test_server_custom_handler() {
|
||||
x := http.fetch(url: 'http://localhost:${cport}/endpoint?abc=xyz', data: 'my data')!
|
||||
assert x.body == 'my data, /endpoint?abc=xyz'
|
||||
assert x.status_code == 200
|
||||
assert x.status_msg == 'OK'
|
||||
assert x.http_version == '1.1'
|
||||
y := http.fetch(url: 'http://localhost:${cport}/another/endpoint', data: 'abcde')!
|
||||
assert y.body == 'abcde, /another/endpoint'
|
||||
assert y.status_code == 200
|
||||
assert x.status_msg == 'OK'
|
||||
assert y.status() == .ok
|
||||
assert y.http_version == '1.1'
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user