-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
97 lines (93 loc) · 2.09 KB
/
example_test.go
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
package ara_test
import (
"context"
"fmt"
"github.com/cevatbarisyilmaz/ara"
"io/ioutil"
"log"
"net"
"net/http"
)
func ExampleNewClient() {
server := &http.Server{Addr: "127.0.0.1:80", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("Alo?"))
if err != nil {
log.Fatal(err)
}
})}
go func() {
_ = server.ListenAndServe()
}()
defer func() {
err := server.Shutdown(context.Background())
if err != nil {
log.Fatal(err)
}
}()
client := ara.NewClient(ara.NewCustomResolver(map[string][]string{"example.com": {"127.0.0.1"}}))
res, _ := client.Get("http://example.com")
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
// Output: Alo?
}
func ExampleNewTransport() {
server := &http.Server{Addr: "127.0.0.1:80", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("Alo?"))
if err != nil {
log.Fatal(err)
}
})}
go func() {
_ = server.ListenAndServe()
}()
defer func() {
err := server.Shutdown(context.Background())
if err != nil {
log.Fatal(err)
}
}()
client := &http.Client{
Transport: ara.NewTransport(ara.NewCustomResolver(map[string][]string{"example.com": {"127.0.0.1"}})),
}
res, _ := client.Get("http://example.com")
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
// Output: Alo?
}
func ExampleDialer() {
go func() {
listener, err := net.Listen("tcp", "127.0.0.1:1919")
if err != nil {
log.Fatal(err)
}
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
_, err = conn.Write([]byte("Alo?"))
if err != nil {
log.Fatal(err)
}
err = conn.Close()
if err != nil {
log.Fatal(err)
}
err = listener.Close()
if err != nil {
log.Fatal(err)
}
}()
dialer := ara.Dialer{
Resolver: ara.NewCustomResolver(map[string][]string{"example.com": {"127.0.0.1"}}),
}
conn, err := dialer.DialContext(context.Background(), "tcp", "example.com:1919")
if err != nil {
log.Fatal(err)
}
res, err := ioutil.ReadAll(conn)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(res))
// Output: Alo?
}