| 1 | // vtest build: !sanitized_job? |
| 2 | import net.http |
| 3 | import net.mbedtls |
| 4 | import os |
| 5 | import veb |
| 6 | |
| 7 | const https_port = 13013 |
| 8 | |
| 9 | pub struct Context { |
| 10 | veb.Context |
| 11 | } |
| 12 | |
| 13 | pub struct App { |
| 14 | started chan bool |
| 15 | } |
| 16 | |
| 17 | pub fn (mut app App) before_accept_loop() { |
| 18 | app.started <- true |
| 19 | } |
| 20 | |
| 21 | pub fn (app &App) index(mut ctx Context) veb.Result { |
| 22 | return ctx.text('secure') |
| 23 | } |
| 24 | |
| 25 | fn 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 | |