|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/int128/oauth2cli" |
| 12 | + "github.com/int128/oauth2cli/e2e_test/authserver" |
| 13 | + "golang.org/x/oauth2" |
| 14 | +) |
| 15 | + |
| 16 | +func TestContextCancel(t *testing.T) { |
| 17 | + ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond) |
| 18 | + defer cancel() |
| 19 | + s := httptest.NewServer(&authserver.Handler{ |
| 20 | + T: t, |
| 21 | + NewAuthorizationResponse: func(r authserver.AuthorizationRequest) string { |
| 22 | + return fmt.Sprintf("%s?error=server_error", r.RedirectURI) |
| 23 | + }, |
| 24 | + NewTokenResponse: func(r authserver.TokenRequest) (int, string) { |
| 25 | + return 500, "should not reach here" |
| 26 | + }, |
| 27 | + }) |
| 28 | + defer s.Close() |
| 29 | + cfg := oauth2cli.Config{ |
| 30 | + OAuth2Config: oauth2.Config{ |
| 31 | + ClientID: "YOUR_CLIENT_ID", |
| 32 | + ClientSecret: "YOUR_CLIENT_SECRET", |
| 33 | + Scopes: []string{"email", "profile"}, |
| 34 | + Endpoint: oauth2.Endpoint{ |
| 35 | + AuthURL: s.URL + "/auth", |
| 36 | + TokenURL: s.URL + "/token", |
| 37 | + }, |
| 38 | + }, |
| 39 | + Logf: t.Logf, |
| 40 | + } |
| 41 | + _, err := oauth2cli.GetToken(ctx, cfg) |
| 42 | + if err == nil { |
| 43 | + t.Errorf("GetToken wants error but was nil") |
| 44 | + return |
| 45 | + } |
| 46 | + if !errors.Is(err, context.DeadlineExceeded) { |
| 47 | + t.Errorf("err wants DeadlineExceeded but %+v", err) |
| 48 | + } |
| 49 | +} |
0 commit comments