From da8c4ac02022befd234249b642d0b5a629028ba1 Mon Sep 17 00:00:00 2001 From: sentriz Date: Sat, 6 Jun 2020 16:24:27 +0100 Subject: [PATCH] clean up ctrl_test --- server/ctrlsubsonic/ctrl_test.go | 38 ++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/server/ctrlsubsonic/ctrl_test.go b/server/ctrlsubsonic/ctrl_test.go index ed0189d..eaef9d6 100644 --- a/server/ctrlsubsonic/ctrl_test.go +++ b/server/ctrlsubsonic/ctrl_test.go @@ -32,31 +32,41 @@ type queryCase struct { 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) { for _, qc := range cases { qc := qc // pin t.Run(qc.expectPath, func(t *testing.T) { t.Parallel() - // ensure the handlers give us json - 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() + rr, req := makeHTTPMock(qc.params) testController.H(h).ServeHTTP(rr, req) body := rr.Body.String() if status := rr.Code; status != http.StatusOK { t.Fatalf("didn't give a 200\n%s", body) } - // convert test name to query case path - snake := testCamelExpr.ReplaceAllString(t.Name(), "${1}_${2}") - lower := strings.ToLower(snake) - relPath := strings.ReplaceAll(lower, "/", "_") - absExpPath := path.Join(testDataDir, relPath) + goldenPath := makeGoldenPath(t.Name()) // read case to differ with handler result - expected, err := jd.ReadJsonFile(absExpPath) + expected, err := jd.ReadJsonFile(goldenPath) if err != nil { t.Fatalf("parsing expected: %v", err) }