| 1 | module veb |
| 2 | |
| 3 | import net.http |
| 4 | import net.urllib |
| 5 | import os |
| 6 | |
| 7 | struct RegressionContext { |
| 8 | Context |
| 9 | } |
| 10 | |
| 11 | struct AppStaticRegressionApp { |
| 12 | Controller |
| 13 | StaticHandler |
| 14 | } |
| 15 | |
| 16 | struct AppStaticRootController {} |
| 17 | |
| 18 | fn (app &AppStaticRootController) index(mut ctx RegressionContext) Result { |
| 19 | return ctx.text('from app static root controller') |
| 20 | } |
| 21 | |
| 22 | struct ControllerStaticRootBase { |
| 23 | StaticHandler |
| 24 | } |
| 25 | |
| 26 | struct ControllerStaticRootController { |
| 27 | ControllerStaticRootBase |
| 28 | } |
| 29 | |
| 30 | fn (app &ControllerStaticRootController) index(mut ctx RegressionContext) Result { |
| 31 | return ctx.text('from controller static root controller') |
| 32 | } |
| 33 | |
| 34 | fn 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 | |
| 56 | fn 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 | |
| 77 | fn 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 | |
| 87 | fn regression_testdata_path(rel string) string { |
| 88 | return os.join_path(os.dir(@FILE), 'tests', 'testdata', rel) |
| 89 | } |
| 90 | |
| 91 | fn 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 | |
| 100 | fn 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 | |