Skip to content

Commit

Permalink
add test for getFeedCtrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur committed Apr 3, 2022
1 parent 003e210 commit ff01066
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions app/api/server_test.go
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)
}

0 comments on commit ff01066

Please sign in to comment.