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:
TSUYUSATO Kitsune 2017-04-19 18:26:12 +09:00
parent 909a57c5ea
commit fb53d41dd9
5 changed files with 100 additions and 2 deletions

View File

@ -55,6 +55,7 @@ func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error
if err != nil { if err != nil {
return nil, err return nil, err
} }
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := appConfig.Do(req) resp, err := appConfig.Do(req)
if err != nil { if err != nil {

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time"
) )
func TestRegisterApp(t *testing.T) { func TestRegisterApp(t *testing.T) {
@ -41,3 +42,25 @@ func TestRegisterApp(t *testing.T) {
t.Fatalf("want %q but %q", "bar", app.ClientSecret) t.Fatalf("want %q but %q", "bar", app.ClientSecret)
} }
} }
func TestRegisterAppWithCancel(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
fmt.Fprintln(w, `{"client_id": "foo", "client_secret": "bar"}`)
return
}))
defer ts.Close()
ctx, cancel := context.WithCancel(context.Background())
go cancel()
_, err := RegisterApp(ctx, &AppConfig{
Server: ts.URL,
Scopes: "read write follow",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
if want := "Post " + ts.URL + "/api/v1/apps: context canceled"; want != err.Error() {
t.Fatalf("want %q but %q", want, err.Error())
}
}

View File

@ -129,7 +129,7 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
} else { } else {
req, err = http.NewRequest(method, u.String(), nil) req, err = http.NewRequest(method, u.String(), nil)
} }
req.WithContext(ctx) req = req.WithContext(ctx)
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
if params != nil { if params != nil {
req.Header.Set("Content-Type", ct) req.Header.Set("Content-Type", ct)
@ -191,6 +191,7 @@ func (c *Client) Authenticate(ctx context.Context, username, password string) er
if err != nil { if err != nil {
return err return err
} }
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.Do(req) resp, err := c.Do(req)
if err != nil { if err != nil {

View File

@ -8,6 +8,7 @@ import (
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"testing" "testing"
"time"
) )
func TestAuthenticate(t *testing.T) { 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) { func TestPostStatus(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.Header.Get("Authorization") != "Bearer zoo" { 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) { func TestGetTimelineHome(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, `[{"Content": "foo"}, {"Content": "bar"}]`) 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) { func TestForTheCoverages(t *testing.T) {
(*UpdateEvent)(nil).event() (*UpdateEvent)(nil).event()
(*NotificationEvent)(nil).event() (*NotificationEvent)(nil).event()

View File

@ -89,6 +89,7 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch
} }
req, err := http.NewRequest(http.MethodGet, u.String(), in) req, err := http.NewRequest(http.MethodGet, u.String(), in)
if err == nil { if err == nil {
req = req.WithContext(ctx)
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
resp, err = c.Do(req) resp, err = c.Do(req)
if resp != nil && resp.StatusCode != http.StatusOK { if resp != nil && resp.StatusCode != http.StatusOK {
@ -114,7 +115,6 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch
} }
}() }()
return q, nil return q, nil
} }
// StreamingPublic return channel to read events on public. // StreamingPublic return channel to read events on public.