package relay import ( "context" "encoding/json" "net/http" "net/http/httptest" "sync/atomic" "testing" "time" ) func TestPoll(t *testing.T) { var polls atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/oauth/poll" || r.URL.Query().Get("state") != "st-1" { w.WriteHeader(http.StatusBadRequest) return } w.Header().Set("Content-Type", "application/json") if polls.Add(1) < 2 { w.WriteHeader(http.StatusAccepted) _ = json.NewEncoder(w).Encode(map[string]string{"status": "pending"}) return } _ = json.NewEncoder(w).Encode(map[string]string{"status": "ready", "code": "code-abc"}) })) defer srv.Close() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() code, err := Poll(ctx, srv.Client(), srv.URL, "st-1", 10*time.Millisecond) if err != nil { t.Fatalf("Poll: %v", err) } if code != "code-abc" { t.Fatalf("code = %q", code) } } func TestRedirectURI(t *testing.T) { if got := RedirectURI("https://x.example/"); got != "https://x.example/oauth/callback" { t.Fatalf("got %q", got) } }