pull/54/head
178inaba 2017-05-04 23:35:05 +09:00
parent eacd4e066d
commit 5fad354d1a
3 changed files with 142 additions and 1 deletions

View File

@ -113,7 +113,6 @@ func TestGetAccountStatuses(t *testing.T) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
//w.Header().Set("Link", `<http://example.com/api/v1/accounts/1234567/statuses?max_id=123>; rel="next", <http://example.com/api/v1/accounts/1234567/statuses?since_id=789>; rel="prev"`)
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
return
}))

View File

@ -62,6 +62,14 @@ func TestBase64Encode(t *testing.T) {
}
}
func TestInt64(t *testing.T) {
i := int64(1234567)
ip := Int64(i)
if *ip != i {
t.Fatalf("want %d but %d", i, *ip)
}
}
func TestString(t *testing.T) {
s := "test"
sp := String(s)

View File

@ -6,10 +6,74 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
func TestDoAPI(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if q.Get("max_id") == "123" && q.Get("since_id") == "789" && q.Get("limit") == "10" {
w.Header().Set("Link", `<http://example.com?max_id=234>; rel="next", <http://example.com?since_id=890>; rel="prev"`)
fmt.Fprintln(w, `[{"username": "foo"}, {"username": "bar"}]`)
}
w.Header().Set("Link", `<:>; rel="next"`)
}))
defer ts.Close()
c := NewClient(&Config{Server: ts.URL})
_, err := c.doAPI(context.Background(), http.MethodGet, "/", nil, nil, &Pagination{
MaxID: Int64(999),
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
var accounts []Account
pg, err := c.doAPI(context.Background(), http.MethodGet, "/", url.Values{}, &accounts, &Pagination{
MaxID: Int64(123),
SinceID: Int64(789),
Limit: Int64(10),
})
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if *pg.MaxID != 234 {
t.Fatalf("want %d but %d", 234, *pg.MaxID)
}
if *pg.SinceID != 890 {
t.Fatalf("want %d but %d", 890, *pg.SinceID)
}
if accounts[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", accounts[0].Username)
}
if accounts[1].Username != "bar" {
t.Fatalf("want %q but %q", "bar", accounts[1].Username)
}
pg, err = c.doAPI(context.Background(), http.MethodGet, "/", nil, &accounts, &Pagination{
MaxID: Int64(123),
SinceID: Int64(789),
Limit: Int64(10),
})
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if *pg.MaxID != 234 {
t.Fatalf("want %d but %d", 234, *pg.MaxID)
}
if *pg.SinceID != 890 {
t.Fatalf("want %d but %d", 890, *pg.SinceID)
}
if accounts[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", accounts[0].Username)
}
if accounts[1].Username != "bar" {
t.Fatalf("want %q but %q", "bar", accounts[1].Username)
}
}
func TestAuthenticate(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("username") != "valid" || r.FormValue("password") != "user" {
@ -198,3 +262,73 @@ func TestForTheCoverages(t *testing.T) {
(*ErrorEvent)(nil).event()
_ = (&ErrorEvent{io.EOF}).Error()
}
func TestNewPagination(t *testing.T) {
_, err := newPagination("")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
_, err = newPagination(`<:>; rel="next"`)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
_, err = newPagination(`<:>; rel="prev"`)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
pg, err := newPagination(`<http://example.com?max_id=123>; rel="next", <http://example.com?since_id=789>; rel="prev"`)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if *pg.MaxID != 123 {
t.Fatalf("want %d but %d", 123, *pg.MaxID)
}
if *pg.SinceID != 789 {
t.Fatalf("want %d but %d", 789, *pg.SinceID)
}
}
func TestGetPaginationID(t *testing.T) {
_, err := getPaginationID(":", "max_id")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
_, err = getPaginationID("http://example.com?max_id=abc", "max_id")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
id, err := getPaginationID("http://example.com?max_id=123", "max_id")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if id != 123 {
t.Fatalf("want %d but %d", 123, id)
}
}
func TestPaginationSetValues(t *testing.T) {
p := &Pagination{
MaxID: Int64(123),
SinceID: Int64(789),
Limit: Int64(10),
}
before := url.Values{"key": {"value"}}
after := p.setValues(before)
if after.Get("key") != "value" {
t.Fatalf("want %q but %q", "value", after.Get("key"))
}
if after.Get("max_id") != "123" {
t.Fatalf("want %q but %q", "123", after.Get("max_id"))
}
if after.Get("since_id") != "789" {
t.Fatalf("want %q but %q", "789", after.Get("since_id"))
}
if after.Get("limit") != "10" {
t.Fatalf("want %q but %q", "10", after.Get("limit"))
}
}