154 lines
4.1 KiB
Go
Executable File
154 lines
4.1 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_Core(t *testing.T) {
|
|
server := httptest.NewUnstartedServer(nil)
|
|
server.Close()
|
|
|
|
var rssserverURL *string
|
|
rssserver := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/feed" {
|
|
fmt.Fprintln(w,
|
|
`
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
<rss version="2.0"><channel>
|
|
<title>fake.com</title>
|
|
<link>`+*rssserverURL+`/feed</link>
|
|
<description>this is fake</description>
|
|
<item>
|
|
<title>title A</title>
|
|
<link>`+*rssserverURL+`/itemA</link>
|
|
<description></description>
|
|
<pubDate>Wed, 10 Oct 2018 04:00:00 -0000</pubDate>
|
|
</item>
|
|
<item>
|
|
<title>title B</title>
|
|
<link>`+*rssserverURL+`/itemB</link>
|
|
<description>content B</description>
|
|
<pubDate>Wed, 10 Oct 2018 04:00:00 -0000</pubDate>
|
|
</item>
|
|
</channel></rss>
|
|
`,
|
|
)
|
|
} else {
|
|
fmt.Fprintln(w,
|
|
`
|
|
body of an item here 12345 <img src="fake/path"></img>
|
|
`,
|
|
)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
rssserverURL = &rssserver.URL
|
|
|
|
wasDBPath := os.Getenv("DBPATH")
|
|
wasPort := os.Getenv("PORT")
|
|
defer os.Setenv("DBPATH", wasDBPath)
|
|
defer os.Setenv("PORT", wasPort)
|
|
tmpf, err := ioutil.TempFile("", "testdb.db")
|
|
if err != nil {
|
|
t.Fatalf("cannot create temp db file: %v", err)
|
|
}
|
|
defer os.Remove(tmpf.Name())
|
|
os.Setenv("DBPATH", tmpf.Name())
|
|
os.Setenv("PORT", ":"+strings.Split(server.Listener.Addr().String(), ":")[1])
|
|
go core()
|
|
time.Sleep(time.Second * 5)
|
|
|
|
client := &http.Client{
|
|
Timeout: time.Second * 5,
|
|
}
|
|
testhost := "http://localhost" + os.Getenv("PORT")
|
|
|
|
cases := []struct {
|
|
method string
|
|
path string
|
|
body string
|
|
status int
|
|
pre func()
|
|
post func()
|
|
}{
|
|
{
|
|
method: "post",
|
|
path: "api/feed",
|
|
body: `{"url":"` + rssserver.URL + `/feed", "refresh":"30m", "items":"[AB]", "content":"2", "tags": ["gotest"]}`,
|
|
status: 200,
|
|
post: func() { time.Sleep(time.Second * 10) },
|
|
},
|
|
{
|
|
method: "get",
|
|
path: "api/feed/" + rssserver.URL + "/feed",
|
|
status: 200,
|
|
},
|
|
{
|
|
method: "get",
|
|
path: "api/feed/item/http___127_0_0_1_" + strings.Split(rssserver.URL, ":")[2] + "_feed.20181010_http___127_0_0_1_" + strings.Split(rssserver.URL, ":")[2] + "_itemB",
|
|
status: 200,
|
|
},
|
|
{
|
|
method: "get",
|
|
path: "api/feed/tag/gotest",
|
|
status: 200,
|
|
},
|
|
{
|
|
method: "post",
|
|
path: "api/feed",
|
|
body: `{"url":"` + rssserver.URL + `/feed", "refresh":"10s", "items":"[AB]", "content":"1"}`,
|
|
status: 200,
|
|
post: func() { time.Sleep(time.Second * 15) },
|
|
},
|
|
{
|
|
method: "get",
|
|
path: "api/list/tag/gotest",
|
|
status: 200,
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
c.method = strings.ToUpper(c.method)
|
|
if c.pre != nil {
|
|
c.pre()
|
|
}
|
|
loc, err := url.Parse(testhost + "/" + c.path)
|
|
if err != nil {
|
|
t.Errorf("cannot create loc %s+%s: %v", testhost, c.path, err)
|
|
}
|
|
var body io.Reader = nil
|
|
if c.method == "GET" && c.body != "" {
|
|
v := url.Values{}
|
|
v.Add("url", c.body)
|
|
loc.RawQuery = v.Encode()
|
|
} else if c.body != "" {
|
|
body = bytes.NewBuffer([]byte(c.body))
|
|
}
|
|
req, err := http.NewRequest(c.method, loc.String(), body)
|
|
if err != nil {
|
|
t.Errorf("cannot create request %s:%s: %v", c.method, loc.String(), err)
|
|
}
|
|
if resp, err := client.Do(req); err != nil {
|
|
t.Errorf("cannot %s to %s: %v", c.method, loc.String(), err)
|
|
} else if resp.StatusCode != c.status {
|
|
t.Errorf("wrong %s status to %s: %v, expected %v", c.method, loc.String(), resp.StatusCode, c.status)
|
|
} else if b, err := ioutil.ReadAll(resp.Body); err != nil {
|
|
t.Errorf("cannot read body on %s to %s: %v", c.method, loc.String(), err)
|
|
} else {
|
|
t.Logf("%s resp body: %s", loc.String(), b)
|
|
}
|
|
if c.post != nil {
|
|
c.post()
|
|
}
|
|
}
|
|
}
|