v2 / vlib / veb / tests / veb_aliased_app_and_context_test.v
63 lines · 53 sloc · 1.25 KB · 344b9afcfe67902dd9660bd3b077f18464d1d114
Raw
1// vtest build: !windows // fasthttp.Server.run is not implemented on windows yet
2import os
3import veb
4import time
5import net.http
6
7const app_port = 29123
8const exit_after = 10 * time.second
9
10pub struct Context {
11 veb.Context
12}
13
14pub struct App {
15 veb.Middleware[Context]
16 veb.Controller
17 veb.StaticHandler
18 started chan bool
19}
20
21pub fn (mut app App) before_accept_loop() {
22 app.started <- true
23}
24
25pub fn (mut app App) index(mut ctx Context) veb.Result {
26 return ctx.text('Hello V!')
27}
28
29type AliasApp = App
30type AliasContext = Context
31
32fn testsuite_begin() {
33 os.chdir(os.dir(@FILE))!
34 spawn fn () {
35 time.sleep(exit_after)
36 assert true == false, 'timeout reached!'
37 exit(1)
38 }()
39}
40
41fn test_aliased_app_compiles_and_starts() {
42 mut app := &AliasApp{}
43 spawn veb.run_at[AliasApp, AliasContext](mut app,
44 port: app_port
45 timeout_in_seconds: 2
46 family: .ip
47 )
48 eprintln('waiting for app to start ...')
49 _ := <-app.started
50 eprintln('>>> app was started')
51}
52
53fn test_static_root() {
54 x := http.get('http://127.0.0.1:${app_port}/')!
55 eprintln('>>>> http request was sent and received')
56 assert x.status() == .ok
57 assert x.body == 'index success'
58}
59
60@['/'; get]
61fn (mut app AliasApp) index(mut ctx AliasContext) veb.Result {
62 return ctx.text('index success')
63}
64