| 1 | // vtest build: !windows // fasthttp.Server.run is not implemented on windows yet |
| 2 | import net.http |
| 3 | import time |
| 4 | import veb |
| 5 | |
| 6 | const catchall_route_order_port = 13062 |
| 7 | |
| 8 | struct CatchallRouteOrderContext { |
| 9 | veb.Context |
| 10 | } |
| 11 | |
| 12 | struct CatchallRouteOrderApp { |
| 13 | mut: |
| 14 | server &veb.Server = unsafe { nil } |
| 15 | started chan bool |
| 16 | } |
| 17 | |
| 18 | pub fn (mut app CatchallRouteOrderApp) init_server(server &veb.Server) { |
| 19 | app.server = server |
| 20 | } |
| 21 | |
| 22 | pub fn (mut app CatchallRouteOrderApp) before_accept_loop() { |
| 23 | app.started <- true |
| 24 | } |
| 25 | |
| 26 | @['/:path...'] |
| 27 | fn (mut app CatchallRouteOrderApp) any(mut ctx CatchallRouteOrderContext, path string) veb.Result { |
| 28 | ctx.res.set_status(.not_found) |
| 29 | return ctx.text('fallback ${path}') |
| 30 | } |
| 31 | |
| 32 | @['/api/user/:id'] |
| 33 | fn (mut app CatchallRouteOrderApp) get_user(mut ctx CatchallRouteOrderContext, id string) veb.Result { |
| 34 | return ctx.text('user ${id}') |
| 35 | } |
| 36 | |
| 37 | fn test_variadic_routes_do_not_override_more_specific_handlers() { |
| 38 | mut app := &CatchallRouteOrderApp{} |
| 39 | spawn veb.run_at[CatchallRouteOrderApp, CatchallRouteOrderContext](mut app, |
| 40 | port: catchall_route_order_port |
| 41 | show_startup_message: false |
| 42 | timeout_in_seconds: 1 |
| 43 | family: .ip |
| 44 | ) |
| 45 | _ := <-app.started |
| 46 | app.server.wait_till_running(max_retries: 1000, retry_period_ms: 10)! |
| 47 | defer { |
| 48 | app.server.shutdown(timeout: 5 * time.second) or {} |
| 49 | } |
| 50 | |
| 51 | user := http.get('http://127.0.0.1:${catchall_route_order_port}/api/user/42')! |
| 52 | assert user.status() == .ok |
| 53 | assert user.body == 'user 42' |
| 54 | |
| 55 | missing := http.get('http://127.0.0.1:${catchall_route_order_port}/missing/path')! |
| 56 | assert missing.status() == .not_found |
| 57 | assert missing.body == 'fallback /missing/path' |
| 58 | } |
| 59 | |