-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprovider_test.go
147 lines (138 loc) · 3.46 KB
/
provider_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
package proxytv
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func createTempFile(content string, pattern string) (*os.File, error) {
tmpFile, err := os.CreateTemp("", pattern)
if err != nil {
return nil, err
}
_, err = tmpFile.WriteString(content)
if err != nil {
tmpFile.Close()
return nil, err
}
tmpFile.Close()
return tmpFile, nil
}
func TestProviderLoad(t *testing.T) {
tests := []struct {
name string
config *Config
m3uContent string
expectedM3u string
epgContent string
wantErr bool
}{
{
name: "Valid M3U content",
config: &Config{
Filters: []*Filter{
{Type: "id", Value: ".*"},
},
},
m3uContent: `#EXTM3U
#EXTINF:-1 tvg-id="id1" tvg-name="name1",Channel 1
http://example.com/channel1
#EXTINF:-1 tvg-id="id2" tvg-name="name2",Channel 2
http://example.com/channel2`,
expectedM3u: `#EXTM3U
#EXTINF:-1 tvg-id="id1" tvg-name="name1",Channel 1
http://example.com/channel1
#EXTINF:-1 tvg-id="id2" tvg-name="name2",Channel 2
http://example.com/channel2
`,
epgContent: `<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
`,
wantErr: false,
},
{
name: "Invalid M3U content",
config: &Config{
Filters: []*Filter{
{Type: "id", Value: ".*"},
},
},
m3uContent: "Invalid content",
wantErr: true,
},
{
name: "Filtered valid M3U content",
config: &Config{
Filters: []*Filter{
{Type: "id", Value: "id2"},
},
},
m3uContent: `#EXTM3U
#EXTINF:-1 tvg-id="id1" tvg-name="name1",Channel 1
http://example.com/channel1
#EXTINF:-1 tvg-id="id2" tvg-name="name2",Channel 2
http://example.com/channel2`,
expectedM3u: `#EXTM3U
#EXTINF:-1 tvg-id="id2" tvg-name="name2",Channel 2
http://example.com/channel2
`,
epgContent: `<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
`,
wantErr: false,
},
{
name: "Filtered valid M3U content",
config: &Config{
Filters: []*Filter{
{Type: "id", Value: ".*"},
},
UseFFMPEG: true,
ServerAddress: "test.com:6078",
},
m3uContent: `#EXTM3U
#EXTINF:-1 tvg-id="id1" tvg-name="name1",Channel 1
http://example.com/channel1
#EXTINF:-1 tvg-id="id2" tvg-name="name2",Channel 2
http://example.com/channel2`,
expectedM3u: `#EXTM3U
#EXTINF:-1 tvg-id="id1" tvg-name="name1",Channel 1
http://test.com:6078/channel/0
#EXTINF:-1 tvg-id="id2" tvg-name="name2",Channel 2
http://test.com:6078/channel/1
`,
epgContent: `<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
`,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary file for m3u content
tmpFile, err := createTempFile(tt.m3uContent, "test_m3u_*.m3u")
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}
defer os.Remove(tmpFile.Name())
// Set the IPTVUrl to the temporary file path
tt.config.IPTVUrl = filepath.ToSlash(tmpFile.Name())
tmpFile, err = createTempFile(tt.epgContent, "test_epg_*.xml")
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}
defer os.Remove(tmpFile.Name())
tt.config.EPGUrl = filepath.ToSlash(tmpFile.Name())
tt.config.compileFilterRegexps()
provider, err := NewProvider(tt.config)
assert.NoError(t, err)
err = provider.Refresh()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedM3u, provider.GetM3u())
}
})
}
}