v2 / vlib / veb / controller_static_regression_test.v
114 lines · 92 sloc · 3.44 KB · 184883b418c7ec2febd5d4def70259ad81f7bb4b
Raw
1module veb
2
3import net.http
4import net.urllib
5import os
6
7struct RegressionContext {
8 Context
9}
10
11struct AppStaticRegressionApp {
12 Controller
13 StaticHandler
14}
15
16struct AppStaticRootController {}
17
18fn (app &AppStaticRootController) index(mut ctx RegressionContext) Result {
19 return ctx.text('from app static root controller')
20}
21
22struct ControllerStaticRootBase {
23 StaticHandler
24}
25
26struct ControllerStaticRootController {
27 ControllerStaticRootBase
28}
29
30fn (app &ControllerStaticRootController) index(mut ctx RegressionContext) Result {
31 return ctx.text('from controller static root controller')
32}
33
34fn test_app_static_is_served_before_root_controller() {
35 root_file := regression_testdata_path('root.txt')
36 sub_dir := regression_testdata_path('sub_folder')
37 sub_file := os.join_path(sub_dir, 'sub.txt')
38
39 mut root_controller := AppStaticRootController{}
40 mut app := AppStaticRegressionApp{}
41 app.serve_static('/root.txt', root_file) or { panic(err) }
42 app.handle_static(sub_dir, true) or { panic(err) }
43 app.register_controller[AppStaticRootController, RegressionContext]('/', mut root_controller) or {
44 panic(err)
45 }
46
47 completed_root := regression_handle_app_request(mut app, regression_request('/root.txt'))
48 assert completed_root.return_type == .file
49 assert completed_root.return_file == root_file
50
51 completed_sub := regression_handle_app_request(mut app, regression_request('/sub.txt'))
52 assert completed_sub.return_type == .file
53 assert completed_sub.return_file == sub_file
54}
55
56fn test_controller_static_keeps_root_relative_paths() {
57 root_file := regression_testdata_path('root.txt')
58 sub_dir := regression_testdata_path('sub_folder')
59 sub_file := os.join_path(sub_dir, 'sub.txt')
60
61 mut controller_app := ControllerStaticRootController{}
62 controller_app.serve_static('/root.txt', root_file) or { panic(err) }
63 controller_app.handle_static(sub_dir, true) or { panic(err) }
64
65 ctrl_path := controller[ControllerStaticRootController, RegressionContext]('/', mut
66 controller_app) or { panic(err) }
67
68 completed_root := regression_handle_controller_request(ctrl_path, '/root.txt')
69 assert completed_root.return_type == .file
70 assert completed_root.return_file == root_file
71
72 completed_sub := regression_handle_controller_request(ctrl_path, '/sub.txt')
73 assert completed_sub.return_type == .file
74 assert completed_sub.return_file == sub_file
75}
76
77fn regression_request(url string) http.Request {
78 return http.Request{
79 method: .get
80 url: url
81 header: http.new_custom_header_from_map({
82 'Host': '127.0.0.1'
83 }) or { panic(err) }
84 }
85}
86
87fn regression_testdata_path(rel string) string {
88 return os.join_path(os.dir(@FILE), 'tests', 'testdata', rel)
89}
90
91fn regression_handle_controller_request(ctrl_path &ControllerPath, path string) &Context {
92 req := regression_request(path)
93 mut url := urllib.parse_request_uri(path) or { panic(err) }
94 ctx := &Context{
95 req: req
96 }
97 return ctrl_path.handler(ctx, mut url, '')
98}
99
100fn regression_handle_app_request(mut app AppStaticRegressionApp, req http.Request) &Context {
101 routes := generate_routes[AppStaticRegressionApp, RegressionContext](app) or { panic(err) }
102 controllers_sorted := check_duplicate_routes_in_controllers[AppStaticRegressionApp](app, routes) or {
103 panic(err)
104 }
105
106 params := RequestParams{
107 global_app: unsafe { voidptr(&app) }
108 controllers_sorted: controllers_sorted
109 routes: &routes
110 benchmark_page_generation: false
111 }
112 return handle_request_and_route[AppStaticRegressionApp, RegressionContext](mut app, req, 0,
113 params)
114}
115