@@ -18,12 +18,22 @@ import (
18
18
19
19
"go.senan.xyz/gonic/server/ctrlbase"
20
20
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
21
+ "go.senan.xyz/gonic/server/db"
21
22
"go.senan.xyz/gonic/server/mockfs"
23
+ "go.senan.xyz/gonic/server/transcode"
22
24
)
23
25
24
- var (
25
- testDataDir = "testdata"
26
- testCamelExpr = regexp .MustCompile ("([a-z0-9])([A-Z])" )
26
+ var testCamelExpr = regexp .MustCompile ("([a-z0-9])([A-Z])" )
27
+
28
+ const (
29
+ mockUsername = "admin"
30
+ mockPassword = "admin"
31
+ mockClientName = "test"
32
+ )
33
+
34
+ const (
35
+ audioPath5s = "testdata/audio/5s.flac" //nolint:deadcode,varcheck
36
+ audioPath10s = "testdata/audio/10s.flac" //nolint:deadcode,varcheck
27
37
)
28
38
29
39
type queryCase struct {
@@ -37,22 +47,43 @@ func makeGoldenPath(test string) string {
37
47
snake := testCamelExpr .ReplaceAllString (test , "${1}_${2}" )
38
48
lower := strings .ToLower (snake )
39
49
relPath := strings .ReplaceAll (lower , "/" , "_" )
40
- return path .Join (testDataDir , relPath )
50
+ return path .Join ("testdata" , relPath )
41
51
}
42
52
43
53
func makeHTTPMock (query url.Values ) (* httptest.ResponseRecorder , * http.Request ) {
44
54
// ensure the handlers give us json
45
55
query .Add ("f" , "json" )
56
+ query .Add ("u" , mockUsername )
57
+ query .Add ("p" , mockPassword )
58
+ query .Add ("v" , "1" )
59
+ query .Add ("c" , mockClientName )
46
60
// request from the handler in question
47
61
req , _ := http .NewRequest ("" , "" , nil )
48
62
req .URL .RawQuery = query .Encode ()
49
- subParams := params .New (req )
50
- withParams := context .WithValue (req .Context (), CtxParams , subParams )
63
+ ctx := req .Context ()
64
+ ctx = context .WithValue (ctx , CtxParams , params .New (req ))
65
+ ctx = context .WithValue (ctx , CtxUser , & db.User {})
66
+ req = req .WithContext (ctx )
51
67
rr := httptest .NewRecorder ()
52
- req = req .WithContext (withParams )
53
68
return rr , req
54
69
}
55
70
71
+ func serveRaw (t * testing.T , contr * Controller , h handlerSubsonicRaw , rr * httptest.ResponseRecorder , req * http.Request ) {
72
+ type middleware func (http.Handler ) http.Handler
73
+ middlewares := []middleware {
74
+ contr .WithParams ,
75
+ contr .WithRequiredParams ,
76
+ contr .WithUser ,
77
+ }
78
+
79
+ handler := contr .HR (h )
80
+ for _ , m := range middlewares {
81
+ handler = m (handler )
82
+ }
83
+
84
+ handler .ServeHTTP (rr , req )
85
+ }
86
+
56
87
func runQueryCases (t * testing.T , contr * Controller , h handlerSubsonic , cases []* queryCase ) {
57
88
t .Helper ()
58
89
for _ , qc := range cases {
@@ -96,28 +127,40 @@ func runQueryCases(t *testing.T, contr *Controller, h handlerSubsonic, cases []*
96
127
}
97
128
}
98
129
99
- func makeController (t * testing.T ) * Controller { return makec (t , []string {"" }) }
100
- func makeControllerRoots (t * testing.T , r []string ) * Controller { return makec (t , r ) }
130
+ func makeController (t * testing.T ) * Controller { return makec (t , []string {"" }, false ) }
131
+ func makeControllerRoots (t * testing.T , r []string ) * Controller { return makec (t , r , false ) }
132
+ func makeControllerAudio (t * testing.T ) * Controller { return makec (t , []string {"" }, true ) }
101
133
102
- func makec (t * testing.T , roots []string ) * Controller {
134
+ func makec (t * testing.T , roots []string , audio bool ) * Controller {
103
135
t .Helper ()
104
136
105
137
m := mockfs .NewWithDirs (t , roots )
106
138
for _ , root := range roots {
107
139
m .AddItemsPrefixWithCovers (root )
140
+ if ! audio {
141
+ continue
142
+ }
143
+ m .SetRealAudio (filepath .Join (root , "artist-0/album-0/track-0.flac" ), 10 , audioPath10s )
144
+ m .SetRealAudio (filepath .Join (root , "artist-0/album-0/track-1.flac" ), 10 , audioPath10s )
145
+ m .SetRealAudio (filepath .Join (root , "artist-0/album-0/track-2.flac" ), 10 , audioPath10s )
108
146
}
109
147
110
148
m .ScanAndClean ()
111
149
m .ResetDates ()
112
- m .LogAlbums ()
113
150
114
151
var absRoots []string
115
152
for _ , root := range roots {
116
153
absRoots = append (absRoots , filepath .Join (m .TmpDir (), root ))
117
154
}
118
155
119
156
base := & ctrlbase.Controller {DB : m .DB ()}
120
- return & Controller {Controller : base , MusicPaths : absRoots }
157
+ contr := & Controller {
158
+ Controller : base ,
159
+ MusicPaths : absRoots ,
160
+ Transcoder : transcode .NewFFmpegTranscoder (),
161
+ }
162
+
163
+ return contr
121
164
}
122
165
123
166
func TestMain (m * testing.M ) {
0 commit comments