-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect_test.go
More file actions
111 lines (92 loc) · 2.39 KB
/
Copy pathconnect_test.go
File metadata and controls
111 lines (92 loc) · 2.39 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
package httpproxy
import (
"bytes"
"fmt"
"io"
"log/slog"
"net/http/httptest"
"net/url"
"strings"
"sync/atomic"
"testing"
)
func TestGetAddress(t *testing.T) {
u1, _ := url.Parse("http://foo.bar/")
if x := getAddress(u1); x != "foo.bar:80" {
t.Error(x)
}
u2, _ := url.Parse("https://foo.bar/")
if x := getAddress(u2); x != "foo.bar:443" {
t.Error(x)
}
}
func TestSimpleHTTPSRequestViaHTTP(t *testing.T) {
destsrv := makeHTTPSDestSrv(t)
defer destsrv.Close()
proxysrv := httptest.NewServer(&Server{})
defer proxysrv.Close()
resp, err := makeClient(t, proxysrv.URL).Get(destsrv.URL)
maybeFatal(t, err)
body, err := io.ReadAll(resp.Body)
maybeFatal(t, err)
maybeFatal(t, resp.Body.Close())
if !bytes.Equal(body, testBody) {
t.Errorf("status %q: got %q, wanted %q\n", resp.Status, string(body), string(testBody))
}
}
func TestSimpleHTTPSRequestViaHTTPS(t *testing.T) {
destsrv := makeHTTPSDestSrv(t)
defer destsrv.Close()
proxysrv := httptest.NewTLSServer(&Server{})
defer proxysrv.Close()
resp, err := makeClient(t, proxysrv.URL).Get(destsrv.URL)
maybeFatal(t, err)
body, err := io.ReadAll(resp.Body)
maybeFatal(t, err)
maybeFatal(t, resp.Body.Close())
if !bytes.Equal(body, testBody) {
t.Errorf("status %q: got %q, wanted %q\n", resp.Status, string(body), string(testBody))
}
}
func TestNotAHijacker(t *testing.T) {
var logbuf bytes.Buffer
logger1 := slog.New(slog.NewTextHandler(&logbuf, nil))
srv := &Server{Logger: logger1}
rw := httptest.NewRecorder()
srv.connect(rw, nil)
if x := logbuf.String(); !strings.Contains(x, ErrHijackingNotSupported.Error()) {
t.Error(x)
}
}
var fakeRoundTripperFprintfFail atomic.Bool
func init() {
fakeRoundTripperFprintf = func(w io.Writer, format string, a ...any) (n int, err error) {
if fakeRoundTripperFprintfFail.Load() {
return 0, io.EOF
}
return fmt.Fprintf(w, format, a...)
}
}
func TestFailHTTPSRequestViaHTTP(t *testing.T) {
fakeRoundTripperFprintfFail.Store(true)
defer func() {
fakeRoundTripperFprintfFail.Store(false)
}()
destsrv := makeHTTPSDestSrv(t)
defer destsrv.Close()
proxysrv := httptest.NewServer(&Server{
Logger: slog.Default(),
})
defer proxysrv.Close()
client := makeClient(t, proxysrv.URL)
resp, err := client.Get(destsrv.URL)
if resp != nil {
resp.Body.Close()
}
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), io.EOF.Error()) {
t.Error(err)
}
}