Skip to content

Commit 0e11f3c

Browse files
committed
add tests for fetch error scenarios including invalid URL and interrupted IO
1 parent fe88528 commit 0e11f3c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

sitemap_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"encoding/xml"
77
"errors"
88
"fmt"
9+
"net/http"
10+
"net/http/httptest"
911
"net/url"
1012
"reflect"
1113
"regexp/syntax"
@@ -1195,6 +1197,43 @@ func TestS_fetch(t *testing.T) {
11951197
}
11961198
}
11971199

1200+
func TestS_fetch_NewRequestError(t *testing.T) {
1201+
e := New()
1202+
1203+
_, err := e.fetch("://invalid-url")
1204+
if err == nil {
1205+
t.Error("expected error for invalid URL but got none")
1206+
}
1207+
}
1208+
1209+
func TestS_fetch_IOCopyError(t *testing.T) {
1210+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1211+
w.WriteHeader(http.StatusOK)
1212+
for i := 0; i < 1000; i++ {
1213+
_, err := w.Write([]byte("Some content that will be interrupted"))
1214+
if err != nil {
1215+
return
1216+
}
1217+
}
1218+
if hijacker, ok := w.(http.Hijacker); ok {
1219+
conn, _, _ := hijacker.Hijack()
1220+
err := conn.Close()
1221+
if err != nil {
1222+
return
1223+
}
1224+
}
1225+
}))
1226+
defer server.Close()
1227+
1228+
e := New()
1229+
e.SetFetchTimeout(1)
1230+
1231+
_, err := e.fetch(server.URL)
1232+
if err == nil {
1233+
t.Error("expected io.Copy error but got none")
1234+
}
1235+
}
1236+
11981237
func TestS_checkAndUnzipContent(t *testing.T) {
11991238
// Preparing gzipped data
12001239
//gzipPrefix := []byte("\x1f\x8b\x08")

0 commit comments

Comments
 (0)