| 1 | // vtest build: !windows // fasthttp.Server.run is not implemented on windows yet |
| 2 | import os |
| 3 | import veb |
| 4 | import time |
| 5 | import net.http |
| 6 | |
| 7 | const app_port = 29123 |
| 8 | const exit_after = 10 * time.second |
| 9 | |
| 10 | pub struct Context { |
| 11 | veb.Context |
| 12 | } |
| 13 | |
| 14 | pub struct App { |
| 15 | veb.Middleware[Context] |
| 16 | veb.Controller |
| 17 | veb.StaticHandler |
| 18 | started chan bool |
| 19 | } |
| 20 | |
| 21 | pub fn (mut app App) before_accept_loop() { |
| 22 | app.started <- true |
| 23 | } |
| 24 | |
| 25 | pub fn (mut app App) index(mut ctx Context) veb.Result { |
| 26 | return ctx.text('Hello V!') |
| 27 | } |
| 28 | |
| 29 | type AliasApp = App |
| 30 | type AliasContext = Context |
| 31 | |
| 32 | fn 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 | |
| 41 | fn 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 | |
| 53 | fn 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] |
| 61 | fn (mut app AliasApp) index(mut ctx AliasContext) veb.Result { |
| 62 | return ctx.text('index success') |
| 63 | } |
| 64 | |