From 8048e79a2fc38778d0a1e5802b854cf5c455bf15 Mon Sep 17 00:00:00 2001 From: mig-af Date: Sat, 23 May 2026 01:46:48 +0000 Subject: [PATCH] feat: add LoadURLWithClient to support custom HTTP client --- query.go | 13 +++++++++++-- query_test.go | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/query.go b/query.go index 5415005..ab22207 100644 --- a/query.go +++ b/query.go @@ -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 } diff --git a/query_test.go b/query_test.go index 997b379..4d2fedf 100644 --- a/query_test.go +++ b/query_test.go @@ -10,6 +10,7 @@ import ( "strings" "sync" "testing" + "time" "github.com/antchfx/xpath" "golang.org/x/net/html" @@ -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)