clean up ctrl_test
This commit is contained in:
@@ -32,31 +32,41 @@ type queryCase struct {
|
|||||||
listSet bool
|
listSet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeGoldenPath(test string) string {
|
||||||
|
// convert test name to query case path
|
||||||
|
snake := testCamelExpr.ReplaceAllString(test, "${1}_${2}")
|
||||||
|
lower := strings.ToLower(snake)
|
||||||
|
relPath := strings.ReplaceAll(lower, "/", "_")
|
||||||
|
return path.Join(testDataDir, relPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeHTTPMock(query url.Values) (*httptest.ResponseRecorder, *http.Request) {
|
||||||
|
// ensure the handlers give us json
|
||||||
|
query.Add("f", "json")
|
||||||
|
// request from the handler in question
|
||||||
|
req, _ := http.NewRequest("", "", nil)
|
||||||
|
req.URL.RawQuery = query.Encode()
|
||||||
|
subParams := params.New(req)
|
||||||
|
withParams := context.WithValue(req.Context(), CtxParams, subParams)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
req = req.WithContext(withParams)
|
||||||
|
return rr, req
|
||||||
|
}
|
||||||
|
|
||||||
func runQueryCases(t *testing.T, h handlerSubsonic, cases []*queryCase) {
|
func runQueryCases(t *testing.T, h handlerSubsonic, cases []*queryCase) {
|
||||||
for _, qc := range cases {
|
for _, qc := range cases {
|
||||||
qc := qc // pin
|
qc := qc // pin
|
||||||
t.Run(qc.expectPath, func(t *testing.T) {
|
t.Run(qc.expectPath, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
// ensure the handlers give us json
|
rr, req := makeHTTPMock(qc.params)
|
||||||
qc.params.Add("f", "json")
|
|
||||||
// request from the handler in question
|
|
||||||
req, _ := http.NewRequest("", "?"+qc.params.Encode(), nil)
|
|
||||||
params := params.New(req)
|
|
||||||
withParams := context.WithValue(req.Context(), CtxParams, params)
|
|
||||||
req = req.WithContext(withParams)
|
|
||||||
rr := httptest.NewRecorder()
|
|
||||||
testController.H(h).ServeHTTP(rr, req)
|
testController.H(h).ServeHTTP(rr, req)
|
||||||
body := rr.Body.String()
|
body := rr.Body.String()
|
||||||
if status := rr.Code; status != http.StatusOK {
|
if status := rr.Code; status != http.StatusOK {
|
||||||
t.Fatalf("didn't give a 200\n%s", body)
|
t.Fatalf("didn't give a 200\n%s", body)
|
||||||
}
|
}
|
||||||
// convert test name to query case path
|
goldenPath := makeGoldenPath(t.Name())
|
||||||
snake := testCamelExpr.ReplaceAllString(t.Name(), "${1}_${2}")
|
|
||||||
lower := strings.ToLower(snake)
|
|
||||||
relPath := strings.ReplaceAll(lower, "/", "_")
|
|
||||||
absExpPath := path.Join(testDataDir, relPath)
|
|
||||||
// read case to differ with handler result
|
// read case to differ with handler result
|
||||||
expected, err := jd.ReadJsonFile(absExpPath)
|
expected, err := jd.ReadJsonFile(goldenPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("parsing expected: %v", err)
|
t.Fatalf("parsing expected: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user