From 65cc1c696094b6359231637fafb7f8393bdb0d6f Mon Sep 17 00:00:00 2001 From: David Colburn Date: Thu, 8 Aug 2024 17:15:40 -0400 Subject: [PATCH] update urls_test --- pkg/config/urls.go | 36 +++++++++++++++--------------------- pkg/config/urls_test.go | 40 +++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/pkg/config/urls.go b/pkg/config/urls.go index 1081b77c..291f25b6 100644 --- a/pkg/config/urls.go +++ b/pkg/config/urls.go @@ -79,17 +79,13 @@ func (o *StreamConfig) ValidateUrl(rawUrl string, outputType types.OutputType) ( } else if parsedUrl.Scheme == "twitch" { parsed, err = o.updateTwitchURL(parsedUrl.Host) if err != nil { - err = errors.ErrInvalidUrl(rawUrl, err.Error()) return } } else if match := twitchEndpoint.FindStringSubmatch(rawUrl); len(match) > 0 { - updated, err := o.updateTwitchURL(match[1]) - if err == nil { + if updated, err := o.updateTwitchURL(match[1]); err == nil { parsed = updated } - } - - if parsed == "" { + } else { parsed = rawUrl } @@ -122,30 +118,28 @@ func (o *StreamConfig) GetStream(rawUrl string) (*Stream, error) { return nil, errors.ErrInvalidUrl(rawUrl, err.Error()) } - var parsed, twitchStreamID string + var parsed string if parsedUrl.Scheme == "mux" { parsed = fmt.Sprintf("rtmps://global-live.mux.com:443/app/%s", parsedUrl.Host) } else if parsedUrl.Scheme == "twitch" { - twitchStreamID = parsedUrl.Host + parsed, err = o.updateTwitchURL(parsedUrl.Host) + if err != nil { + return nil, err + } } else if match := twitchEndpoint.FindStringSubmatch(rawUrl); len(match) > 0 { - twitchStreamID = match[1] + parsed, err = o.updateTwitchURL(match[1]) + if err != nil { + return nil, err + } } else { parsed = rawUrl } - var stream *Stream - o.Streams.Range(func(url, s any) bool { - if (parsed != "" && url == parsed) || (twitchStreamID != "" && s.(*Stream).StreamID == twitchStreamID) { - stream = s.(*Stream) - return false - } - return true - }) - - if stream != nil { - return stream, nil + stream, ok := o.Streams.Load(parsed) + if !ok { + return nil, errors.ErrStreamNotFound(rawUrl) } - return nil, errors.ErrStreamNotFound(rawUrl) + return stream.(*Stream), nil } func (o *StreamConfig) updateTwitchURL(key string) (string, error) { diff --git a/pkg/config/urls_test.go b/pkg/config/urls_test.go index a3501579..9a9fa0e3 100644 --- a/pkg/config/urls_test.go +++ b/pkg/config/urls_test.go @@ -16,12 +16,12 @@ package config import ( "regexp" + "strings" "testing" "github.com/stretchr/testify/require" "github.com/livekit/egress/pkg/types" - "github.com/livekit/protocol/livekit" ) func TestValidateUrl(t *testing.T) { @@ -33,12 +33,12 @@ func TestValidateUrl(t *testing.T) { for _, test := range []struct { url string twitch bool - updated string + parsed string redacted string }{ { url: "mux://streamkey", - updated: "rtmps://global-live.mux.com:443/app/streamkey", + parsed: "rtmps://global-live.mux.com:443/app/streamkey", redacted: "rtmps://global-live.mux.com:443/app/{str...key}", }, { @@ -51,52 +51,54 @@ func TestValidateUrl(t *testing.T) { }, { url: "rtmp://localhost:1935/live/streamkey", - updated: "rtmp://localhost:1935/live/streamkey", + parsed: "rtmp://localhost:1935/live/streamkey", redacted: "rtmp://localhost:1935/live/{str...key}", }, { url: "rtmps://localhost:1935/live/streamkey", - updated: "rtmps://localhost:1935/live/streamkey", + parsed: "rtmps://localhost:1935/live/streamkey", redacted: "rtmps://localhost:1935/live/{str...key}", }, } { - updated, redacted, err := o.ValidateUrl(test.url, types.OutputTypeRTMP) + parsed, redacted, streamID, err := o.ValidateUrl(test.url, types.OutputTypeRTMP) require.NoError(t, err) + require.NotEmpty(t, streamID) if test.twitch { - require.NotEmpty(t, twitchUpdated.FindString(updated), updated) + require.NotEmpty(t, twitchUpdated.FindString(parsed), parsed) require.NotEmpty(t, twitchRedacted.FindString(redacted), redacted) } else { - require.Equal(t, test.updated, updated) + require.Equal(t, test.parsed, parsed) require.Equal(t, test.redacted, redacted) } } } func TestGetUrl(t *testing.T) { + o := &StreamConfig{} + require.NoError(t, o.updateTwitchTemplate()) + + parsedTwitchUrl := strings.ReplaceAll(o.twitchTemplate, "{stream_key}", "streamkey") urls := []string{ "rtmps://global-live.mux.com:443/app/streamkey", - "rtmp://sfo.contribute.live-video.net/app/streamkey", - "rtmp://sfo.contribute.live-video.net/app/streamkey", + parsedTwitchUrl, + parsedTwitchUrl, "rtmp://localhost:1935/live/streamkey", } - o := &StreamConfig{ - StreamInfo: map[string]*livekit.StreamInfo{ - urls[0]: {Url: urls[0]}, - urls[1]: {Url: urls[1]}, - urls[3]: {Url: urls[3]}, - }, + for _, url := range []string{urls[0], urls[1], urls[3]} { + _, err := o.AddStream(url, types.OutputTypeRTMP) + require.NoError(t, err) } for i, rawUrl := range []string{ "mux://streamkey", "twitch://streamkey", - "rtmp://jfk.contribute.live-video.net/app/streamkey", + "rtmp://any.contribute.live-video.net/app/streamkey", "rtmp://localhost:1935/live/streamkey", } { - url, err := o.GetStreamUrl(rawUrl) + stream, err := o.GetStream(rawUrl) require.NoError(t, err) - require.Equal(t, urls[i], url) + require.Equal(t, urls[i], stream.ParsedUrl) } }