v2 / vlib / veb / tests / large_payload_test.v
126 lines · 100 sloc · 2.78 KB · 344b9afcfe67902dd9660bd3b077f18464d1d114
Raw
1// vtest retry: 3
2// vtest build: !windows // fasthttp.Server.run is not implemented on windows yet
3import veb
4import net.http
5import time
6import os
7
8const port = 13002
9
10const localserver = 'http://127.0.0.1:${port}'
11
12const exit_after = time.second * 10
13
14const tmp_file = os.join_path(os.vtmp_dir(), 'veb_large_payload.txt')
15
16pub struct App {
17mut:
18 started chan bool
19}
20
21pub fn (mut app App) before_accept_loop() {
22 app.started <- true
23}
24
25pub fn (mut app App) index(mut ctx Context) veb.Result {
26 return ctx.text('Hello V!')
27}
28
29@[post]
30pub fn (mut app App) post_request(mut ctx Context) veb.Result {
31 return ctx.text(ctx.req.data)
32}
33
34pub fn (app &App) file(mut ctx Context) veb.Result {
35 return ctx.file(tmp_file)
36}
37
38pub struct Context {
39 veb.Context
40}
41
42fn testsuite_begin() {
43 spawn fn () {
44 time.sleep(exit_after)
45 assert true == false, 'timeout reached!'
46 exit(1)
47 }()
48
49 mut app := &App{}
50 spawn veb.run_at[App, Context](mut app, port: port, timeout_in_seconds: 2, family: .ip)
51 // app startup time
52 _ := <-app.started
53}
54
55fn test_large_request_body() {
56 // string of a's of 8.96mb send over the connection
57 // veb reads a maximum of 4096KB per read cycle
58 // this test tests if veb is able to do multiple of these
59 // cycles and updates the response body each cycle
60 mut buf := []u8{len: veb.max_read * 10, init: `a`}
61
62 str := buf.bytestr()
63 mut x := http.post('${localserver}/post_request', str)!
64
65 assert x.body.len == veb.max_read * 10
66}
67
68fn test_large_request_header() {
69 // same test as test_large_request_body, but then with a large header,
70 // which is parsed separately
71 mut buf := []u8{len: veb.max_read * 2, init: `a`}
72
73 str := buf.bytestr()
74 // make 1 header longer than vebs max read limit
75 mut x := http.fetch(http.FetchConfig{
76 url: localserver
77 header: http.new_custom_header_from_map({
78 'X-Overflow-Header': str
79 })!
80 })!
81
82 assert x.status() == .request_entity_too_large
83}
84
85fn test_bigger_content_length() {
86 data := '123456789'
87 mut x := http.fetch(http.FetchConfig{
88 method: .post
89 url: '${localserver}/post_request'
90 header: http.new_header_from_map({
91 .content_length: '10'
92 })
93 data: data
94 })!
95
96 // Content-length is larger than the data sent, so the request should timeout
97 assert x.status() == .request_timeout
98}
99
100fn test_smaller_content_length() {
101 data := '123456789'
102 mut x := http.fetch(http.FetchConfig{
103 method: .post
104 url: '${localserver}/post_request'
105 header: http.new_header_from_map({
106 .content_length: '5'
107 })
108 data: data
109 })!
110
111 assert x.status() == .bad_request
112 assert x.body == 'Mismatch of body length and Content-Length header'
113}
114
115fn test_sendfile() {
116 mut buf := []u8{len: veb.max_write * 10, init: `a`}
117 os.write_file(tmp_file, buf.bytestr())!
118
119 x := http.get('${localserver}/file')!
120
121 assert x.body.len == veb.max_write * 10
122}
123
124fn testsuite_end() {
125 os.rm(tmp_file)!
126}
127