forked from tellytv/go.xtream-codes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxtream-codes_test.go
216 lines (175 loc) · 5.6 KB
/
xtream-codes_test.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package xtreamcodes
// There's a 99% chance that this is a terrible way to write tests but I just needed a quick way to validate the JSON schemas.
// Apologies in advance!
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/onsi/gomega"
)
var expectedContents = make(map[string]map[string][]byte)
var types = []string{"live", "vod", "series"}
var tsURL string
func walkFunc(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() || f.Name() == "testData" {
return nil
}
bytes, err := ioutil.ReadFile(path) // path is the path to the file.
if err != nil {
return err
}
splitPath := strings.Split(path, "/")
if expectedContents[f.Name()] == nil {
expectedContents[f.Name()] = make(map[string][]byte)
}
expectedContents[f.Name()][splitPath[1]] = bytes
return nil
}
func TestMain(m *testing.M) {
if err := filepath.Walk("testData", walkFunc); err != nil {
panic(err)
}
serv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
qs := r.URL.Query()
provider := strings.ToLower(strings.Replace(qs.Get("username"), "_USERNAME", "", -1))
action := fmt.Sprintf("%s.json", qs.Get("action"))
if action == ".json" {
action = "auth.json"
}
// fmt.Printf("httptest: Serving request for provider %s action %s\n", provider, action)
w.Write(expectedContents[action][provider])
}))
defer serv.Close()
tsURL = serv.URL
os.Exit(m.Run())
}
func TestNewClient(t *testing.T) {
for providerName, expectedContent := range expectedContents["auth.json"] {
t.Run(providerName, func(t *testing.T) {
t.Parallel()
xc := getClient(t, providerName)
t.Logf("%s client successfully created!", providerName)
marshalled, marshalErr := json.Marshal(AuthenticationResponse{
UserInfo: xc.UserInfo,
ServerInfo: xc.ServerInfo,
})
if marshalErr != nil {
t.Errorf("error marshalling auth response back to json: %s", marshalErr)
t.Fail()
return
}
g := gomega.NewGomegaWithT(t)
g.Expect(string(expectedContent)).To(gomega.MatchJSON(string(marshalled)))
})
}
}
func TestGetCategories(t *testing.T) {
for _, xType := range types {
t.Run(xType, func(t *testing.T) {
t.Parallel()
filePath := fmt.Sprintf("get_%s_categories.json", xType)
for providerName, expectedContent := range expectedContents[filePath] {
t.Run(providerName, func(t *testing.T) {
t.Parallel()
xc := getClient(t, providerName)
cats, catsErr := xc.GetCategories(xType)
if catsErr != nil {
t.Errorf("error getting %s categories: %s", xType, catsErr)
t.Fail()
return
}
marshalled, marshalErr := json.Marshal(cats)
if marshalErr != nil {
t.Errorf("error marshalling %s response back to json: %s", xType, marshalErr)
t.Fail()
return
}
g := gomega.NewGomegaWithT(t)
g.Expect(string(expectedContent)).To(gomega.MatchJSON(string(marshalled)))
t.Logf("Got %s categories from %s successfully!", xType, providerName)
})
}
})
}
}
func TestGetStreams(t *testing.T) {
for _, xType := range types {
t.Run(xType, func(t *testing.T) {
if xType == "series" {
return
}
t.Parallel()
filePath := fmt.Sprintf("get_%s_streams.json", xType)
for providerName, expectedContent := range expectedContents[filePath] {
t.Run(providerName, func(t *testing.T) {
t.Parallel()
xc := getClient(t, providerName)
streams, streamsErr := xc.GetStreams(xType, "")
if streamsErr != nil {
t.Errorf("error getting %s streams: %s", xType, streamsErr)
t.Fail()
return
}
t.Logf("Calling json.Marshal for %s streams from %s", xType, providerName)
marshalled, marshalErr := json.Marshal(streams)
if marshalErr != nil {
t.Errorf("error marshalling %s response back to json: %s", xType, marshalErr)
t.Fail()
return
}
t.Logf("Calling assert.JSONEq for %s streams from %s", xType, providerName)
g := gomega.NewGomegaWithT(t)
g.Expect(string(expectedContent)).To(gomega.MatchJSON(string(marshalled)))
t.Logf("Got %s streams from %s successfully!", xType, providerName)
})
}
})
}
}
// func TestGetSeries(t *testing.T) {
// for providerName, expectedContent := range expectedContents["get_series.json"] {
// t.Run(providerName, func(t *testing.T) {
// t.Parallel()
// xc := getClient(t, providerName)
// streams, streamsErr := xc.GetSeries("")
// if streamsErr != nil {
// t.Errorf("error getting series: %s", streamsErr)
// t.Fail()
// return
// }
// t.Logf("Calling json.Marshal for series from %s", providerName)
// marshalled, marshalErr := json.Marshal(streams)
// if marshalErr != nil {
// t.Errorf("error marshalling series response back to json: %s", marshalErr)
// t.Fail()
// return
// }
// t.Logf("Calling assert.JSONEq for series from %s", providerName)
// t.Logf("%s\n\n\n%s", string(expectedContent), string(marshalled))
// g := gomega.NewGomegaWithT(t)
// g.Expect(string(expectedContent)).To(gomega.MatchJSON(string(marshalled)))
// t.Logf("Got series from %s successfully!", providerName)
// })
// }
// }
func getClient(t *testing.T, providerName string) *XtreamClient {
t.Helper()
xc, xcErr := NewClient(fmt.Sprintf("%s_USERNAME", strings.ToUpper(providerName)), fmt.Sprintf("%s_PASSWORD", strings.ToUpper(providerName)), tsURL)
if xcErr != nil {
t.Errorf("NewClient() returned an error: %s", xcErr)
t.Fail()
return nil
}
return xc
}