v2 / vlib / veb / tests / catchall_route_order_regression_test.v
58 lines · 48 sloc · 1.59 KB · 344b9afcfe67902dd9660bd3b077f18464d1d114
Raw
1// vtest build: !windows // fasthttp.Server.run is not implemented on windows yet
2import net.http
3import time
4import veb
5
6const catchall_route_order_port = 13062
7
8struct CatchallRouteOrderContext {
9 veb.Context
10}
11
12struct CatchallRouteOrderApp {
13mut:
14 server &veb.Server = unsafe { nil }
15 started chan bool
16}
17
18pub fn (mut app CatchallRouteOrderApp) init_server(server &veb.Server) {
19 app.server = server
20}
21
22pub fn (mut app CatchallRouteOrderApp) before_accept_loop() {
23 app.started <- true
24}
25
26@['/:path...']
27fn (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']
33fn (mut app CatchallRouteOrderApp) get_user(mut ctx CatchallRouteOrderContext, id string) veb.Result {
34 return ctx.text('user ${id}')
35}
36
37fn 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