Live video on the AT Protocol
1package misttriggers
2
3import (
4 "bytes"
5 "context"
6 "fmt"
7 "net/http"
8 "net/http/httptest"
9 "testing"
10
11 "github.com/stretchr/testify/require"
12 "stream.place/streamplace/pkg/config"
13)
14
15var pushOutStartPayload = MistTriggerBody(`
16 video+c447r0acdmqhhhpb
17 rtmp://rtmp.livepeer.com/live/stream-key?video=maxbps&audio=maxbps
18`)
19
20var pushOutStartPayloadInvalidLines = MistTriggerBody(`
21 video+c447r0acdmqhhhpb
22`)
23
24func TestItCanParseAValidPushOutStartPayload(t *testing.T) {
25 p, err := ParsePushOutStartPayload(pushOutStartPayload)
26 require.NoError(t, err)
27 require.Equal(t, p.URL, "rtmp://rtmp.livepeer.com/live/stream-key?video=maxbps&audio=maxbps")
28 require.Equal(t, p.StreamName, "video+c447r0acdmqhhhpb")
29}
30
31func ItCanRejectABadPushOutStartPayload(t *testing.T) {
32 _, err := ParsePushOutStartPayload(pushOutStartPayloadInvalidLines)
33 require.Error(t, err)
34}
35
36func doPushOutStartRequest(t *testing.T, payload MistTriggerBody, cb func(ctx context.Context, p *PushOutStartPayload) (string, error)) *httptest.ResponseRecorder {
37 broker := NewTriggerBroker()
38 broker.OnPushOutStart(cb)
39 d := NewMistCallbackHandlersCollection(&config.CLI{}, broker)
40 req, err := http.NewRequest("POST", "/trigger", bytes.NewBuffer([]byte(payload)))
41 require.NoError(t, err)
42 rr := httptest.NewRecorder()
43 d.TriggerPushOutStart(context.Background(), rr, req, payload)
44 return rr
45}
46
47func TestItCanHandlePushOutStartRequests(t *testing.T) {
48 rr := doPushOutStartRequest(t, pushOutStartPayload, func(ctx context.Context, p *PushOutStartPayload) (string, error) {
49 require.Equal(t, p.URL, "rtmp://rtmp.livepeer.com/live/stream-key?video=maxbps&audio=maxbps")
50 return "rtmp://example.com/live/funky-stream", nil
51 })
52 require.Equal(t, rr.Result().StatusCode, 200)
53 require.Equal(t, rr.Body.String(), "rtmp://example.com/live/funky-stream")
54}
55
56func TestItCanRejectPushOutStartRequests(t *testing.T) {
57 rr := doPushOutStartRequest(t, pushOutStartPayload, func(ctx context.Context, p *PushOutStartPayload) (string, error) {
58 // Proper way to reject Mist
59 return "", nil
60 })
61 require.Equal(t, rr.Result().StatusCode, 200)
62 require.Equal(t, rr.Body.String(), "")
63}
64
65func TestPushOutStartCan500(t *testing.T) {
66 rr := doPushOutStartRequest(t, pushOutStartPayload, func(ctx context.Context, p *PushOutStartPayload) (string, error) {
67 return "", fmt.Errorf("something went wrong")
68 })
69 require.Equal(t, rr.Result().StatusCode, 500)
70}
71
72func TestItCanErrorPushOutStartRequests(t *testing.T) {
73 rr := doPushOutStartRequest(t, pushOutStartPayloadInvalidLines, func(ctx context.Context, p *PushOutStartPayload) (string, error) {
74 require.Fail(t, "test should be failing before it gets to me")
75 return "", nil
76 })
77 require.Equal(t, rr.Result().StatusCode, 400)
78}