-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream_test.go
More file actions
125 lines (117 loc) · 3.51 KB
/
stream_test.go
File metadata and controls
125 lines (117 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package scdl
import (
"context"
"fmt"
"io"
"net/http"
"strings"
"testing"
)
func TestParseHLSURL(t *testing.T) {
tests := []struct {
name string
url string
wantTrackID string
wantStreamToken string
wantErr bool
}{
{
name: "Valid URL",
url: "https://api-v2.soundcloud.com/media/soundcloud:tracks:123456/ab-cd-ef/stream/hls",
wantTrackID: "123456",
wantStreamToken: "ab-cd-ef",
wantErr: false,
},
{
name: "Valid URL with query params",
url: "https://api-v2.soundcloud.com/media/soundcloud:tracks:987654/xyz/stream/hls?foo=bar",
wantTrackID: "987654",
wantStreamToken: "xyz",
wantErr: false,
},
{
name: "Invalid URL - too short",
url: "https://api-v2.soundcloud.com/media",
wantErr: true,
},
{
name: "Invalid URL - missing track ID",
url: "https://api-v2.soundcloud.com/media/something/ab-cd-ef/stream/hls",
wantErr: true,
},
{
name: "Invalid URL - bad track format",
url: "https://api-v2.soundcloud.com/media/soundcloud:tracks/ab-cd-ef/stream/hls",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotTrackID, gotStreamToken, err := parseHLSURL(tt.url)
if (err != nil) != tt.wantErr {
t.Errorf("parseHLSURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if gotTrackID != tt.wantTrackID {
t.Errorf("parseHLSURL() gotTrackID = %v, want %v", gotTrackID, tt.wantTrackID)
}
if gotStreamToken != tt.wantStreamToken {
t.Errorf("parseHLSURL() gotStreamToken = %v, want %v", gotStreamToken, tt.wantStreamToken)
}
}
})
}
}
func TestGetStreamURL_Errors(t *testing.T) {
client := &Client{
clientID: "test",
httpClient: &http.Client{
Transport: &mockTransport{
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
u := req.URL.String()
if strings.Contains(u, "fail") {
return nil, fmt.Errorf("fail")
}
if strings.Contains(u, "empty") {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"url": ""}`))}, nil
}
if strings.Contains(u, "badjson") {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{invalid`))}, nil
}
return &http.Response{StatusCode: 404, Body: io.NopCloser(strings.NewReader("404"))}, nil
},
},
},
}
t.Run("ParseFail", func(t *testing.T) {
_, err := client.GetStreamURL(context.Background(), &Track{HLSURL: "invalid"})
if err == nil {
t.Error("expected error")
}
})
t.Run("FetchFail", func(t *testing.T) {
_, err := client.GetStreamURL(context.Background(), &Track{HLSURL: "http://api/soundcloud:tracks:123/fail/stream/hls"})
if err == nil {
t.Error("expected error")
}
})
t.Run("EmptyURL", func(t *testing.T) {
_, err := client.GetStreamURL(context.Background(), &Track{HLSURL: "http://api/soundcloud:tracks:123/empty/stream/hls"})
if err == nil || !strings.Contains(err.Error(), "empty stream URL") {
t.Errorf("unexpected error: %v", err)
}
})
t.Run("BadJSON", func(t *testing.T) {
_, err := client.GetStreamURL(context.Background(), &Track{HLSURL: "http://api/soundcloud:tracks:123/badjson/stream/hls"})
if err == nil {
t.Error("expected error")
}
})
t.Run("BadTrackID", func(t *testing.T) {
_, _, err := parseHLSURL("https://api-v2.soundcloud.com/media/soundcloud:tracks/ab-cd-ef/stream/hls")
if err == nil {
t.Error("expected error")
}
})
}