| 1 | // vtest build: !windows // fasthttp.Server.run is not implemented on windows yet |
| 2 | import veb |
| 3 | import time |
| 4 | import os |
| 5 | import net.http |
| 6 | |
| 7 | const port = 13006 |
| 8 | |
| 9 | const localserver = 'http://127.0.0.1:${port}' |
| 10 | |
| 11 | const exit_after = time.second * 10 |
| 12 | |
| 13 | pub struct Context { |
| 14 | veb.Context |
| 15 | } |
| 16 | |
| 17 | pub struct App { |
| 18 | veb.Controller |
| 19 | mut: |
| 20 | started chan bool |
| 21 | } |
| 22 | |
| 23 | pub fn (mut app App) before_accept_loop() { |
| 24 | app.started <- true |
| 25 | } |
| 26 | |
| 27 | pub fn (app &App) index(mut ctx Context) veb.Result { |
| 28 | return ctx.text('from app') |
| 29 | } |
| 30 | |
| 31 | @['/conflict/test'] |
| 32 | pub fn (app &App) conflicting(mut ctx Context) veb.Result { |
| 33 | return ctx.text('from conflicting') |
| 34 | } |
| 35 | |
| 36 | pub struct Other { |
| 37 | veb.Controller |
| 38 | } |
| 39 | |
| 40 | pub fn (app &Other) index(mut ctx Context) veb.Result { |
| 41 | return ctx.text('from other') |
| 42 | } |
| 43 | |
| 44 | pub struct HiddenByOther {} |
| 45 | |
| 46 | pub fn (app &HiddenByOther) index(mut ctx Context) veb.Result { |
| 47 | return ctx.text('from hidden') |
| 48 | } |
| 49 | |
| 50 | pub struct SubController {} |
| 51 | |
| 52 | pub fn (app &SubController) index(mut ctx Context) veb.Result { |
| 53 | return ctx.text('from sub') |
| 54 | } |
| 55 | |
| 56 | fn testsuite_begin() { |
| 57 | os.chdir(os.dir(@FILE))! |
| 58 | |
| 59 | mut sub := &SubController{} |
| 60 | mut other := &Other{} |
| 61 | other.register_controller[SubController, Context]('/sub', mut sub)! |
| 62 | mut hidden := &HiddenByOther{} |
| 63 | |
| 64 | mut app := &App{} |
| 65 | app.register_controller[Other, Context]('/other', mut other)! |
| 66 | // controllers should be sorted, so this controller should be accessible |
| 67 | // even though it is declared last |
| 68 | app.register_controller[HiddenByOther, Context]('/other/hide', mut hidden)! |
| 69 | |
| 70 | spawn veb.run_at[App, Context](mut app, port: port, timeout_in_seconds: 2, family: .ip) |
| 71 | _ := <-app.started |
| 72 | |
| 73 | spawn fn () { |
| 74 | time.sleep(exit_after) |
| 75 | assert true == false, 'timeout reached!' |
| 76 | exit(1) |
| 77 | }() |
| 78 | } |
| 79 | |
| 80 | fn test_app_home() { |
| 81 | x := http.get(localserver)! |
| 82 | assert x.body == 'from app' |
| 83 | } |
| 84 | |
| 85 | fn test_other() { |
| 86 | x := http.get('${localserver}/other')! |
| 87 | assert x.body == 'from other' |
| 88 | } |
| 89 | |
| 90 | fn test_sub_controller() { |
| 91 | x := http.get('${localserver}/other/sub')! |
| 92 | assert x.body == 'from sub' |
| 93 | } |
| 94 | |
| 95 | fn test_hidden_route() { |
| 96 | x := http.get('${localserver}/other/hide')! |
| 97 | assert x.body == 'from hidden' |
| 98 | } |
| 99 | |
| 100 | fn test_conflicting_controllers() { |
| 101 | mut other := &Other{} |
| 102 | |
| 103 | mut app := &App{} |
| 104 | app.register_controller[Other, Context]('/other', mut other) or { |
| 105 | assert true == false, 'this should not fail' |
| 106 | } |
| 107 | |
| 108 | app.register_controller[Other, Context]('/other', mut other) or { |
| 109 | assert true == false, 'this should not fail' |
| 110 | } |
| 111 | |
| 112 | veb.run_at[App, Context](mut app, port: port) or { |
| 113 | assert err.msg() == 'conflicting paths: duplicate controller handling the route "/other"' |
| 114 | return |
| 115 | } |
| 116 | assert true == false, 'the previous call should have failed!' |
| 117 | } |
| 118 | |
| 119 | fn test_conflicting_controller_routes() { |
| 120 | mut other := &Other{} |
| 121 | |
| 122 | mut app := &App{} |
| 123 | app.register_controller[Other, Context]('/conflict', mut other) or { |
| 124 | assert true == false, 'this should not fail' |
| 125 | } |
| 126 | |
| 127 | veb.run_at[App, Context](mut app, port: port) or { |
| 128 | assert err.msg() == 'conflicting paths: method "conflicting" with route "/conflict/test" should be handled by the Controller of path "/conflict"' |
| 129 | return |
| 130 | } |
| 131 | assert true == false, 'the previous call should have failed!' |
| 132 | } |
| 133 | |