-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
static_handler_test.v
121 lines (92 loc) · 2.71 KB
/
static_handler_test.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import x.vweb
import net.http
import os
import time
const port = 13003
const localserver = 'http://127.0.0.1:${port}'
const exit_after = time.second * 10
pub struct App {
vweb.StaticHandler
mut:
started chan bool
}
pub fn (mut app App) before_accept_loop() {
app.started <- true
}
pub fn (mut app App) index(mut ctx Context) vweb.Result {
return ctx.text('Hello V!')
}
@[post]
pub fn (mut app App) post_request(mut ctx Context) vweb.Result {
return ctx.text(ctx.req.data)
}
pub struct Context {
vweb.Context
}
fn testsuite_begin() {
os.chdir(os.dir(@FILE))!
spawn fn () {
time.sleep(exit_after)
assert true == false, 'timeout reached!'
exit(1)
}()
run_app_test()
}
fn run_app_test() {
mut app := &App{}
if _ := app.handle_static('testdata', true) {
assert true == false, 'should throw unkown mime type error'
} else {
assert err.msg().starts_with('unkown MIME type for file extension ".what"'), 'throws error on unkown mime type'
}
app.static_mime_types['.what'] = vweb.mime_types['.txt']
if _ := app.handle_static('not_found', true) {
assert false, 'should throw directory not found error'
} else {
assert err.msg().starts_with('directory `not_found` does not exist') == true
}
app.handle_static('testdata', true) or { panic(err) }
if _ := app.mount_static_folder_at('testdata', 'static') {
assert true == false, 'should throw invalid mount path error'
} else {
assert err.msg() == 'invalid mount path! The path should start with `/`'
}
if _ := app.mount_static_folder_at('not_found', '/static') {
assert true == false, 'should throw mount path does not exist error'
} else {
assert err.msg().starts_with('directory `not_found` does not exist') == true
}
app.mount_static_folder_at('testdata', '/static') or { panic(err) }
spawn vweb.run_at[App, Context](mut app, port: port, timeout_in_seconds: 2, family: .ip)
// app startup time
_ := <-app.started
}
fn test_static_root() {
x := http.get('${localserver}/root.txt')!
assert x.status() == .ok
assert x.body == 'root'
}
fn test_scans_subdirs() {
x := http.get('${localserver}/sub_folder/sub.txt')!
assert x.status() == .ok
assert x.body == 'sub'
}
fn test_index_subdirs() {
x := http.get('${localserver}/sub_folder/')!
y := http.get('${localserver}/sub.folder/sub_folder')!
assert x.status() == .ok
assert x.body.trim_space() == 'OK'
assert y.status() == .ok
assert y.body.trim_space() == 'OK'
}
fn test_custom_mime_types() {
x := http.get('${localserver}/unkown_mime.what')!
assert x.status() == .ok
assert x.header.get(.content_type)! == vweb.mime_types['.txt']
assert x.body == 'unkown_mime'
}
fn test_custom_folder_mount() {
x := http.get('${localserver}/static/root.txt')!
assert x.status() == .ok
assert x.body == 'root'
}