Live video on the AT Protocol
1package misttriggers
2
3import (
4 "bytes"
5 "context"
6 "net/http"
7 "net/http/httptest"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11 "stream.place/streamplace/pkg/config"
12)
13
14var userEndPayload = MistTriggerBody(`
15 3132396757
16 video+788dip9jqar876kl
17 HLS
18 154.47.98.190
19 15
20 1049826
21 10173
22 [UA:VLC/3.0.18 LibVLC/3.0.18]
23 22
24 22
25 22
26 6ea6ddaf2565aaee726fa802a5ecc28210b345a4a875a3c8122fe985943e6a8d
27`)
28
29var userEndPayloadBadLines = MistTriggerBody(`
30 too
31 few
32 lines
33`)
34
35func TestItCanParseAValidUserEndPayload(t *testing.T) {
36 p, err := ParseUserEndPayload(userEndPayload, "example-uuid")
37 require.NoError(t, err)
38 require.Equal(t, 1, len(p.StreamNames))
39 require.Equal(t, "video+788dip9jqar876kl", p.StreamNames[0])
40}
41
42func TestItCanRejectABadUserEndPayload(t *testing.T) {
43 _, err := ParseUserEndPayload(userEndPayloadBadLines, "example-uuid")
44 require.ErrorContains(t, err, "expected 12 lines in USER_NEW payload")
45}
46
47func doUserEndRequest(t *testing.T, payload MistTriggerBody, cb func(ctx context.Context, p *UserEndPayload) error) *httptest.ResponseRecorder {
48 broker := NewTriggerBroker()
49 broker.OnUserEnd(cb)
50 d := NewMistCallbackHandlersCollection(&config.CLI{}, broker)
51 req, err := http.NewRequest("POST", "/trigger", bytes.NewBuffer([]byte(payload)))
52 require.NoError(t, err)
53 rr := httptest.NewRecorder()
54 d.TriggerUserEnd(context.Background(), rr, req, payload)
55 return rr
56}
57
58func TestItCanHandleUserEndRequests(t *testing.T) {
59 rr := doUserEndRequest(t, userEndPayload, func(ctx context.Context, p *UserEndPayload) error {
60 require.Equal(t, 1, len(p.StreamNames))
61 require.Equal(t, "video+788dip9jqar876kl", p.StreamNames[0])
62 return nil
63 })
64 require.Equal(t, rr.Result().StatusCode, 200)
65}
66
67func TestItCanErrorUserEndRequests(t *testing.T) {
68 rr := doUserEndRequest(t, userEndPayloadBadLines, func(ctx context.Context, p *UserEndPayload) error {
69 require.Fail(t, "test should be failing before it gets to me")
70 return nil
71 })
72 require.Equal(t, rr.Result().StatusCode, 400)
73}