Merge pull request #24 from 178inaba/get_query

Accounts fix
pull/26/head
mattn 2017-04-18 09:11:33 +09:00 committed by GitHub
commit 26ca3942bf
3 changed files with 88 additions and 12 deletions

View File

@ -194,12 +194,14 @@ func (c *Client) AccountUnmute(ctx context.Context, id int64) (*Relationship, er
} }
// GetAccountRelationship return relationship for the account. // GetAccountRelationship return relationship for the account.
func (c *Client) GetAccountRelationship(ctx context.Context, id int64) ([]*Relationship, error) { func (c *Client) GetAccountRelationships(ctx context.Context, ids []int64) ([]*Relationship, error) {
params := url.Values{} params := url.Values{}
params.Set("id", fmt.Sprint(id)) for _, id := range ids {
params.Add("id[]", fmt.Sprint(id))
}
var relationships []*Relationship var relationships []*Relationship
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationship", params, &relationships) err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationships", params, &relationships)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -450,12 +450,44 @@ func TestAccountUnmute(t *testing.T) {
func TestGetAccountRelationship(t *testing.T) { func TestGetAccountRelationship(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.String()) ids := r.URL.Query()["id[]"]
if r.URL.Query().Get("id") != "1234567" { if ids[0] == "1234567" && ids[1] == "8901234" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) fmt.Fprintln(w, `[{"id":1234567},{"id":8901234}]`)
return return
} }
fmt.Fprintln(w, `[{"id":1234567}]`) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetAccountRelationships(context.Background(), []int64{123, 456})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rels, err := client.GetAccountRelationships(context.Background(), []int64{1234567, 8901234})
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rels[0].ID != 1234567 {
t.Fatalf("want %d but %d", 1234567, rels[0].ID)
}
if rels[1].ID != 8901234 {
t.Fatalf("want %d but %d", 8901234, rels[1].ID)
}
}
func TestAccountsSearch(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query()["q"][0] != "foo" {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `[{"Username": "foobar"}, {"Username": "barfoo"}]`)
return return
})) }))
defer ts.Close() defer ts.Close()
@ -466,16 +498,52 @@ func TestGetAccountRelationship(t *testing.T) {
ClientSecret: "bar", ClientSecret: "bar",
AccessToken: "zoo", AccessToken: "zoo",
}) })
_, err := client.GetAccountRelationship(context.Background(), 123) _, err := client.AccountsSearch(context.Background(), "zzz", 2)
if err == nil { if err == nil {
t.Fatalf("should be fail: %v", err) t.Fatalf("should be fail: %v", err)
} }
rels, err := client.GetAccountRelationship(context.Background(), 1234567) res, err := client.AccountsSearch(context.Background(), "foo", 2)
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
if rels[0].ID != 1234567 { if len(res) != 2 {
t.Fatalf("want %d but %d", 1234567, rels[0].ID) t.Fatalf("result should be two: %d", len(res))
}
if res[0].Username != "foobar" {
t.Fatalf("want %q but %q", "foobar", res[0].Username)
}
if res[1].Username != "barfoo" {
t.Fatalf("want %q but %q", "barfoo", res[1].Username)
}
}
func TestFollowRemoteUser(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.PostFormValue("uri") != "foo@success.social" {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.FollowRemoteUser(context.Background(), "foo@fail.social")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
ru, err := client.FollowRemoteUser(context.Background(), "foo@success.social")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if ru.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", ru.Username)
} }
} }

View File

@ -39,7 +39,13 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
var req *http.Request var req *http.Request
ct := "application/x-www-form-urlencoded" ct := "application/x-www-form-urlencoded"
if values, ok := params.(url.Values); ok { if values, ok := params.(url.Values); ok {
req, err = http.NewRequest(method, u.String(), strings.NewReader(values.Encode())) var body io.Reader
if method == http.MethodGet {
u.RawQuery = values.Encode()
} else {
body = strings.NewReader(values.Encode())
}
req, err = http.NewRequest(method, u.String(), body)
if err != nil { if err != nil {
return err return err
} }