v2 / vlib / veb / tests / controller_test.v
132 lines · 102 sloc · 3.0 KB · 344b9afcfe67902dd9660bd3b077f18464d1d114
Raw
1// vtest build: !windows // fasthttp.Server.run is not implemented on windows yet
2import veb
3import time
4import os
5import net.http
6
7const port = 13006
8
9const localserver = 'http://127.0.0.1:${port}'
10
11const exit_after = time.second * 10
12
13pub struct Context {
14 veb.Context
15}
16
17pub struct App {
18 veb.Controller
19mut:
20 started chan bool
21}
22
23pub fn (mut app App) before_accept_loop() {
24 app.started <- true
25}
26
27pub fn (app &App) index(mut ctx Context) veb.Result {
28 return ctx.text('from app')
29}
30
31@['/conflict/test']
32pub fn (app &App) conflicting(mut ctx Context) veb.Result {
33 return ctx.text('from conflicting')
34}
35
36pub struct Other {
37 veb.Controller
38}
39
40pub fn (app &Other) index(mut ctx Context) veb.Result {
41 return ctx.text('from other')
42}
43
44pub struct HiddenByOther {}
45
46pub fn (app &HiddenByOther) index(mut ctx Context) veb.Result {
47 return ctx.text('from hidden')
48}
49
50pub struct SubController {}
51
52pub fn (app &SubController) index(mut ctx Context) veb.Result {
53 return ctx.text('from sub')
54}
55
56fn 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
80fn test_app_home() {
81 x := http.get(localserver)!
82 assert x.body == 'from app'
83}
84
85fn test_other() {
86 x := http.get('${localserver}/other')!
87 assert x.body == 'from other'
88}
89
90fn test_sub_controller() {
91 x := http.get('${localserver}/other/sub')!
92 assert x.body == 'from sub'
93}
94
95fn test_hidden_route() {
96 x := http.get('${localserver}/other/hide')!
97 assert x.body == 'from hidden'
98}
99
100fn 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
119fn 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