| 1 | // vtest retry: 3 |
| 2 | // vtest build: !windows // fasthttp.Server.run is not implemented on windows yet |
| 3 | import veb |
| 4 | import veb.sse |
| 5 | import time |
| 6 | import net.http |
| 7 | |
| 8 | const port = 13008 |
| 9 | const localserver = 'http://127.0.0.1:${port}' |
| 10 | const exit_after = time.second * 10 |
| 11 | |
| 12 | pub struct Context { |
| 13 | veb.Context |
| 14 | } |
| 15 | |
| 16 | pub struct App { |
| 17 | mut: |
| 18 | started chan bool |
| 19 | } |
| 20 | |
| 21 | pub fn (mut app App) before_accept_loop() { |
| 22 | app.started <- true |
| 23 | } |
| 24 | |
| 25 | fn (app &App) sse(mut ctx Context) veb.Result { |
| 26 | ctx.takeover_conn() |
| 27 | spawn handle_sse_conn(mut ctx) |
| 28 | return veb.no_result() |
| 29 | } |
| 30 | |
| 31 | fn handle_sse_conn(mut ctx Context) { |
| 32 | mut sse_conn := sse.start_connection(mut ctx.Context) |
| 33 | |
| 34 | for _ in 0 .. 3 { |
| 35 | time.sleep(time.second) |
| 36 | sse_conn.send_message(data: 'ping') or { break } |
| 37 | } |
| 38 | sse_conn.close() |
| 39 | } |
| 40 | |
| 41 | fn testsuite_begin() { |
| 42 | mut app := &App{} |
| 43 | spawn fn () { |
| 44 | time.sleep(exit_after) |
| 45 | assert true == false, 'timeout reached!' |
| 46 | exit(1) |
| 47 | }() |
| 48 | |
| 49 | spawn veb.run_at[App, Context](mut app, port: port, family: .ip) |
| 50 | // app startup time |
| 51 | _ := <-app.started |
| 52 | } |
| 53 | |
| 54 | fn test_sse() ! { |
| 55 | mut x := http.get('${localserver}/sse')! |
| 56 | |
| 57 | connection := x.header.get(.connection) or { |
| 58 | assert true == false, 'Header Connection should be set!' |
| 59 | panic('missing header') |
| 60 | } |
| 61 | cache_control := x.header.get(.cache_control) or { |
| 62 | assert true == false, 'Header Cache-Control should be set!' |
| 63 | panic('missing header') |
| 64 | } |
| 65 | content_type := x.header.get(.content_type) or { |
| 66 | assert true == false, 'Header Content-Type should be set!' |
| 67 | panic('missing header') |
| 68 | } |
| 69 | assert connection == 'keep-alive' |
| 70 | assert cache_control == 'no-cache' |
| 71 | assert content_type == 'text/event-stream' |
| 72 | |
| 73 | eprintln(x.body) |
| 74 | assert x.body == 'data: ping\n\ndata: ping\n\ndata: ping\n\nevent: close\ndata: Closing the connection\nretry: -1\n\n' |
| 75 | } |
| 76 | |