File: proxy_test.go

package info (click to toggle)
golang-goptlib 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: makefile: 8
file content (80 lines) | stat: -rw-r--r-- 2,197 bytes parent folder | download | duplicates (4)
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
package pt

import (
	"os"
	"testing"
)

func TestGetProxyURL(t *testing.T) {
	badTests := [...]string{
		"bogus",
		"http:",
		"://127.0.0.1",
		"//127.0.0.1",
		"http:127.0.0.1",
		"://[::1]",
		"//[::1]",
		"http:[::1]",
		"://localhost",
		"//localhost",
		"http:localhost",
		// No port in these.
		"http://127.0.0.1",
		"socks4a://127.0.0.1",
		"socks5://127.0.0.1",
		"http://127.0.0.1:",
		"http://[::1]",
		"http://localhost",
		"unknown://localhost/whatever",
		// No host in these.
		"http://:8080",
		"socks4a://:1080",
		"socks5://:1080",
	}
	goodTests := [...]struct {
		input, expected string
	}{
		{"http://127.0.0.1:8080", "http://127.0.0.1:8080"},
		{"http://127.0.0.1:8080/", "http://127.0.0.1:8080/"},
		{"http://127.0.0.1:8080/path", "http://127.0.0.1:8080/path"},
		{"http://[::1]:8080", "http://[::1]:8080"},
		{"http://[::1]:8080/", "http://[::1]:8080/"},
		{"http://[::1]:8080/path", "http://[::1]:8080/path"},
		{"http://localhost:8080", "http://localhost:8080"},
		{"http://localhost:8080/", "http://localhost:8080/"},
		{"http://localhost:8080/path", "http://localhost:8080/path"},
		{"http://user@localhost:8080", "http://user@localhost:8080"},
		{"http://user:password@localhost:8080", "http://user:password@localhost:8080"},
		{"socks5://localhost:1080", "socks5://localhost:1080"},
		{"socks4a://localhost:1080", "socks4a://localhost:1080"},
		{"unknown://localhost:9999/whatever", "unknown://localhost:9999/whatever"},
	}

	os.Clearenv()
	u, err := getProxyURL()
	if err != nil {
		t.Errorf("empty environment unexpectedly returned an error: %s", err)
	}
	if u != nil {
		t.Errorf("empty environment returned %q", u)
	}

	for _, input := range badTests {
		os.Setenv("TOR_PT_PROXY", input)
		u, err = getProxyURL()
		if err == nil {
			t.Errorf("TOR_PT_PROXY=%q unexpectedly succeeded and returned %q", input, u)
		}
	}

	for _, test := range goodTests {
		os.Setenv("TOR_PT_PROXY", test.input)
		u, err := getProxyURL()
		if err != nil {
			t.Errorf("TOR_PT_PROXY=%q unexpectedly returned an error: %s", test.input, err)
		}
		if u == nil || u.String() != test.expected {
			t.Errorf("TOR_PT_PROXY=%q → %q (expected %q)", test.input, u, test.expected)
		}
	}
}