package service_test import ( "io" "net/http" "net/http/httptest" "testing" "time" "github.com/dubyte/dir2opds/internal/service" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestHandler(t *testing.T) { // pre-setup nowFn := service.TimeNowFunc defer func() { service.TimeNowFunc = nowFn }() tests := map[string]struct { input string want string WantedContentType string }{ "feed (dir of folders )": {input: "/", want: feed, WantedContentType: "application/xml"}, "acquisitionFeed(dir of files)": {input: "/mybook", want: acquisitionFeed, WantedContentType: "application/xml"}, "servingAFile": {input: "/mybook/mybook.txt", want: "Fixture", WantedContentType: "text/plain; charset=utf-8"}, } for name, tc := range tests { t.Run(name, func(t *testing.T) { // setup s := service.OPDS{"testdata", "", "", ""} w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, tc.input, nil) service.TimeNowFunc = func() time.Time { return time.Date(2020, 05, 25, 00, 00, 00, 0, time.UTC) } // act err := s.Handler(w, req) require.NoError(t, err) // post act resp := w.Result() body, err := io.ReadAll(resp.Body) require.NoError(t, err) // verify assert.Equal(t, 200, resp.StatusCode) assert.Equal(t, tc.WantedContentType, resp.Header.Get("Content-Type")) assert.Equal(t, tc.want, string(body)) }) } } var feed = ` Catalog in / / 2020-05-25T00:00:00+00:00 emptyFolder /emptyFolder 2020-05-25T00:00:00+00:00 2020-05-25T00:00:00+00:00 mybook /mybook 2020-05-25T00:00:00+00:00 2020-05-25T00:00:00+00:00 ` var acquisitionFeed = ` Catalog in /mybook /mybook 2020-05-25T00:00:00+00:00 mybook.epub /mybookmybook.epub 2020-05-25T00:00:00+00:00 2020-05-25T00:00:00+00:00 mybook.pdf /mybookmybook.pdf 2020-05-25T00:00:00+00:00 2020-05-25T00:00:00+00:00 mybook.txt /mybookmybook.txt 2020-05-25T00:00:00+00:00 2020-05-25T00:00:00+00:00 `