2020-08-13 23:51:57 +02:00
|
|
|
import os
|
|
|
|
import time
|
2020-08-14 01:43:57 +02:00
|
|
|
import json
|
2020-08-13 23:51:57 +02:00
|
|
|
import net
|
|
|
|
import net.http
|
2020-11-15 21:54:47 +01:00
|
|
|
import io
|
2020-08-13 23:51:57 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
sport = 12380
|
2021-01-02 12:21:30 +01:00
|
|
|
exit_after_time = 7000 // milliseconds
|
2020-08-13 23:51:57 +02:00
|
|
|
vexe = os.getenv('VEXE')
|
2021-01-02 12:21:30 +01:00
|
|
|
vweb_logfile = os.getenv('VWEB_LOGFILE')
|
2020-08-13 23:51:57 +02:00
|
|
|
vroot = os.dir(vexe)
|
|
|
|
serverexe = os.join_path(os.cache_dir(), 'vweb_test_server.exe')
|
2020-11-15 21:54:47 +01:00
|
|
|
tcp_r_timeout = 30 * time.second
|
|
|
|
tcp_w_timeout = 30 * time.second
|
2020-08-13 23:51:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// setup of vweb webserver
|
|
|
|
fn testsuite_begin() {
|
2021-01-25 12:08:43 +01:00
|
|
|
os.chdir(vroot)
|
|
|
|
if os.exists(serverexe) {
|
2021-01-26 15:43:10 +01:00
|
|
|
os.rm(serverexe) or { }
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_a_simple_vweb_app_can_be_compiled() {
|
2021-01-20 06:04:59 +01:00
|
|
|
// did_server_compile := os.system('$vexe -g -o $serverexe vlib/vweb/tests/vweb_test_server.v')
|
|
|
|
// TODO: find out why it does not compile with -usecache and -g
|
2021-01-25 12:08:43 +01:00
|
|
|
did_server_compile := os.system('$vexe -o $serverexe vlib/vweb/tests/vweb_test_server.v')
|
2020-08-13 23:51:57 +02:00
|
|
|
assert did_server_compile == 0
|
2021-01-25 12:08:43 +01:00
|
|
|
assert os.exists(serverexe)
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_a_simple_vweb_app_runs_in_the_background() {
|
2021-01-02 12:21:30 +01:00
|
|
|
mut suffix := ''
|
|
|
|
$if !windows {
|
|
|
|
suffix = ' > /dev/null &'
|
|
|
|
}
|
2021-01-25 12:08:43 +01:00
|
|
|
if vweb_logfile != '' {
|
|
|
|
suffix = ' 2>> $vweb_logfile >> $vweb_logfile &'
|
2021-01-02 12:21:30 +01:00
|
|
|
}
|
2021-01-25 12:08:43 +01:00
|
|
|
server_exec_cmd := '$serverexe $sport $exit_after_time $suffix'
|
2020-08-13 23:51:57 +02:00
|
|
|
$if debug_net_socket_client ? {
|
|
|
|
eprintln('running:\n$server_exec_cmd')
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
$if windows {
|
|
|
|
go os.system(server_exec_cmd)
|
|
|
|
} $else {
|
|
|
|
res := os.system(server_exec_cmd)
|
|
|
|
assert res == 0
|
|
|
|
}
|
2020-08-13 23:51:57 +02:00
|
|
|
time.sleep_ms(100)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 01:30:27 +01:00
|
|
|
received := simple_tcp_client(path: '/') or {
|
2020-08-13 23:51:57 +02:00
|
|
|
assert err == ''
|
|
|
|
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 10:38:12 +01:00
|
|
|
received := simple_tcp_client(path: '/simple') or {
|
2020-08-13 23:51:57 +02:00
|
|
|
assert err == ''
|
|
|
|
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')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_a_simple_tcp_client_html_page() {
|
2020-12-27 10:38:12 +01:00
|
|
|
received := simple_tcp_client(path: '/html_page') or {
|
2020-08-13 23:51:57 +02:00
|
|
|
assert err == ''
|
|
|
|
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:
|
|
|
|
fn assert_common_http_headers(x http.Response) {
|
|
|
|
assert x.status_code == 200
|
|
|
|
assert x.headers['Server'] == 'VWeb'
|
|
|
|
assert x.headers['Content-Length'].int() > 0
|
|
|
|
assert x.headers['Connection'] == 'close'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_client_index() {
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.get('http://127.0.0.1:$sport/') or { panic(err) }
|
2020-08-13 23:51:57 +02:00
|
|
|
assert_common_http_headers(x)
|
|
|
|
assert x.headers['Content-Type'] == 'text/plain'
|
|
|
|
assert x.text == 'Welcome to VWeb'
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:04:21 +01:00
|
|
|
fn test_http_client_chunk_transfer() {
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.get('http://127.0.0.1:$sport/chunk') or { panic(err) }
|
2021-01-18 13:04:21 +01:00
|
|
|
assert_common_http_headers(x)
|
|
|
|
assert x.headers['Transfer-Encoding'] == 'chunked'
|
|
|
|
assert x.text == 'Lorem ipsum dolor sit amet, consetetur sadipscing'
|
|
|
|
}
|
|
|
|
|
2020-08-14 01:43:57 +02:00
|
|
|
fn test_http_client_404() {
|
|
|
|
url_404_list := [
|
2021-01-25 12:08:43 +01:00
|
|
|
'http://127.0.0.1:$sport/zxcnbnm',
|
|
|
|
'http://127.0.0.1:$sport/JHKAJA',
|
|
|
|
'http://127.0.0.1:$sport/unknown',
|
2020-11-15 21:54:47 +01:00
|
|
|
]
|
2020-08-14 01:43:57 +02:00
|
|
|
for url in url_404_list {
|
2020-12-27 10:38:12 +01:00
|
|
|
res := http.get(url) or { panic(err) }
|
2020-08-14 01:43:57 +02:00
|
|
|
assert res.status_code == 404
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 23:51:57 +02:00
|
|
|
fn test_http_client_simple() {
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.get('http://127.0.0.1:$sport/simple') or { panic(err) }
|
2020-08-13 23:51:57 +02:00
|
|
|
assert_common_http_headers(x)
|
|
|
|
assert x.headers['Content-Type'] == 'text/plain'
|
|
|
|
assert x.text == 'A simple result'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_client_html_page() {
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.get('http://127.0.0.1:$sport/html_page') or { panic(err) }
|
2020-08-13 23:51:57 +02:00
|
|
|
assert_common_http_headers(x)
|
|
|
|
assert x.headers['Content-Type'] == 'text/html'
|
|
|
|
assert x.text == '<h1>ok</h1>'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_client_settings_page() {
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.get('http://127.0.0.1:$sport/bilbo/settings') or { panic(err) }
|
2020-08-13 23:51:57 +02:00
|
|
|
assert_common_http_headers(x)
|
|
|
|
assert x.text == 'username: bilbo'
|
2020-08-14 01:43:57 +02:00
|
|
|
//
|
2021-01-25 12:08:43 +01:00
|
|
|
y := http.get('http://127.0.0.1:$sport/kent/settings') or { panic(err) }
|
2020-08-13 23:51:57 +02:00
|
|
|
assert_common_http_headers(y)
|
|
|
|
assert y.text == 'username: kent'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_client_user_repo_settings_page() {
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.get('http://127.0.0.1:$sport/bilbo/gostamp/settings') or { panic(err) }
|
2020-08-13 23:51:57 +02:00
|
|
|
assert_common_http_headers(x)
|
|
|
|
assert x.text == 'username: bilbo | repository: gostamp'
|
2020-08-14 01:43:57 +02:00
|
|
|
//
|
2021-01-25 12:08:43 +01:00
|
|
|
y := http.get('http://127.0.0.1:$sport/kent/golang/settings') or { panic(err) }
|
2020-08-13 23:51:57 +02:00
|
|
|
assert_common_http_headers(y)
|
|
|
|
assert y.text == 'username: kent | repository: golang'
|
2020-08-14 01:43:57 +02:00
|
|
|
//
|
2021-01-25 12:08:43 +01:00
|
|
|
z := http.get('http://127.0.0.1:$sport/missing/golang/settings') or { panic(err) }
|
2020-08-14 01:43:57 +02:00
|
|
|
assert z.status_code == 404
|
|
|
|
}
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
name string
|
|
|
|
age int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_client_json_post() {
|
|
|
|
ouser := User{
|
|
|
|
name: 'Bilbo'
|
|
|
|
age: 123
|
|
|
|
}
|
|
|
|
json_for_ouser := json.encode(ouser)
|
2021-01-25 12:08:43 +01:00
|
|
|
mut x := http.post_json('http://127.0.0.1:$sport/json_echo', json_for_ouser) or { panic(err) }
|
2020-08-14 01:43:57 +02:00
|
|
|
$if debug_net_socket_client ? {
|
2021-01-02 12:21:30 +01:00
|
|
|
eprintln('/json_echo endpoint response: $x')
|
2020-08-14 01:43:57 +02:00
|
|
|
}
|
|
|
|
assert x.headers['Content-Type'] == 'application/json'
|
|
|
|
assert x.text == json_for_ouser
|
2020-12-27 10:38:12 +01:00
|
|
|
nuser := json.decode(User, x.text) or { User{} }
|
2020-08-14 01:43:57 +02:00
|
|
|
assert '$ouser' == '$nuser'
|
2021-01-01 21:29:14 +01:00
|
|
|
//
|
2021-01-25 12:08:43 +01:00
|
|
|
x = http.post_json('http://127.0.0.1:$sport/json', json_for_ouser) or { panic(err) }
|
2021-01-01 21:29:14 +01:00
|
|
|
$if debug_net_socket_client ? {
|
2021-01-02 12:21:30 +01:00
|
|
|
eprintln('/json endpoint response: $x')
|
2021-01-01 21:29:14 +01:00
|
|
|
}
|
|
|
|
assert x.headers['Content-Type'] == 'application/json'
|
|
|
|
assert x.text == json_for_ouser
|
|
|
|
nuser2 := json.decode(User, x.text) or { User{} }
|
|
|
|
assert '$ouser' == '$nuser2'
|
2020-08-14 01:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_http_client_shutdown_does_not_work_without_a_cookie() {
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.get('http://127.0.0.1:$sport/shutdown') or {
|
2020-08-14 01:43:57 +02:00
|
|
|
assert err == ''
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert x.status_code == 404
|
|
|
|
assert x.text == '404 Not Found'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn testsuite_end() {
|
|
|
|
// This test is guaranteed to be called last.
|
|
|
|
// It sends a request to the server to shutdown.
|
2021-01-25 12:08:43 +01:00
|
|
|
x := http.fetch('http://127.0.0.1:$sport/shutdown',
|
2021-01-10 17:39:37 +01:00
|
|
|
method: .get
|
|
|
|
cookies: {
|
2020-08-14 01:43:57 +02:00
|
|
|
'skey': 'superman'
|
2021-01-10 17:39:37 +01:00
|
|
|
}
|
|
|
|
) or {
|
2020-08-14 01:43:57 +02:00
|
|
|
assert err == ''
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert x.status_code == 200
|
|
|
|
assert x.text == 'good bye'
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// utility code:
|
|
|
|
struct SimpleTcpClientConfig {
|
2021-01-03 21:10:25 +01:00
|
|
|
retries int = 20
|
2020-08-13 23:51:57 +02:00
|
|
|
host string = 'static.dev'
|
|
|
|
path string = '/'
|
|
|
|
agent string = 'v/net.tcp.v'
|
2021-01-21 11:08:38 +01:00
|
|
|
headers string = '\r\n'
|
2020-09-09 14:14:44 +02:00
|
|
|
content string
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn simple_tcp_client(config SimpleTcpClientConfig) ?string {
|
2021-01-20 11:11:01 +01:00
|
|
|
mut client := &net.TcpConn(0)
|
2020-08-25 16:05:40 +02:00
|
|
|
mut tries := 0
|
|
|
|
for tries < config.retries {
|
|
|
|
tries++
|
2021-01-25 12:08:43 +01:00
|
|
|
client = net.dial_tcp('127.0.0.1:$sport') or {
|
2020-08-25 16:05:40 +02:00
|
|
|
if tries > config.retries {
|
|
|
|
return error(err)
|
|
|
|
}
|
2020-08-25 16:52:44 +02:00
|
|
|
time.sleep_ms(100)
|
2020-08-25 16:05:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
2021-01-25 12:08:43 +01:00
|
|
|
client.set_read_timeout(tcp_r_timeout)
|
|
|
|
client.set_write_timeout(tcp_w_timeout)
|
2020-08-13 23:51:57 +02:00
|
|
|
defer {
|
2021-01-26 15:43:10 +01:00
|
|
|
client.close() or { }
|
2020-08-13 23:51:57 +02: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')
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
client.write(message.bytes()) ?
|
2020-12-16 18:22:26 +01:00
|
|
|
read := io.read_all(reader: client) ?
|
2020-08-13 23:51:57 +02:00
|
|
|
$if debug_net_socket_client ? {
|
2020-11-15 21:54:47 +01:00
|
|
|
eprintln('received:\n$read')
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
return read.bytestr()
|
2020-08-13 23:51:57 +02:00
|
|
|
}
|