Skip to content

Commit 31b70cd

Browse files
committed
Merge branch '#76'
close #75
2 parents 8189c48 + 1080a6c commit 31b70cd

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ go 1.14
55
require (
66
github.com/antchfx/xpath v1.3.3
77
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
8+
github.com/stretchr/testify v1.10.0
89
golang.org/x/net v0.33.0
910
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
github.com/antchfx/xpath v1.3.3 h1:tmuPQa1Uye0Ym1Zn65vxPgfltWb/Lxu2jeqIGteJSRs=
22
github.com/antchfx/xpath v1.3.3/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
3+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
36
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
47
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
58
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
9+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
13+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
14+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
15+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
16+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
17+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
18+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
19+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
620
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
721
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
822
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
@@ -69,3 +83,8 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
6983
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
7084
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
7185
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
86+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
87+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
88+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
89+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
90+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

query.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,17 @@ func LoadURL(url string) (*html.Node, error) {
112112
encoding := resp.Header.Get("Content-Encoding")
113113
switch encoding {
114114
case "gzip":
115-
reader, err = gzip.NewReader(resp.Body)
115+
gzipReader, err := gzip.NewReader(resp.Body)
116116
if err != nil {
117117
return nil, err
118118
}
119+
reader = gzipReader
119120
case "deflate":
120-
reader, err = zlib.NewReader(resp.Body)
121+
zlibReader, err := zlib.NewReader(resp.Body)
121122
if err != nil {
122123
return nil, err
123124
}
125+
reader = zlibReader
124126
case "":
125127
reader = resp.Body
126128
default:

query_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"testing"
1313

1414
"github.com/antchfx/xpath"
15+
"github.com/stretchr/testify/require"
1516
"golang.org/x/net/html"
1617
)
1718

@@ -191,6 +192,57 @@ func TestConcurrentQuery(t *testing.T) {
191192
wg.Wait()
192193
}
193194

195+
func TestLoadURL_ErrorCases(t *testing.T) {
196+
tests := []struct {
197+
name string
198+
contentEncoding string
199+
responseBody []byte
200+
expectedError string
201+
}{
202+
{
203+
name: "ZlibReaderError",
204+
contentEncoding: "deflate",
205+
responseBody: []byte("invalid zlib data"),
206+
expectedError: "zlib",
207+
},
208+
{
209+
name: "EmptyContentEncodingNilBody",
210+
contentEncoding: "",
211+
responseBody: nil,
212+
expectedError: "EOF",
213+
},
214+
{
215+
name: "GzipReaderError",
216+
contentEncoding: "gzip",
217+
responseBody: []byte("invalid gzip data"),
218+
expectedError: "gzip",
219+
},
220+
}
221+
222+
for _, tt := range tests {
223+
t.Run(tt.name, func(t *testing.T) {
224+
// Create a local HTTP server
225+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
226+
w.Header().Set("Content-Encoding", tt.contentEncoding)
227+
w.WriteHeader(http.StatusOK)
228+
w.Write(tt.responseBody)
229+
}))
230+
defer server.Close()
231+
232+
node, err := LoadURL(server.URL)
233+
234+
// Assert that the returned node is nil
235+
require.Nil(t, node)
236+
237+
// Assert that an error is returned
238+
require.NotNil(t, err)
239+
240+
// Optionally, check the error message
241+
require.Contains(t, err.Error(), tt.expectedError)
242+
})
243+
}
244+
}
245+
194246
func loadHTML(str string) *html.Node {
195247
node, err := Parse(strings.NewReader(str))
196248
if err != nil {

0 commit comments

Comments
 (0)