-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
49 lines (42 loc) · 927 Bytes
/
Copy pathexample_test.go
File metadata and controls
49 lines (42 loc) · 927 Bytes
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
package connect
import (
"bufio"
"fmt"
"io"
"net"
"net/http/httptest"
"strings"
)
func Example() {
upstreamLn, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
defer upstreamLn.Close()
done := make(chan struct{})
go func() {
defer close(done)
conn, err := upstreamLn.Accept()
if err == nil {
_, _ = io.Copy(io.Discard, conn)
_ = conn.Close()
}
}()
server := httptest.NewServer(New(Options{}))
defer server.Close()
conn, err := net.Dial("tcp", strings.TrimPrefix(server.URL, "http://"))
if err != nil {
panic(err)
}
defer conn.Close()
_, _ = fmt.Fprintf(conn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", upstreamLn.Addr().String(), upstreamLn.Addr().String())
line, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
panic(err)
}
fmt.Println(strings.TrimSpace(line))
_ = conn.Close()
<-done
// Output:
// HTTP/1.1 200 Connection Established
}