mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vweb: add controllers (#17840)
This commit is contained in:
41
vlib/vweb/tests/controller_duplicate_server.v
Normal file
41
vlib/vweb/tests/controller_duplicate_server.v
Normal file
@@ -0,0 +1,41 @@
|
||||
module main
|
||||
|
||||
import vweb
|
||||
import time
|
||||
import os
|
||||
|
||||
struct App {
|
||||
vweb.Context
|
||||
vweb.Controller
|
||||
}
|
||||
|
||||
struct Admin {
|
||||
vweb.Context
|
||||
}
|
||||
|
||||
['/admin/duplicate']
|
||||
fn (mut app App) duplicate() vweb.Result {
|
||||
return app.text('duplicate')
|
||||
}
|
||||
|
||||
fn exit_after_timeout(timeout_in_ms int) {
|
||||
time.sleep(timeout_in_ms * time.millisecond)
|
||||
println('>> webserver: pid: ${os.getpid()}, exiting ...')
|
||||
exit(0)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if os.args.len != 3 {
|
||||
panic('Usage: `controller_test_server.exe PORT TIMEOUT_IN_MILLISECONDS`')
|
||||
}
|
||||
http_port := os.args[1].int()
|
||||
assert http_port > 0
|
||||
timeout := os.args[2].int()
|
||||
mut app_dup := &App{
|
||||
controllers: [
|
||||
vweb.controller('/admin', &Admin{}),
|
||||
]
|
||||
}
|
||||
|
||||
vweb.run_at(app_dup, host: 'localhost', port: http_port, family: .ip) or { panic(err) }
|
||||
}
|
||||
133
vlib/vweb/tests/controller_test.v
Normal file
133
vlib/vweb/tests/controller_test.v
Normal file
@@ -0,0 +1,133 @@
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import net
|
||||
import net.http
|
||||
import io
|
||||
|
||||
const (
|
||||
sport = 12382
|
||||
sport2 = 12383
|
||||
localserver = '127.0.0.1:${sport}'
|
||||
exit_after_time = 12000 // milliseconds
|
||||
vexe = os.getenv('VEXE')
|
||||
vweb_logfile = os.getenv('VWEB_LOGFILE')
|
||||
vroot = os.dir(vexe)
|
||||
serverexe = os.join_path(os.cache_dir(), 'controller_test_server.exe')
|
||||
tcp_r_timeout = 30 * time.second
|
||||
tcp_w_timeout = 30 * time.second
|
||||
)
|
||||
|
||||
// setup of vweb webserver
|
||||
fn testsuite_begin() {
|
||||
os.chdir(vroot) or {}
|
||||
if os.exists(serverexe) {
|
||||
os.rm(serverexe) or {}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_middleware_vweb_app_can_be_compiled() {
|
||||
// did_server_compile := os.system('${os.quoted_path(vexe)} -g -o ${os.quoted_path(serverexe)} vlib/vweb/tests/controller_test_server.vv')
|
||||
// TODO: find out why it does not compile with -usecache and -g
|
||||
did_server_compile := os.system('${os.quoted_path(vexe)} -o ${os.quoted_path(serverexe)} vlib/vweb/tests/controller_test_server.v')
|
||||
assert did_server_compile == 0
|
||||
assert os.exists(serverexe)
|
||||
}
|
||||
|
||||
fn test_middleware_vweb_app_runs_in_the_background() {
|
||||
mut suffix := ''
|
||||
$if !windows {
|
||||
suffix = ' > /dev/null &'
|
||||
}
|
||||
if vweb_logfile != '' {
|
||||
suffix = ' 2>> ${os.quoted_path(vweb_logfile)} >> ${os.quoted_path(vweb_logfile)} &'
|
||||
}
|
||||
server_exec_cmd := '${os.quoted_path(serverexe)} ${sport} ${exit_after_time} ${suffix}'
|
||||
$if debug_net_socket_client ? {
|
||||
eprintln('running:\n${server_exec_cmd}')
|
||||
}
|
||||
$if windows {
|
||||
spawn os.system(server_exec_cmd)
|
||||
} $else {
|
||||
res := os.system(server_exec_cmd)
|
||||
assert res == 0
|
||||
}
|
||||
$if macos {
|
||||
time.sleep(1000 * time.millisecond)
|
||||
} $else {
|
||||
time.sleep(100 * time.millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// test functions:
|
||||
|
||||
fn test_app_home() {
|
||||
x := http.get('http://${localserver}/') or { panic(err) }
|
||||
assert x.body == 'App'
|
||||
}
|
||||
|
||||
fn test_app_path() {
|
||||
x := http.get('http://${localserver}/path') or { panic(err) }
|
||||
assert x.body == 'App path'
|
||||
}
|
||||
|
||||
fn test_admin_home() {
|
||||
x := http.get('http://${localserver}/admin/') or { panic(err) }
|
||||
assert x.body == 'Admin'
|
||||
}
|
||||
|
||||
fn test_admin_path() {
|
||||
x := http.get('http://${localserver}/admin/path') or { panic(err) }
|
||||
assert x.body == 'Admin path'
|
||||
}
|
||||
|
||||
fn test_other_home() {
|
||||
x := http.get('http://${localserver}/other/') or { panic(err) }
|
||||
assert x.body == 'Other'
|
||||
}
|
||||
|
||||
fn test_other_path() {
|
||||
x := http.get('http://${localserver}/other/path') or { panic(err) }
|
||||
assert x.body == 'Other path'
|
||||
}
|
||||
|
||||
fn test_shutdown() {
|
||||
// This test is guaranteed to be called last.
|
||||
// It sends a request to the server to shutdown.
|
||||
x := http.fetch(
|
||||
url: 'http://${localserver}/shutdown'
|
||||
method: .get
|
||||
cookies: {
|
||||
'skey': 'superman'
|
||||
}
|
||||
) or {
|
||||
assert err.msg() == ''
|
||||
return
|
||||
}
|
||||
assert x.status() == .ok
|
||||
assert x.body == 'good bye'
|
||||
}
|
||||
|
||||
fn test_duplicate_route() {
|
||||
did_server_compile := os.system('${os.quoted_path(vexe)} -o ${os.quoted_path(serverexe)} vlib/vweb/tests/controller_duplicate_server.v')
|
||||
assert did_server_compile == 0
|
||||
assert os.exists(serverexe)
|
||||
|
||||
mut suffix := ''
|
||||
|
||||
if vweb_logfile != '' {
|
||||
suffix = ' 2>> ${os.quoted_path(vweb_logfile)} >> ${os.quoted_path(vweb_logfile)} &'
|
||||
}
|
||||
server_exec_cmd := '${os.quoted_path(serverexe)} ${sport2} ${exit_after_time} ${suffix}'
|
||||
$if debug_net_socket_client ? {
|
||||
eprintln('running:\n${server_exec_cmd}')
|
||||
}
|
||||
$if windows {
|
||||
task := spawn os.execute(server_exec_cmd)
|
||||
res := task.wait()
|
||||
assert res.output.contains('V panic: method "duplicate" with route "/admin/duplicate" should be handled by the Controller of "/admin"')
|
||||
} $else {
|
||||
res := os.execute(server_exec_cmd)
|
||||
assert res.output.contains('V panic: method "duplicate" with route "/admin/duplicate" should be handled by the Controller of "/admin"')
|
||||
}
|
||||
}
|
||||
93
vlib/vweb/tests/controller_test_server.v
Normal file
93
vlib/vweb/tests/controller_test_server.v
Normal file
@@ -0,0 +1,93 @@
|
||||
module main
|
||||
|
||||
import vweb
|
||||
import time
|
||||
import os
|
||||
|
||||
struct App {
|
||||
vweb.Context
|
||||
vweb.Controller
|
||||
timeout int
|
||||
}
|
||||
|
||||
struct Admin {
|
||||
vweb.Context
|
||||
}
|
||||
|
||||
struct Other {
|
||||
vweb.Context
|
||||
}
|
||||
|
||||
fn exit_after_timeout(timeout_in_ms int) {
|
||||
time.sleep(timeout_in_ms * time.millisecond)
|
||||
println('>> webserver: pid: ${os.getpid()}, exiting ...')
|
||||
exit(0)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if os.args.len != 3 {
|
||||
panic('Usage: `controller_test_server.exe PORT TIMEOUT_IN_MILLISECONDS`')
|
||||
}
|
||||
http_port := os.args[1].int()
|
||||
assert http_port > 0
|
||||
timeout := os.args[2].int()
|
||||
spawn exit_after_timeout(timeout)
|
||||
|
||||
mut app := &App{
|
||||
timeout: timeout
|
||||
controllers: [
|
||||
vweb.controller('/admin', &Admin{}),
|
||||
vweb.controller('/other', &Other{}),
|
||||
]
|
||||
}
|
||||
|
||||
eprintln('>> webserver: pid: ${os.getpid()}, started on http://localhost:${http_port}/ , with maximum runtime of ${app.timeout} milliseconds.')
|
||||
vweb.run_at(app, host: 'localhost', port: http_port, family: .ip)!
|
||||
}
|
||||
|
||||
['/']
|
||||
fn (mut app App) home() vweb.Result {
|
||||
return app.text('App')
|
||||
}
|
||||
|
||||
['/path']
|
||||
fn (mut app App) app_path() vweb.Result {
|
||||
return app.text('App path')
|
||||
}
|
||||
|
||||
['/']
|
||||
fn (mut app Admin) admin_home() vweb.Result {
|
||||
return app.text('Admin')
|
||||
}
|
||||
|
||||
['/path']
|
||||
fn (mut app Admin) admin_path() vweb.Result {
|
||||
return app.text('Admin path')
|
||||
}
|
||||
|
||||
['/']
|
||||
fn (mut app Other) other_home() vweb.Result {
|
||||
return app.text('Other')
|
||||
}
|
||||
|
||||
['/path']
|
||||
fn (mut app Other) other_path() vweb.Result {
|
||||
return app.text('Other path')
|
||||
}
|
||||
|
||||
// utility functions:
|
||||
|
||||
pub fn (mut app App) shutdown() vweb.Result {
|
||||
session_key := app.get_cookie('skey') or { return app.not_found() }
|
||||
if session_key != 'superman' {
|
||||
return app.not_found()
|
||||
}
|
||||
spawn app.gracefull_exit()
|
||||
return app.ok('good bye')
|
||||
}
|
||||
|
||||
fn (mut app App) gracefull_exit() {
|
||||
eprintln('>> webserver: gracefull_exit')
|
||||
time.sleep(100 * time.millisecond)
|
||||
exit(0)
|
||||
}
|
||||
Reference in New Issue
Block a user