Correct context uses
`doAPI`'s `context` does not work due to `WithContext` immutability. `Authenticate` and `RegisterApp` does not use `context` although they needs `context`, so their `context` does not work also. This commit fixes them and add test cases.
This commit is contained in:
parent
909a57c5ea
commit
fb53d41dd9
5 changed files with 100 additions and 2 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAuthenticate(t *testing.T) {
|
||||
|
@ -42,6 +43,29 @@ func TestAuthenticate(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAuthenticateWithCancel(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(3 * time.Second)
|
||||
return
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
client := NewClient(&Config{
|
||||
Server: ts.URL,
|
||||
ClientID: "foo",
|
||||
ClientSecret: "bar",
|
||||
})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go cancel()
|
||||
err := client.Authenticate(ctx, "invalid", "user")
|
||||
if err == nil {
|
||||
t.Fatalf("should be fail: %v", err)
|
||||
}
|
||||
if want := "Post " + ts.URL + "/oauth/token: context canceled"; want != err.Error() {
|
||||
t.Fatalf("want %q but %q", want, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostStatus(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") != "Bearer zoo" {
|
||||
|
@ -79,6 +103,31 @@ func TestPostStatus(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPostStatusWithCancel(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(3 * time.Second)
|
||||
return
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
client := NewClient(&Config{
|
||||
Server: ts.URL,
|
||||
ClientID: "foo",
|
||||
ClientSecret: "bar",
|
||||
})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go cancel()
|
||||
_, err := client.PostStatus(ctx, &Toot{
|
||||
Status: "foobar",
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatalf("should be fail: %v", err)
|
||||
}
|
||||
if want := "Post " + ts.URL + "/api/v1/statuses: context canceled"; want != err.Error() {
|
||||
t.Fatalf("want %q but %q", want, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTimelineHome(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, `[{"Content": "foo"}, {"Content": "bar"}]`)
|
||||
|
@ -119,6 +168,30 @@ func TestGetTimelineHome(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGetTimelineHomeWithCancel(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(3 * time.Second)
|
||||
return
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
client := NewClient(&Config{
|
||||
Server: ts.URL,
|
||||
ClientID: "foo",
|
||||
ClientSecret: "bar",
|
||||
AccessToken: "zoo",
|
||||
})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go cancel()
|
||||
_, err := client.GetTimelineHome(ctx)
|
||||
if err == nil {
|
||||
t.Fatalf("should be fail: %v", err)
|
||||
}
|
||||
if want := "Get " + ts.URL + "/api/v1/timelines/home: context canceled"; want != err.Error() {
|
||||
t.Fatalf("want %q but %q", want, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestForTheCoverages(t *testing.T) {
|
||||
(*UpdateEvent)(nil).event()
|
||||
(*NotificationEvent)(nil).event()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue