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

net.http: Response.text -> Response.body (#14478)

This commit is contained in:
Hunam
2022-05-29 19:27:18 +02:00
committed by GitHub
parent 2c5febe25e
commit 78d1b7f4ef
19 changed files with 65 additions and 74 deletions

View File

@ -120,7 +120,7 @@ fn test_http_client_index() ? {
x := http.get('http://$localserver/') or { panic(err) }
assert_common_http_headers(x)?
assert x.header.get(.content_type)? == 'text/plain'
assert x.text == 'Welcome to VWeb'
assert x.body == 'Welcome to VWeb'
}
fn test_http_client_404() ? {
@ -139,34 +139,34 @@ fn test_http_client_simple() ? {
x := http.get('http://$localserver/simple') or { panic(err) }
assert_common_http_headers(x)?
assert x.header.get(.content_type)? == 'text/plain'
assert x.text == 'A simple result'
assert x.body == 'A simple result'
}
fn test_http_client_html_page() ? {
x := http.get('http://$localserver/html_page') or { panic(err) }
assert_common_http_headers(x)?
assert x.header.get(.content_type)? == 'text/html'
assert x.text == '<h1>ok</h1>'
assert x.body == '<h1>ok</h1>'
}
fn test_http_client_settings_page() ? {
x := http.get('http://$localserver/bilbo/settings') or { panic(err) }
assert_common_http_headers(x)?
assert x.text == 'username: bilbo'
assert x.body == 'username: bilbo'
//
y := http.get('http://$localserver/kent/settings') or { panic(err) }
assert_common_http_headers(y)?
assert y.text == 'username: kent'
assert y.body == 'username: kent'
}
fn test_http_client_user_repo_settings_page() ? {
x := http.get('http://$localserver/bilbo/gostamp/settings') or { panic(err) }
assert_common_http_headers(x)?
assert x.text == 'username: bilbo | repository: gostamp'
assert x.body == 'username: bilbo | repository: gostamp'
//
y := http.get('http://$localserver/kent/golang/settings') or { panic(err) }
assert_common_http_headers(y)?
assert y.text == 'username: kent | repository: golang'
assert y.body == 'username: kent | repository: golang'
//
z := http.get('http://$localserver/missing/golang/settings') or { panic(err) }
assert z.status() == .not_found
@ -188,8 +188,8 @@ fn test_http_client_json_post() ? {
eprintln('/json_echo endpoint response: $x')
}
assert x.header.get(.content_type)? == 'application/json'
assert x.text == json_for_ouser
nuser := json.decode(User, x.text) or { User{} }
assert x.body == json_for_ouser
nuser := json.decode(User, x.body) or { User{} }
assert '$ouser' == '$nuser'
//
x = http.post_json('http://$localserver/json', json_for_ouser) or { panic(err) }
@ -197,8 +197,8 @@ fn test_http_client_json_post() ? {
eprintln('/json endpoint response: $x')
}
assert x.header.get(.content_type)? == 'application/json'
assert x.text == json_for_ouser
nuser2 := json.decode(User, x.text) or { User{} }
assert x.body == json_for_ouser
nuser2 := json.decode(User, x.body) or { User{} }
assert '$ouser' == '$nuser2'
}
@ -225,7 +225,7 @@ $contents\r
$if debug_net_socket_client ? {
eprintln('/form_echo endpoint response: $x')
}
assert x.text == contents
assert x.body == contents
}
fn test_http_client_shutdown_does_not_work_without_a_cookie() {
@ -234,7 +234,7 @@ fn test_http_client_shutdown_does_not_work_without_a_cookie() {
return
}
assert x.status() == .not_found
assert x.text == '404 Not Found'
assert x.body == '404 Not Found'
}
fn testsuite_end() {
@ -251,7 +251,7 @@ fn testsuite_end() {
return
}
assert x.status() == .ok
assert x.text == 'good bye'
assert x.body == 'good bye'
}
// utility code:

View File

@ -27,12 +27,12 @@ pub const (
http_302 = http.new_response(
status: .found
text: '302 Found'
body: '302 Found'
header: headers_close
)
http_400 = http.new_response(
status: .bad_request
text: '400 Bad Request'
body: '400 Bad Request'
header: http.new_header(
key: .content_type
value: 'text/plain'
@ -40,7 +40,7 @@ pub const (
)
http_404 = http.new_response(
status: .not_found
text: '404 Not Found'
body: '404 Not Found'
header: http.new_header(
key: .content_type
value: 'text/plain'
@ -48,7 +48,7 @@ pub const (
)
http_500 = http.new_response(
status: .internal_server_error
text: '500 Internal Server Error'
body: '500 Internal Server Error'
header: http.new_header(
key: .content_type
value: 'text/plain'
@ -217,7 +217,7 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, res string) bo
mut resp := http.Response{
header: header.join(vweb.headers_close)
text: res
body: res
}
resp.set_version(.v1_1)
resp.set_status(http.status_from_int(ctx.status.int()))

View File

@ -25,18 +25,6 @@ fn test_a_vweb_application_compiles() {
vweb.run(&App{}, 18081)
}
/*
/TODO
pub fn (mut app App) init_server_old() {
app.db = sqlite.connect('blog.db') or { panic(err) }
app.db.create_table('article', [
'id integer primary key',
"title text default ''",
"text text default ''",
])
}
*/
pub fn (mut app App) before_request() {
app.user_id = app.get_cookie('id') or { '0' }
}