Fix accounts test

pull/23/head
178inaba 2017-04-17 23:21:52 +09:00
parent a0232ea6ef
commit 39239ae270
2 changed files with 106 additions and 31 deletions

View File

@ -8,8 +8,12 @@ import (
"testing" "testing"
) )
func TestAccountUpdate(t *testing.T) { func TestGetAccount(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) {
if r.URL.Path != "/api/v1/accounts/1234567" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`) fmt.Fprintln(w, `{"Username": "zzz"}`)
return return
})) }))
@ -21,6 +25,74 @@ func TestAccountUpdate(t *testing.T) {
ClientSecret: "bar", ClientSecret: "bar",
AccessToken: "zoo", AccessToken: "zoo",
}) })
a, err := client.GetAccount(context.Background(), 1)
if err == nil {
t.Fatalf("should not be fail: %v", err)
}
a, err = client.GetAccount(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Username)
}
}
func TestGetAccountCurrentUser(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
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.GetAccountCurrentUser(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
a, err := client.GetAccountCurrentUser(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Username)
}
}
func TestAccountUpdate(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
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.AccountUpdate(context.Background(), &Profile{})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
a, err := client.AccountUpdate(context.Background(), &Profile{ a, err := client.AccountUpdate(context.Background(), &Profile{
DisplayName: String("display_name"), DisplayName: String("display_name"),
Note: String("note"), Note: String("note"),
@ -35,6 +107,39 @@ func TestAccountUpdate(t *testing.T) {
} }
} }
func TestGetAccountStatuses(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567/statuses" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"Content": "foo"}, {"Content": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetAccountStatuses(context.Background(), 123)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
ss, err := client.GetAccountStatuses(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if ss[0].Content != "foo" {
t.Fatalf("want %q but %q", "foo", ss[0].Content)
}
if ss[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", ss[1].Content)
}
}
func TestGetBlocks(t *testing.T) { func TestGetBlocks(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.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)

View File

@ -126,36 +126,6 @@ func TestForTheCoverages(t *testing.T) {
_ = (&ErrorEvent{io.EOF}).Error() _ = (&ErrorEvent{io.EOF}).Error()
} }
func TestGetAccount(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/accounts/1234567" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"Username": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
a, err := client.GetAccount(context.Background(), 1)
if err == nil {
t.Fatalf("should not be fail: %v", err)
}
a, err = client.GetAccount(context.Background(), 1234567)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Username)
}
}
func TestGetAccountFollowing(t *testing.T) { func TestGetAccountFollowing(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) {
if r.URL.Path != "/api/v1/accounts/1234567/following" { if r.URL.Path != "/api/v1/accounts/1234567/following" {