-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
119 lines (108 loc) · 2.66 KB
/
client_test.go
File metadata and controls
119 lines (108 loc) · 2.66 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package zsocket
import (
"bufio"
"context"
"fmt"
"net"
"net/http"
"net/url"
"testing"
"time"
)
func TestDialContextInvalidScheme(t *testing.T) {
d := &Dialer{}
if _, _, err := d.DialContext(context.Background(), "http://example.com", nil); err == nil {
t.Fatalf("expected invalid scheme error")
}
}
func TestConnectHTTPProxy(t *testing.T) {
tests := []struct {
name string
status string
wantErr bool
}{
{name: "success", status: "HTTP/1.1 200 Connection Established\r\n\r\n", wantErr: false},
{name: "forbidden", status: "HTTP/1.1 403 Forbidden\r\n\r\n", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
defer ln.Close()
done := make(chan struct{})
go func() {
defer close(done)
conn, err := ln.Accept()
if err != nil {
return
}
defer conn.Close()
br := bufio.NewReader(conn)
_, _ = br.ReadString('\n')
for {
line, _ := br.ReadString('\n')
if line == "\r\n" || line == "" {
break
}
}
_, _ = fmt.Fprint(conn, tt.status)
}()
proxyURL, _ := url.Parse("http://" + ln.Addr().String())
d := &Dialer{}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c, err := d.connectHTTPProxy(ctx, proxyURL, "example.com:80")
if c != nil {
_ = c.Close()
}
if (err != nil) != tt.wantErr {
t.Fatalf("err=%v wantErr=%v", err, tt.wantErr)
}
<-done
})
}
}
func TestConnectSOCKS5Proxy(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
defer ln.Close()
done := make(chan struct{})
go func() {
defer close(done)
conn, err := ln.Accept()
if err != nil {
return
}
defer conn.Close()
buf := make([]byte, 3)
_, _ = conn.Read(buf)
_, _ = conn.Write([]byte{0x05, 0x00})
head := make([]byte, 5)
_, _ = conn.Read(head)
addr := make([]byte, int(head[4])+2)
_, _ = conn.Read(addr)
_, _ = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x1f, 0x90})
}()
proxyURL, _ := url.Parse("socks5://" + ln.Addr().String())
d := &Dialer{}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c, err := d.connectSOCKS5Proxy(ctx, proxyURL, "example.com:80")
if err != nil {
t.Fatalf("connectSOCKS5Proxy: %v", err)
}
_ = c.Close()
<-done
}
func TestIsWebSocketUpgrade(t *testing.T) {
r, _ := http.NewRequest(http.MethodGet, "http://example.com/ws", nil)
r.Header.Set("Connection", "Upgrade")
r.Header.Set("Upgrade", "websocket")
if !IsWebSocketUpgrade(r) {
t.Fatalf("expected true")
}
}