From ff01066442c72feaf8e42bf807ca38a782a1fc20 Mon Sep 17 00:00:00 2001 From: Artur Date: Sun, 3 Apr 2022 15:28:42 +0300 Subject: [PATCH] add test for getFeedCtrl --- app/api/server_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 app/api/server_test.go diff --git a/app/api/server_test.go b/app/api/server_test.go new file mode 100644 index 00000000..569fec34 --- /dev/null +++ b/app/api/server_test.go @@ -0,0 +1,79 @@ +package api + +import ( + "encoding/xml" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/umputun/feed-master/app/config" + "github.com/umputun/feed-master/app/feed" + "github.com/umputun/feed-master/app/proc" + bolt "go.etcd.io/bbolt" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func TestGetFeedCtrl(t *testing.T) { + tmpfile := filepath.Join(os.TempDir(), "test.db") + defer os.Remove(tmpfile) + testDB, err := bolt.Open(tmpfile, 0o600, nil) + require.NoError(t, err) + boltStore := &proc.BoltDB{DB: testDB} + + conf := &config.Conf{} + conf.System.BaseURL = "http://localhost:8080" + conf.System.MaxTotal = 3 + conf.YouTube.BaseURL = "" + + srv := Server{ + Store: boltStore, + Conf: *conf, + } + ts := httptest.NewServer(srv.router()) + defer ts.Close() + + item := feed.Item{ + Title: "test", + Link: "http://test.com", + Description: "Description", + GUID: "GUID123", + Duration: "123", + PubDate: "Mon, 02 Jan 2006 15:04:05 -0700", + } + created, err := srv.Store.Save("test", item) + require.NoError(t, err) + assert.True(t, created) + + rr, err := ts.Client().Get(ts.URL + "/rss/test") + require.NoError(t, err) + defer rr.Body.Close() + + body := make([]byte, rr.ContentLength) + n, err := rr.Body.Read(body) + require.EqualError(t, err, "EOF") + require.Equal(t, rr.ContentLength, int64(n)) + + // RssU struct for unmarshalling image and thumbnail tags + type RssU struct { + XMLName xml.Name `xml:"rss"` + ItunesImage feed.ItunesImg `xml:"channel>image"` + MediaThumbnail feed.MediaThumbnail `xml:"channel>thumbnail"` + Link string `xml:"channel>link"` + + ItemList []feed.Item `xml:"channel>item"` + } + + expectedFeed := RssU{ + XMLName: xml.Name{Local: "rss"}, + ItunesImage: feed.ItunesImg{URL: "http://localhost:8080/images/test"}, + MediaThumbnail: feed.MediaThumbnail{URL: "http://localhost:8080/images/test"}, + Link: "http://localhost:8080/feed/test", + } + expectedFeed.ItemList = append(expectedFeed.ItemList, item) + + actualFeed := RssU{} + err = xml.Unmarshal(body, &actualFeed) + assert.NoError(t, err) + assert.Equal(t, expectedFeed, actualFeed) +}