aboutsummaryrefslogtreecommitdiff
path: root/main_test.go
diff options
context:
space:
mode:
authorEvgeny Kuznetsov <evgeny@kuznetsov.md>2020-01-28 14:23:18 +0300
committerEvgeny Kuznetsov <evgeny@kuznetsov.md>2020-01-28 14:31:34 +0300
commit6ed6988602811b9af5d524151721fd7db90834ef (patch)
tree5ce7017cf86daf4327be4dadc2ca364caf9f83b5 /main_test.go
parentc5144080d434bbfd316f7fef2cbf27fbf2d37770 (diff)
downloadradiorus-rss-6ed6988602811b9af5d524151721fd7db90834ef.tar.gz
radiorus-rss-6ed6988602811b9af5d524151721fd7db90834ef.zip
mock server for testing
Diffstat (limited to 'main_test.go')
-rw-r--r--main_test.go67
1 files changed, 63 insertions, 4 deletions
diff --git a/main_test.go b/main_test.go
index 4a5e53f..819f9d0 100644
--- a/main_test.go
+++ b/main_test.go
@@ -18,14 +18,24 @@ package main
import (
"bytes"
"flag"
+ "fmt"
"io/ioutil"
+ "net/http"
+ "net/http/httptest"
+ "os"
"path/filepath"
+ "sync"
"testing"
"github.com/gorilla/feeds"
)
-var update = flag.Bool("update", false, "update .golden files")
+var (
+ update = flag.Bool("update", false, "update .golden files")
+ fakeURL = `**localhost**`
+)
+
+const pth = "testdata/brand/57083"
func helperLoadBytes(t *testing.T, name string) []byte {
t.Helper()
@@ -56,9 +66,30 @@ func TestFeed(t *testing.T) {
actual := createFeed(feed)
golden := filepath.Join("testdata", t.Name()+".golden")
if *update {
- if err := ioutil.WriteFile(golden, actual, 0644); err != nil {
- t.Fatal(err)
- }
+ writeFile(actual, golden)
+ }
+ expected, _ := ioutil.ReadFile(golden)
+
+ if !bytes.Equal(actual, expected) {
+ t.Fail()
+ }
+}
+
+func TestServedFeed(t *testing.T) {
+ server := helperMockServer(t)
+ defer helperCleanupServer(t)
+
+ feed := getFeed(fmt.Sprintf("%s/brand/57083/episodes", server.URL))
+
+ var wg sync.WaitGroup
+ wg.Add(1)
+ describeFeed(feed, &wg)
+ wg.Wait()
+
+ actual := bytes.ReplaceAll(createFeed(feed), []byte(server.URL), []byte(fakeURL))
+ golden := filepath.Join("testdata", t.Name()+".golden")
+ if *update {
+ writeFile(actual, golden)
}
expected, _ := ioutil.ReadFile(golden)
@@ -66,3 +97,31 @@ func TestFeed(t *testing.T) {
t.Fail()
}
}
+
+func helperMockServer(t *testing.T) *httptest.Server {
+ t.Helper()
+
+ fileserver := http.FileServer(http.Dir("testdata"))
+ server := httptest.NewServer(fileserver)
+
+ episodes := helperLoadBytes(t, "episodes")
+ writeFile(episodes, filepath.Join(pth, "episodes"))
+
+ about := helperLoadBytes(t, "about")
+ writeFile(about, filepath.Join(pth, "about"))
+
+ return server
+}
+
+func helperCleanupServer(t *testing.T) {
+ t.Helper()
+ helperCleanupFile(t, "episodes")
+ helperCleanupFile(t, "about")
+}
+
+func helperCleanupFile(t *testing.T, name string) {
+ t.Helper()
+ if err := os.Remove(filepath.Join(pth, name)); err != nil {
+ t.Fatal(err)
+ }
+}