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

net.http: refactor the Response struct (#10922)

This commit is contained in:
Miccah
2021-07-24 12:47:45 -05:00
committed by GitHub
parent 3979e5c5ff
commit a0e27d3fd9
10 changed files with 242 additions and 160 deletions

View File

@@ -109,7 +109,7 @@ fn test_a_simple_tcp_client_html_page() {
// net.http client based tests follow:
fn assert_common_http_headers(x http.Response) ? {
assert x.status_code == 200
assert x.status() == .ok
assert x.header.get(.server) ? == 'VWeb'
assert x.header.get(.content_length) ?.int() > 0
assert x.header.get(.connection) ? == 'close'
@@ -130,7 +130,7 @@ fn test_http_client_404() ? {
]
for url in url_404_list {
res := http.get(url) or { panic(err) }
assert res.status_code == 404
assert res.status() == .not_found
}
}
@@ -168,7 +168,7 @@ fn test_http_client_user_repo_settings_page() ? {
assert y.text == 'username: kent | repository: golang'
//
z := http.get('http://127.0.0.1:$sport/missing/golang/settings') or { panic(err) }
assert z.status_code == 404
assert z.status() == .not_found
}
struct User {
@@ -231,7 +231,7 @@ fn test_http_client_shutdown_does_not_work_without_a_cookie() {
assert err.msg == ''
return
}
assert x.status_code == 404
assert x.status() == .not_found
assert x.text == '404 Not Found'
}
@@ -247,7 +247,7 @@ fn testsuite_end() {
assert err.msg == ''
return
}
assert x.status_code == 200
assert x.status() == .ok
assert x.text == 'good bye'
}

View File

@@ -17,34 +17,31 @@ pub const (
http.CommonHeader.connection.str(): 'close'
}) or { panic('should never fail') }
http_400 = http.Response{
version: .v1_1
status_code: 400
http_400 = http.new_response(
status: .bad_request
text: '400 Bad Request'
header: http.new_header_from_map(map{
http.CommonHeader.content_type: 'text/plain'
http.CommonHeader.content_length: '15'
}).join(headers_close)
}
http_404 = http.Response{
version: .v1_1
status_code: 404
header: http.new_header(
key: .content_type
value: 'text/plain'
).join(headers_close)
)
http_404 = http.new_response(
status: .not_found
text: '404 Not Found'
header: http.new_header_from_map(map{
http.CommonHeader.content_type: 'text/plain'
http.CommonHeader.content_length: '13'
}).join(headers_close)
}
http_500 = http.Response{
version: .v1_1
status_code: 500
header: http.new_header(
key: .content_type
value: 'text/plain'
).join(headers_close)
)
http_500 = http.new_response(
status: .internal_server_error
text: '500 Internal Server Error'
header: http.new_header_from_map(map{
http.CommonHeader.content_type: 'text/plain'
http.CommonHeader.content_length: '25'
}).join(headers_close)
}
mime_types = map{
header: http.new_header(
key: .content_type
value: 'text/plain'
).join(headers_close)
)
mime_types = map{
'.css': 'text/css; charset=utf-8'
'.gif': 'image/gif'
'.htm': 'text/html; charset=utf-8'
@@ -134,12 +131,12 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo
http.CommonHeader.content_length: res.len.str()
}).join(ctx.header)
resp := http.Response{
version: .v1_1
status_code: ctx.status.int() // TODO: change / remove ctx.status
mut resp := http.Response{
header: header.join(vweb.headers_close)
text: res
}
resp.set_version(.v1_1)
resp.set_status(http.status_from_int(ctx.status.int()))
send_string(mut ctx.conn, resp.bytestr()) or { return false }
return true
}