-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathippool_test.go
68 lines (62 loc) · 2 KB
/
ippool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ippool
import (
"io"
"log"
"net/http"
"strings"
"testing"
"time"
)
// const random = false
const random = true
// const concurrent = 10
const concurrent = 20
// const eachTimeout = 200 * time.Millisecond
const eachTimeout = 10 * time.Second
func TestPool(t *testing.T) {
// proxies, err := Load("https", "./FREE_PROXIES_LIST/https.txt") // 貌似全部阵亡
proxies, err := Load("http", "./FREE_PROXIES_LIST/http.txt")
if err != nil {
t.Fatal("Failed to load pool:", err)
}
if random {
Shuffle(proxies)
}
req, err := http.NewRequest("GET", "http://ipinfo.io", nil)
if err != nil {
t.Fatal("Failed to create request:", err)
}
resp, proxy, err := Race(req, proxies, concurrent, eachTimeout)
if err != nil {
t.Fatal("Failed to proxy request:", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal("Failed to read response:", err)
}
log.Printf("Response from proxy %q: %s\n", proxy, string(body))
}
func TestBoundaryCheck(t *testing.T) {
req, _ := http.NewRequest("GET", "http://foo", nil)
if _, _, err := Race(req, []string{"1.2.3.4"}, concurrent, eachTimeout); err == nil ||
!strings.Contains(err.Error(), "failed to parse proxy") {
t.Fatalf("err=%v should be %q", err, "failed to parse proxy")
}
if _, _, err := Race(req, []string{"", "", ""}, concurrent, eachTimeout); err == nil ||
!strings.Contains(err.Error(), "failed to parse proxy") {
t.Fatalf("err=%v should be %q", err, "failed to parse proxy")
}
if _, _, err := Race(req, []string{}, concurrent, eachTimeout); err == nil ||
!strings.Contains(err.Error(), "be gte 1") {
t.Fatalf("err=%v should be %q", err, "be gte 1")
}
if _, _, err := Race(req, []string{"1.2.3.4"}, 0, eachTimeout); err == nil ||
!strings.Contains(err.Error(), "be gte 1") {
t.Fatalf("err=%v should be %q", err, "be gte 1")
}
if _, _, err := Race(req, []string{"1.2.3.4"}, -1, eachTimeout); err == nil ||
!strings.Contains(err.Error(), "be gte 1") {
t.Fatalf("err=%v should be %q", err, "be gte 1")
}
}