- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Artur
committed
Apr 3, 2022
1 parent
003e210
commit ff01066
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |