-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_test.go
More file actions
106 lines (97 loc) · 2.78 KB
/
Copy pathhttp_test.go
File metadata and controls
106 lines (97 loc) · 2.78 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
package singproxy
import (
"net/url"
"testing"
"github.com/sagernet/sing-box/option"
)
func TestParseHTTP_Basic(t *testing.T) {
u, _ := url.Parse("http://user:pass@http-proxy.example.com:8080")
out := &option.HTTPOutboundOptions{}
if err := parseHTTP(out, u); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.Server != "http-proxy.example.com" || out.ServerPort != 8080 {
t.Errorf("server/port: got %s:%d", out.Server, out.ServerPort)
}
if out.Username != "user" || out.Password != "pass" {
t.Errorf("auth: got %q/%q", out.Username, out.Password)
}
}
func TestParseHTTP_NoAuth(t *testing.T) {
u, _ := url.Parse("http://noauth.example.com:8080")
out := &option.HTTPOutboundOptions{}
if err := parseHTTP(out, u); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.Username != "" || out.Password != "" {
t.Errorf("auth: got %q/%q, want empty", out.Username, out.Password)
}
}
func TestParseHTTP_HTTPS_AutoTLS(t *testing.T) {
u, _ := url.Parse("https://https.example.com:443")
out := &option.HTTPOutboundOptions{}
if err := parseHTTP(out, u); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.TLS == nil || !out.TLS.Enabled {
t.Fatal("expected auto-enabled TLS for https scheme")
}
if out.TLS.ServerName != "https.example.com" {
t.Errorf("TLS SNI: got %q", out.TLS.ServerName)
}
}
func TestParseHTTP_HTTP2_AutoTLSAndALPN(t *testing.T) {
u, _ := url.Parse("http2://h2.example.com:443")
out := &option.HTTPOutboundOptions{}
if err := parseHTTP(out, u); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.TLS == nil || !out.TLS.Enabled {
t.Fatal("expected auto-enabled TLS for http2 scheme")
}
found := false
for _, a := range out.TLS.ALPN {
if a == "h2" {
found = true
break
}
}
if !found {
t.Errorf("expected h2 in ALPN, got %v", out.TLS.ALPN)
}
}
func TestParseHTTP_EmptyHost(t *testing.T) {
u, _ := url.Parse("http://:8080")
out := &option.HTTPOutboundOptions{}
err := parseHTTP(out, u)
if err == nil {
t.Fatal("expected error for empty host")
}
}
func TestParseHTTP_InvalidPort(t *testing.T) {
u, _ := url.Parse("http://host:abc")
out := &option.HTTPOutboundOptions{}
if u == nil || u.Hostname() == "" {
return
}
err := parseHTTP(out, u)
if err == nil {
t.Fatal("expected error for invalid port")
}
}
func TestParseHTTP_TLSWithParams(t *testing.T) {
u, _ := url.Parse("http://host:8080?security=tls&sni=custom-sni.com&insecure=1")
out := &option.HTTPOutboundOptions{}
if err := parseHTTP(out, u); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out.TLS == nil || !out.TLS.Enabled {
t.Fatal("expected enabled TLS from security=tls")
}
if out.TLS.ServerName != "custom-sni.com" {
t.Errorf("TLS SNI: got %q", out.TLS.ServerName)
}
if !out.TLS.Insecure {
t.Errorf("expected insecure=true")
}
}