proxyrotator_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package proxyrotator // import "miniflux.app/v2/internal/proxyrotator"
  4. import (
  5. "testing"
  6. )
  7. func TestProxyRotator(t *testing.T) {
  8. proxyURLs := []string{
  9. "http://proxy1.example.com",
  10. "http://proxy2.example.com",
  11. "http://proxy3.example.com",
  12. }
  13. rotator, err := NewProxyRotator(proxyURLs)
  14. if err != nil {
  15. t.Fatalf("Failed to create ProxyRotator: %v", err)
  16. }
  17. if !rotator.HasProxies() {
  18. t.Fatalf("Expected rotator to have proxies")
  19. }
  20. seenProxies := make(map[string]bool)
  21. for range len(proxyURLs) * 2 {
  22. proxy := rotator.GetNextProxy()
  23. if proxy == nil {
  24. t.Fatalf("Expected a proxy, got nil")
  25. }
  26. seenProxies[proxy.String()] = true
  27. }
  28. if len(seenProxies) != len(proxyURLs) {
  29. t.Fatalf("Expected to see all proxies, but saw: %v", seenProxies)
  30. }
  31. }
  32. func TestProxyRotatorEmpty(t *testing.T) {
  33. rotator, err := NewProxyRotator([]string{})
  34. if err != nil {
  35. t.Fatalf("Failed to create ProxyRotator: %v", err)
  36. }
  37. if rotator.HasProxies() {
  38. t.Fatalf("Expected rotator to have no proxies")
  39. }
  40. proxy := rotator.GetNextProxy()
  41. if proxy != nil {
  42. t.Fatalf("Expected no proxy, got: %v", proxy)
  43. }
  44. }
  45. func TestProxyRotatorInvalidURL(t *testing.T) {
  46. invalidProxyURLs := []string{
  47. "http://validproxy.example.com",
  48. "test|test://invalidproxy.example.com",
  49. }
  50. rotator, err := NewProxyRotator(invalidProxyURLs)
  51. if err == nil {
  52. t.Fatalf("Expected an error when creating ProxyRotator with invalid URLs, but got none")
  53. }
  54. if rotator != nil {
  55. t.Fatalf("Expected rotator to be nil when initialization fails, but got: %v", rotator)
  56. }
  57. }