Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,24 @@ func QuerySelectorAll(top *html.Node, selector *xpath.Expr) []*html.Node {
return elems
}

// LoadURL loads the HTML document from the specified URL. Default enabling gzip on a HTTP request.
// LoadURL loads the HTML document from the specified URL. Default HTTP Client.
func LoadURL(url string) (*html.Node, error) {
return LoadURLWithClient(url, http.DefaultClient)
}

// LoadURLWithCustomClient loads the HTML document from the specified URL. Default enabling gzip on a HTTP request.
// Custom HTTP Client.
func LoadURLWithClient(url string, client *http.Client)(*html.Node, error){
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if client == nil{
client = http.DefaultClient
}
// Enable gzip compression.
req.Header.Add("Accept-Encoding", "gzip")
resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"sync"
"testing"
"time"

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

}

func TestLoadURLWithClient(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, htmlSample)
}))
defer ts.Close()
cases := []struct {
Name string
Client *http.Client
}{
{Name: "Client nil", Client: nil},
{Name: "Client Custom", Client: &http.Client{Timeout: 10 * time.Second}},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
_, err := LoadURLWithClient(ts.URL, c.Client)
if err != nil {
t.Fatal(err)
}
})
}
}

func TestLoadURL(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, htmlSample)
Expand Down