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

@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestRegisterApp(t *testing.T) {
@ -41,3 +42,25 @@ func TestRegisterApp(t *testing.T) {
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())
}
}