v2 / vlib / veb / tests / ssl_test.v
46 lines · 40 sloc · 1000 bytes · 4b9fe554a2acbda8e91e6d484bb7826fa3eb527b
Raw
1// vtest build: !sanitized_job?
2import net.http
3import net.mbedtls
4import os
5import veb
6
7const https_port = 13013
8
9pub struct Context {
10 veb.Context
11}
12
13pub struct App {
14 started chan bool
15}
16
17pub fn (mut app App) before_accept_loop() {
18 app.started <- true
19}
20
21pub fn (app &App) index(mut ctx Context) veb.Result {
22 return ctx.text('secure')
23}
24
25fn test_veb_serves_https_requests() {
26 cert_path := os.join_path(@VMODROOT, 'examples', 'ssl_server', 'cert', 'server.crt')
27 key_path := os.join_path(@VMODROOT, 'examples', 'ssl_server', 'cert', 'server.key')
28 mut app := &App{}
29 spawn veb.run_at[App, Context](mut app,
30 host: '127.0.0.1'
31 port: https_port
32 family: .ip
33 timeout_in_seconds: 2
34 ssl_config: mbedtls.SSLConnectConfig{
35 cert: cert_path
36 cert_key: key_path
37 }
38 )
39 _ := <-app.started
40 res := http.fetch(
41 url: 'https://127.0.0.1:${https_port}/'
42 validate: false
43 )!
44 assert res.status_code == 200
45 assert res.body == 'secure'
46}
47