2020-08-14 00:51:57 +03:00
|
|
|
import os
|
|
|
|
import time
|
2020-08-14 02:43:57 +03:00
|
|
|
import json
|
2020-08-14 00:51:57 +03:00
|
|
|
import net
|
|
|
|
import net.http
|
2020-11-15 23:54:47 +03:00
|
|
|
import io
|
2020-08-14 00:51:57 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
sport = 12380
|
2022-02-01 19:41:12 +03:00
|
|
|
localserver = 'localhost:$sport'
|
2021-04-05 22:38:55 +03:00
|
|
|
exit_after_time = 12000 // milliseconds
|
2020-08-14 00:51:57 +03:00
|
|
|
vexe = os.getenv('VEXE')
|
2021-01-02 14:21:30 +03:00
|
|
|
vweb_logfile = os.getenv('VWEB_LOGFILE')
|
2020-08-14 00:51:57 +03:00
|
|
|
vroot = os.dir(vexe)
|
|
|
|
serverexe = os.join_path(os.cache_dir(), 'vweb_test_server.exe')
|
2020-11-15 23:54:47 +03:00
|
|
|
tcp_r_timeout = 30 * time.second
|
|
|
|
tcp_w_timeout = 30 * time.second
|
2020-08-14 00:51:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// setup of vweb webserver
|
|
|
|
fn testsuite_begin() {
|
2021-08-28 09:35:39 +03:00
|
|
|
os.chdir(vroot) or {}
|
2021-01-25 14:08:43 +03:00
|
|
|
if os.exists(serverexe) {
|
2021-03-06 22:04:51 +03:00
|
|
|
os.rm(serverexe) or {}
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_a_simple_vweb_app_can_be_compiled() {
|
2022-01-22 22:13:16 +03:00
|
|
|
// did_server_compile := os.system('${os.quoted_path(vexe)} -g -o ${os.quoted_path(serverexe)} vlib/vweb/tests/vweb_test_server.v')
|
2021-01-20 08:04:59 +03:00
|
|
|
// TODO: find out why it does not compile with -usecache and -g
|
2022-01-22 22:13:16 +03:00
|
|
|
did_server_compile := os.system('${os.quoted_path(vexe)} -o ${os.quoted_path(serverexe)} vlib/vweb/tests/vweb_test_server.v')
|
2020-08-14 00:51:57 +03:00
|
|
|
assert did_server_compile == 0
|
2021-01-25 14:08:43 +03:00
|
|
|
assert os.exists(serverexe)
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_a_simple_vweb_app_runs_in_the_background() {
|
2021-01-02 14:21:30 +03:00
|
|
|
mut suffix := ''
|
|
|
|
$if !windows {
|
|
|
|
suffix = ' > /dev/null &'
|
|
|
|
}
|
2021-01-25 14:08:43 +03:00
|
|
|
if vweb_logfile != '' {
|
2022-01-22 22:13:16 +03:00
|
|
|
suffix = ' 2>> ${os.quoted_path(vweb_logfile)} >> ${os.quoted_path(vweb_logfile)} &'
|
2021-01-02 14:21:30 +03:00
|
|
|
}
|
2022-01-22 22:13:16 +03:00
|
|
|
server_exec_cmd := '${os.quoted_path(serverexe)} $sport $exit_after_time $suffix'
|
2020-08-14 00:51:57 +03:00
|
|
|
$if debug_net_socket_client ? {
|
|
|
|
eprintln('running:\n$server_exec_cmd')
|
|
|
|
}
|
2020-11-15 23:54:47 +03:00
|
|
|
$if windows {
|
2022-11-05 10:46:40 +03:00
|
|
|
spawn os.system(server_exec_cmd)
|
2020-11-15 23:54:47 +03:00
|
|
|
} $else {
|
|
|
|
res := os.system(server_exec_cmd)
|
|
|
|
assert res == 0
|
|
|
|
}
|
2021-03-03 10:56:22 +03:00
|
|
|
$if macos {
|
|
|
|
time.sleep(1000 * time.millisecond)
|
|
|
|
} $else {
|
|
|
|
time.sleep(100 * time.millisecond)
|
|
|
|
}
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// web client tests follow
|
|
|
|
fn assert_common_headers(received string) {
|
|
|
|
assert received.starts_with('HTTP/1.1 200 OK\r\n')
|
|
|
|
assert received.contains('Server: VWeb\r\n')
|
|
|
|
assert received.contains('Content-Length:')
|
|
|
|
assert received.contains('Connection: close\r\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_a_simple_tcp_client_can_connect_to_the_vweb_server() {
|
2021-01-05 03:30:27 +03:00
|
|
|
received := simple_tcp_client(path: '/') or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == ''
|
2020-08-14 00:51:57 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
assert_common_headers(received)
|
|
|
|
assert received.contains('Content-Type: text/plain')
|
|
|
|
assert received.contains('Content-Length: 15')
|
|
|
|
assert received.ends_with('Welcome to VWeb')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_a_simple_tcp_client_simple_route() {
|
2020-12-27 12:38:12 +03:00
|
|
|
received := simple_tcp_client(path: '/simple') or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == ''
|
2020-08-14 00:51:57 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
assert_common_headers(received)
|
|
|
|
assert received.contains('Content-Type: text/plain')
|
|
|
|
assert received.contains('Content-Length: 15')
|
|
|
|
assert received.ends_with('A simple result')
|
|
|
|
}
|
|
|
|
|
2021-03-02 22:31:57 +03:00
|
|
|
fn test_a_simple_tcp_client_zero_content_length() {
|
|
|
|
// tests that sending a content-length header of 0 doesn't hang on a read timeout
|
|
|
|
watch := time.new_stopwatch(auto_start: true)
|
|
|
|
simple_tcp_client(path: '/', headers: 'Content-Length: 0\r\n\r\n') or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == ''
|
2021-03-02 22:31:57 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
assert watch.elapsed() < 1 * time.second
|
|
|
|
}
|
|
|
|
|
2020-08-14 00:51:57 +03:00
|
|
|
fn test_a_simple_tcp_client_html_page() {
|
2020-12-27 12:38:12 +03:00
|
|
|
received := simple_tcp_client(path: '/html_page') or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == ''
|
2020-08-14 00:51:57 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
assert_common_headers(received)
|
|
|
|
assert received.contains('Content-Type: text/html')
|
|
|
|
assert received.ends_with('<h1>ok</h1>')
|
|
|
|
}
|
|
|
|
|
|
|
|
// net.http client based tests follow:
|
2022-11-13 11:57:35 +03:00
|
|
|
fn assert_common_http_headers(x http.Response) ! {
|
2021-07-24 20:47:45 +03:00
|
|
|
assert x.status() == .ok
|
2022-11-13 11:57:35 +03:00
|
|
|
assert x.header.get(.server)! == 'VWeb'
|
|
|
|
assert x.header.get(.content_length)!.int() > 0
|
|
|
|
assert x.header.get(.connection)! == 'close'
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_index() {
|
2022-02-01 19:41:12 +03:00
|
|
|
x := http.get('http://$localserver/') or { panic(err) }
|
2022-11-13 11:57:35 +03:00
|
|
|
assert_common_http_headers(x)!
|
|
|
|
assert x.header.get(.content_type)! == 'text/plain'
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == 'Welcome to VWeb'
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_404() {
|
2020-08-14 02:43:57 +03:00
|
|
|
url_404_list := [
|
2022-02-01 19:41:12 +03:00
|
|
|
'http://$localserver/zxcnbnm',
|
|
|
|
'http://$localserver/JHKAJA',
|
|
|
|
'http://$localserver/unknown',
|
2020-11-15 23:54:47 +03:00
|
|
|
]
|
2020-08-14 02:43:57 +03:00
|
|
|
for url in url_404_list {
|
2021-03-01 02:18:14 +03:00
|
|
|
res := http.get(url) or { panic(err) }
|
2021-07-24 20:47:45 +03:00
|
|
|
assert res.status() == .not_found
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_simple() {
|
2022-02-01 19:41:12 +03:00
|
|
|
x := http.get('http://$localserver/simple') or { panic(err) }
|
2022-11-13 11:57:35 +03:00
|
|
|
assert_common_http_headers(x)!
|
|
|
|
assert x.header.get(.content_type)! == 'text/plain'
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == 'A simple result'
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_html_page() {
|
2022-02-01 19:41:12 +03:00
|
|
|
x := http.get('http://$localserver/html_page') or { panic(err) }
|
2022-11-13 11:57:35 +03:00
|
|
|
assert_common_http_headers(x)!
|
|
|
|
assert x.header.get(.content_type)! == 'text/html'
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == '<h1>ok</h1>'
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_settings_page() {
|
2022-02-01 19:41:12 +03:00
|
|
|
x := http.get('http://$localserver/bilbo/settings') or { panic(err) }
|
2022-11-13 11:57:35 +03:00
|
|
|
assert_common_http_headers(x)!
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == 'username: bilbo'
|
2020-08-14 02:43:57 +03:00
|
|
|
//
|
2022-02-01 19:41:12 +03:00
|
|
|
y := http.get('http://$localserver/kent/settings') or { panic(err) }
|
2022-11-13 11:57:35 +03:00
|
|
|
assert_common_http_headers(y)!
|
2022-05-29 20:27:18 +03:00
|
|
|
assert y.body == 'username: kent'
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_user_repo_settings_page() {
|
2022-02-01 19:41:12 +03:00
|
|
|
x := http.get('http://$localserver/bilbo/gostamp/settings') or { panic(err) }
|
2022-11-13 11:57:35 +03:00
|
|
|
assert_common_http_headers(x)!
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == 'username: bilbo | repository: gostamp'
|
2020-08-14 02:43:57 +03:00
|
|
|
//
|
2022-02-01 19:41:12 +03:00
|
|
|
y := http.get('http://$localserver/kent/golang/settings') or { panic(err) }
|
2022-11-13 11:57:35 +03:00
|
|
|
assert_common_http_headers(y)!
|
2022-05-29 20:27:18 +03:00
|
|
|
assert y.body == 'username: kent | repository: golang'
|
2020-08-14 02:43:57 +03:00
|
|
|
//
|
2022-02-01 19:41:12 +03:00
|
|
|
z := http.get('http://$localserver/missing/golang/settings') or { panic(err) }
|
2021-07-24 20:47:45 +03:00
|
|
|
assert z.status() == .not_found
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
name string
|
|
|
|
age int
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_json_post() {
|
2020-08-14 02:43:57 +03:00
|
|
|
ouser := User{
|
|
|
|
name: 'Bilbo'
|
|
|
|
age: 123
|
|
|
|
}
|
|
|
|
json_for_ouser := json.encode(ouser)
|
2022-02-01 19:41:12 +03:00
|
|
|
mut x := http.post_json('http://$localserver/json_echo', json_for_ouser) or { panic(err) }
|
2020-08-14 02:43:57 +03:00
|
|
|
$if debug_net_socket_client ? {
|
2021-01-02 14:21:30 +03:00
|
|
|
eprintln('/json_echo endpoint response: $x')
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
2022-11-13 11:57:35 +03:00
|
|
|
assert x.header.get(.content_type)! == 'application/json'
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == json_for_ouser
|
|
|
|
nuser := json.decode(User, x.body) or { User{} }
|
2020-08-14 02:43:57 +03:00
|
|
|
assert '$ouser' == '$nuser'
|
2021-01-01 23:29:14 +03:00
|
|
|
//
|
2022-02-01 19:41:12 +03:00
|
|
|
x = http.post_json('http://$localserver/json', json_for_ouser) or { panic(err) }
|
2021-01-01 23:29:14 +03:00
|
|
|
$if debug_net_socket_client ? {
|
2021-01-02 14:21:30 +03:00
|
|
|
eprintln('/json endpoint response: $x')
|
2021-01-01 23:29:14 +03:00
|
|
|
}
|
2022-11-13 11:57:35 +03:00
|
|
|
assert x.header.get(.content_type)! == 'application/json'
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == json_for_ouser
|
|
|
|
nuser2 := json.decode(User, x.body) or { User{} }
|
2021-01-01 23:29:14 +03:00
|
|
|
assert '$ouser' == '$nuser2'
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_http_client_multipart_form_data() {
|
2021-04-16 08:46:06 +03:00
|
|
|
boundary := '6844a625b1f0b299'
|
|
|
|
name := 'foo'
|
2021-11-10 19:41:25 +03:00
|
|
|
ct := 'multipart/form-data; boundary=$boundary'
|
2021-04-16 08:46:06 +03:00
|
|
|
contents := 'baz buzz'
|
2021-11-10 19:41:25 +03:00
|
|
|
data := '--$boundary\r
|
|
|
|
Content-Disposition: form-data; name="$name"\r
|
|
|
|
\r
|
|
|
|
$contents\r
|
|
|
|
--$boundary--\r
|
|
|
|
'
|
2021-08-17 18:36:10 +03:00
|
|
|
mut x := http.fetch(
|
2022-02-01 19:41:12 +03:00
|
|
|
url: 'http://$localserver/form_echo'
|
2021-04-16 08:46:06 +03:00
|
|
|
method: .post
|
|
|
|
header: http.new_header(
|
|
|
|
key: .content_type
|
|
|
|
value: ct
|
|
|
|
)
|
|
|
|
data: data
|
2022-11-13 11:57:35 +03:00
|
|
|
)!
|
2021-04-16 08:46:06 +03:00
|
|
|
$if debug_net_socket_client ? {
|
|
|
|
eprintln('/form_echo endpoint response: $x')
|
|
|
|
}
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == contents
|
2021-04-16 08:46:06 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 02:43:57 +03:00
|
|
|
fn test_http_client_shutdown_does_not_work_without_a_cookie() {
|
2022-02-01 19:41:12 +03:00
|
|
|
x := http.get('http://$localserver/shutdown') or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == ''
|
2020-08-14 02:43:57 +03:00
|
|
|
return
|
|
|
|
}
|
2021-07-24 20:47:45 +03:00
|
|
|
assert x.status() == .not_found
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == '404 Not Found'
|
2020-08-14 02:43:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() {
|
|
|
|
// This test is guaranteed to be called last.
|
|
|
|
// It sends a request to the server to shutdown.
|
2021-08-17 18:36:10 +03:00
|
|
|
x := http.fetch(
|
2022-02-01 19:41:12 +03:00
|
|
|
url: 'http://$localserver/shutdown'
|
2021-01-10 19:39:37 +03:00
|
|
|
method: .get
|
2021-08-04 12:44:41 +03:00
|
|
|
cookies: {
|
2020-08-14 02:43:57 +03:00
|
|
|
'skey': 'superman'
|
2021-01-10 19:39:37 +03:00
|
|
|
}
|
|
|
|
) or {
|
2022-02-11 16:52:33 +03:00
|
|
|
assert err.msg() == ''
|
2020-08-14 02:43:57 +03:00
|
|
|
return
|
|
|
|
}
|
2021-07-24 20:47:45 +03:00
|
|
|
assert x.status() == .ok
|
2022-05-29 20:27:18 +03:00
|
|
|
assert x.body == 'good bye'
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// utility code:
|
|
|
|
struct SimpleTcpClientConfig {
|
2021-01-03 23:10:25 +03:00
|
|
|
retries int = 20
|
2020-08-14 00:51:57 +03:00
|
|
|
host string = 'static.dev'
|
|
|
|
path string = '/'
|
|
|
|
agent string = 'v/net.tcp.v'
|
2021-01-21 13:08:38 +03:00
|
|
|
headers string = '\r\n'
|
2020-09-09 15:14:44 +03:00
|
|
|
content string
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 11:57:35 +03:00
|
|
|
fn simple_tcp_client(config SimpleTcpClientConfig) !string {
|
2021-01-20 13:11:01 +03:00
|
|
|
mut client := &net.TcpConn(0)
|
2020-08-25 17:05:40 +03:00
|
|
|
mut tries := 0
|
|
|
|
for tries < config.retries {
|
|
|
|
tries++
|
2022-08-07 10:40:05 +03:00
|
|
|
eprintln('> client retries: $tries')
|
2022-02-01 19:41:12 +03:00
|
|
|
client = net.dial_tcp(localserver) or {
|
2020-08-25 17:05:40 +03:00
|
|
|
if tries > config.retries {
|
2021-02-28 23:20:21 +03:00
|
|
|
return err
|
2020-08-25 17:05:40 +03:00
|
|
|
}
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(100 * time.millisecond)
|
2020-08-25 17:05:40 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
2022-08-07 10:40:05 +03:00
|
|
|
if client == unsafe { nil } {
|
|
|
|
eprintln('coult not create a tcp client connection to $localserver after $config.retries retries')
|
|
|
|
exit(1)
|
|
|
|
}
|
2021-01-25 14:08:43 +03:00
|
|
|
client.set_read_timeout(tcp_r_timeout)
|
|
|
|
client.set_write_timeout(tcp_w_timeout)
|
2020-08-14 00:51:57 +03:00
|
|
|
defer {
|
2021-03-06 22:04:51 +03:00
|
|
|
client.close() or {}
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
|
|
|
message := 'GET $config.path HTTP/1.1
|
|
|
|
Host: $config.host
|
|
|
|
User-Agent: $config.agent
|
|
|
|
Accept: */*
|
|
|
|
$config.headers
|
|
|
|
$config.content'
|
|
|
|
$if debug_net_socket_client ? {
|
|
|
|
eprintln('sending:\n$message')
|
|
|
|
}
|
2022-11-13 11:57:35 +03:00
|
|
|
client.write(message.bytes())!
|
|
|
|
read := io.read_all(reader: client)!
|
2020-08-14 00:51:57 +03:00
|
|
|
$if debug_net_socket_client ? {
|
2020-11-15 23:54:47 +03:00
|
|
|
eprintln('received:\n$read')
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|
2020-11-15 23:54:47 +03:00
|
|
|
return read.bytestr()
|
2020-08-14 00:51:57 +03:00
|
|
|
}
|