MCPcopy Create free account
hub / github.com/antchfx/htmlquery / TestLoadURL_ErrorCases

Function TestLoadURL_ErrorCases

query_test.go:194–246  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

LoadURLFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…