-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathippool_test.go
More file actions
83 lines (76 loc) · 2.37 KB
/
ippool_test.go
File metadata and controls
83 lines (76 loc) · 2.37 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ippool
import (
"io"
"log"
"net/http"
"regexp"
"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
const nOk = 5
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)
}
okItems, err := Race(req, proxies, concurrent, eachTimeout, nOk)
// if err != nil {
if len(okItems) == 0 {
t.Fatal("Failed to proxy request:", err)
}
for i, item := range okItems {
func() {
defer item.Resp.Body.Close()
log.Printf("------- okItem #%d -------\n", i+1)
body, err := io.ReadAll(item.Resp.Body)
if err != nil {
log.Println("Failed to read response:", err)
return
}
re := regexp.MustCompile(`(?i)error|html|doctype|login|password`)
if re.Match(body) {
log.Printf("Response from proxy %q: not a valid IP Info\n", item.Proxy)
return
}
log.Printf("Response from proxy %q: %s\n", item.Proxy, body)
}()
}
}
func TestBoundaryCheck(t *testing.T) {
req, _ := http.NewRequest("GET", "http://foo", nil)
if _, err := Race(req, []string{"1.2.3.4"}, concurrent, eachTimeout, nOk); 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, nOk); 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, nOk); 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, nOk); 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, nOk); err == nil ||
!strings.Contains(err.Error(), "be gte 1") {
t.Fatalf("err=%v should be %q", err, "be gte 1")
}
}