oauth_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package oauth
  2. import (
  3. "strings"
  4. "sync"
  5. "testing"
  6. "time"
  7. )
  8. func TestAuthorizeURLMatchesClaudeCodeCLI(t *testing.T) {
  9. const want = "https://claude.com/cai/oauth/authorize"
  10. if AuthorizeURL != want {
  11. t.Fatalf("AuthorizeURL = %q, want %q", AuthorizeURL, want)
  12. }
  13. authURL := BuildAuthorizationURL("state-value", "challenge-value", ScopeOAuth)
  14. if !strings.HasPrefix(authURL, want+"?") {
  15. t.Fatalf("BuildAuthorizationURL() = %q, want prefix %q", authURL, want+"?")
  16. }
  17. for _, part := range []string{
  18. "code=true",
  19. "client_id=" + ClientID,
  20. "response_type=code",
  21. "code_challenge=challenge-value",
  22. "code_challenge_method=S256",
  23. "state=state-value",
  24. } {
  25. if !strings.Contains(authURL, part) {
  26. t.Fatalf("BuildAuthorizationURL() missing %q\nURL: %s", part, authURL)
  27. }
  28. }
  29. }
  30. func TestSessionStore_Stop_Idempotent(t *testing.T) {
  31. store := NewSessionStore()
  32. store.Stop()
  33. store.Stop()
  34. select {
  35. case <-store.stopCh:
  36. // ok
  37. case <-time.After(time.Second):
  38. t.Fatal("stopCh 未关闭")
  39. }
  40. }
  41. func TestSessionStore_Stop_Concurrent(t *testing.T) {
  42. store := NewSessionStore()
  43. var wg sync.WaitGroup
  44. for range 50 {
  45. wg.Add(1)
  46. go func() {
  47. defer wg.Done()
  48. store.Stop()
  49. }()
  50. }
  51. wg.Wait()
  52. select {
  53. case <-store.stopCh:
  54. // ok
  55. case <-time.After(time.Second):
  56. t.Fatal("stopCh 未关闭")
  57. }
  58. }