Skip to content

Commit bfb8bdf

Browse files
committed
Merge PR '#81'
2 parents 3e9a8b3 + 8048e79 commit bfb8bdf

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

query.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,24 @@ func QuerySelectorAll(top *html.Node, selector *xpath.Expr) []*html.Node {
9090
return elems
9191
}
9292

93-
// LoadURL loads the HTML document from the specified URL. Default enabling gzip on a HTTP request.
93+
// LoadURL loads the HTML document from the specified URL. Default HTTP Client.
9494
func LoadURL(url string) (*html.Node, error) {
95+
return LoadURLWithClient(url, http.DefaultClient)
96+
}
97+
98+
// LoadURLWithCustomClient loads the HTML document from the specified URL. Default enabling gzip on a HTTP request.
99+
// Custom HTTP Client.
100+
func LoadURLWithClient(url string, client *http.Client)(*html.Node, error){
95101
req, err := http.NewRequest("GET", url, nil)
96102
if err != nil {
97103
return nil, err
98104
}
105+
if client == nil{
106+
client = http.DefaultClient
107+
}
99108
// Enable gzip compression.
100109
req.Header.Add("Accept-Encoding", "gzip")
101-
resp, err := http.DefaultClient.Do(req)
110+
resp, err := client.Do(req)
102111
if err != nil {
103112
return nil, err
104113
}

query_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"sync"
1212
"testing"
13+
"time"
1314

1415
"github.com/antchfx/xpath"
1516
"golang.org/x/net/html"
@@ -69,6 +70,28 @@ func TestSelectorCache(t *testing.T) {
6970

7071
}
7172

73+
func TestLoadURLWithClient(t *testing.T) {
74+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
75+
fmt.Fprint(w, htmlSample)
76+
}))
77+
defer ts.Close()
78+
cases := []struct {
79+
Name string
80+
Client *http.Client
81+
}{
82+
{Name: "Client nil", Client: nil},
83+
{Name: "Client Custom", Client: &http.Client{Timeout: 10 * time.Second}},
84+
}
85+
for _, c := range cases {
86+
t.Run(c.Name, func(t *testing.T) {
87+
_, err := LoadURLWithClient(ts.URL, c.Client)
88+
if err != nil {
89+
t.Fatal(err)
90+
}
91+
})
92+
}
93+
}
94+
7295
func TestLoadURL(t *testing.T) {
7396
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7497
fmt.Fprint(w, htmlSample)

0 commit comments

Comments
 (0)