@@ -2,6 +2,7 @@ package handlers
22
33import (
44 "encoding/json"
5+ "fmt"
56 "net/http"
67 "net/http/httptest"
78 "strings"
@@ -13,9 +14,10 @@ import (
1314 "github.com/Silo-Server/silo-server/internal/playback"
1415)
1516
16- // A remote bitrate cap that the requested 4K version cannot meet must still
17- // let the start fall back to a version that fits the cap.
18- func TestHandleStartPlaybackV3RemoteBitrateCapTriesCompliantAlternate (t * testing.T ) {
17+ // A bitrate cap that the requested 4K version cannot meet must still let the
18+ // start fall back to a version that fits the cap. The request's location
19+ // selects the local or remote cap.
20+ func TestHandleStartPlaybackV3BitrateCapTriesCompliantAlternate (t * testing.T ) {
1921 source := v3HandlerFixtureFile (t )
2022 source .Resolution = "2160p"
2123 source .Bitrate = 32_000
@@ -40,19 +42,35 @@ func TestHandleStartPlaybackV3RemoteBitrateCapTriesCompliantAlternate(t *testing
4042 handler .PlaybackConfig = playbackTestConfig ("" , "" )
4143 handler .ItemAccess = allowAllPlaybackItemAccess {}
4244
43- start := v3HandlerStartRequest ()
44- start .QualityPreference = "auto"
45- start .ClientPlaybackContext .Deliveries [playback .DeliveryClassHLSV3 ] = playback.DeliveryCapabilityV3 {Enabled : true , SupportedOnDevice : true }
46- ctx := access .SetScope (newAuthorizedPlaybackContext (), access.Scope {UserID : 1 , ProfileID : "profile-1" , MaxRemoteStreamBitrateKbps : 10_000 })
47- ctx = clientip .SetContext (ctx , "203.0.113.7" )
48- rr := httptest .NewRecorder ()
49- handler .HandleStartPlayback (rr , httptest .NewRequest (http .MethodPost , "/api/v1/playback/start" , strings .NewReader (marshalV3StartRequest (t , start ))).WithContext (ctx ))
50-
51- var response playback.DecisionResponseV3
52- if rr .Code != http .StatusCreated || json .Unmarshal (rr .Body .Bytes (), & response ) != nil {
53- t .Fatalf ("status=%d body=%s" , rr .Code , rr .Body .String ())
45+ // The other location's cap is too low for any stream, so applying it
46+ // instead would refuse playback.
47+ tests := []struct {
48+ name string
49+ clientIP string
50+ scope access.Scope
51+ }{
52+ {name : "remote" , clientIP : "203.0.113.7" , scope : access.Scope {MaxRemoteStreamBitrateKbps : 10_000 , MaxLocalStreamBitrateKbps : 100 }},
53+ {name : "local" , clientIP : "192.168.1.20" , scope : access.Scope {MaxLocalStreamBitrateKbps : 10_000 , MaxRemoteStreamBitrateKbps : 100 }},
5454 }
55- if response .PlaybackPlan == nil || response .PlaybackPlan .EffectiveMediaFileID != alternate .ID {
56- t .Fatalf ("expected the compliant 1080p alternate, got terminal=%#v plan=%#v" , response .Terminal , response .PlaybackPlan )
55+ for i , test := range tests {
56+ t .Run (test .name , func (t * testing.T ) {
57+ start := v3HandlerStartRequest ()
58+ start .PlaybackAttemptID = fmt .Sprintf ("bitrate-cap-attempt-%d" , i )
59+ start .QualityPreference = "auto"
60+ start .ClientPlaybackContext .Deliveries [playback .DeliveryClassHLSV3 ] = playback.DeliveryCapabilityV3 {Enabled : true , SupportedOnDevice : true }
61+ scope := test .scope
62+ scope .UserID , scope .ProfileID = 1 , "profile-1"
63+ ctx := clientip .SetContext (access .SetScope (newAuthorizedPlaybackContext (), scope ), test .clientIP )
64+ rr := httptest .NewRecorder ()
65+ handler .HandleStartPlayback (rr , httptest .NewRequest (http .MethodPost , "/api/v1/playback/start" , strings .NewReader (marshalV3StartRequest (t , start ))).WithContext (ctx ))
66+
67+ var response playback.DecisionResponseV3
68+ if rr .Code != http .StatusCreated || json .Unmarshal (rr .Body .Bytes (), & response ) != nil {
69+ t .Fatalf ("status=%d body=%s" , rr .Code , rr .Body .String ())
70+ }
71+ if response .PlaybackPlan == nil || response .PlaybackPlan .EffectiveMediaFileID != alternate .ID {
72+ t .Fatalf ("expected the compliant 1080p alternate, got terminal=%#v plan=%#v" , response .Terminal , response .PlaybackPlan )
73+ }
74+ })
5775 }
5876}
0 commit comments