aboutsummaryrefslogtreecommitdiff
path: root/http_server_test.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2026-02-06 07:08:29 +0700
committerShulhan <ms@kilabit.info>2026-02-06 07:08:29 +0700
commit54d395f53e344a3cf3861c0e3d2f82ad14ecc602 (patch)
tree9f19525f8b950ddd9d715d9abe71e5545df4d65e /http_server_test.go
parenta3c27356bfe3e56809f579b617e02829a7ea0a68 (diff)
downloadawwan-54d395f53e344a3cf3861c0e3d2f82ad14ecc602.tar.xz
all: improve the Server-Sent Events (SSE) output
In the ExecResponse, store the event in the Output instead of message data, so the server can iterate the Output directly and pass it to WriteEvent directly. The event ID now start at 1 with type "begin". This is to minimize confusion when comparing empty Last-Event-ID from client, which is equal to 0.
Diffstat (limited to 'http_server_test.go')
-rw-r--r--http_server_test.go34
1 files changed, 14 insertions, 20 deletions
diff --git a/http_server_test.go b/http_server_test.go
index f6a32aa..d5cb270 100644
--- a/http_server_test.go
+++ b/http_server_test.go
@@ -222,6 +222,9 @@ func TestHttpServer_Execute(t *testing.T) {
log.Fatal(err)
}
}()
+ t.Cleanup(func() {
+ aww.Stop()
+ })
err = libnet.WaitAlive(`tcp`, address, 10*time.Second)
if err != nil {
@@ -299,25 +302,19 @@ func TestHttpServer_Execute(t *testing.T) {
for ever {
select {
case ev = <-ssec.C:
- if len(ev.Type) != 0 {
- fmt.Fprintf(&buf, "event: %s\n", ev.Type)
- }
- if len(ev.Data) != 0 {
- fmt.Fprintf(&buf, "data: %q\n", ev.Data)
- }
- if len(ev.ID) != 0 {
- fmt.Fprintf(&buf, "id: %s\n", ev.ID)
- }
- buf.WriteByte('\n')
+ fmt.Fprintf(&buf, "event: %s\ndata: %q\nid: %s\n\n",
+ ev.Type, ev.Data, ev.ID)
if ev.Type == "end" {
ever = false
break
}
+ timeWait.Reset(time.Second)
case <-timeWait.C:
break
}
}
+ timeWait.Stop()
test.Assert(t, `Execute tail`, string(tdata.Output[`local:/local.aww:1-:tail`]), buf.String())
}
@@ -353,6 +350,9 @@ func TestHttpServer_ExecuteCancel(t *testing.T) {
log.Fatal(err)
}
}()
+ t.Cleanup(func() {
+ aww.Stop()
+ })
err = libnet.WaitAlive(`tcp`, address, 10*time.Second)
if err != nil {
@@ -430,16 +430,8 @@ func TestHttpServer_ExecuteCancel(t *testing.T) {
for ever {
select {
case ev = <-ssec.C:
- if len(ev.Type) != 0 {
- fmt.Fprintf(&buf, "event: %s\n", ev.Type)
- }
- if len(ev.Data) != 0 {
- fmt.Fprintf(&buf, "data: %q\n", ev.Data)
- }
- if len(ev.ID) != 0 {
- fmt.Fprintf(&buf, "id: %s\n", ev.ID)
- }
- buf.WriteByte('\n')
+ fmt.Fprintf(&buf, "event: %s\ndata: %q\nid: %s\n\n",
+ ev.Type, ev.Data, ev.ID)
if ev.ID == `1` {
testDoExecuteCancel(t, tdata, cl, execResp.ID)
@@ -449,10 +441,12 @@ func TestHttpServer_ExecuteCancel(t *testing.T) {
ever = false
break
}
+ timeWait.Reset(time.Second)
case <-timeWait.C:
break
}
}
+ timeWait.Stop()
test.Assert(t, `Execute cancel`, string(tdata.Output[`local:/cancel.aww:1-:tail`]), buf.String())
}