mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-05 16:15:43 +00:00
Fix tests: use StartTestCustomServer (no Content-Length) to match real bug scenario
Remove StartTestNoRangeServer and its tests since the real bug is about servers that don't return Content-Length at all (like GitHub's zip downloads), not servers with Content-Length but no Range support. The chunked tests (TestFetcher_DownloadChunkedContinue and TestFetcher_DownloadChunkedNotExceedSize) correctly simulate the real-world bug using StartTestCustomServer which serves files without Content-Length. Co-authored-by: monkeyWie <[email protected]>
This commit is contained in:
1 parent
b8e517b35b
commit
8b02844ec3
2 files changed
+4
-74
No files matched your search
@@ -314,62 +314,11 @@ func TestFetcher_DownloadChunkedContinue(t *testing.T) {
|
||||
downloadContinue(listener, 1, t)
|
||||
}
|
||||
|
||||
func TestFetcher_DownloadNoRangeWithSize(t *testing.T) {
|
||||
listener := test.StartTestNoRangeServer()
|
||||
defer listener.Close()
|
||||
|
||||
downloadNormal(listener, 1, t)
|
||||
}
|
||||
|
||||
func TestFetcher_DownloadNoRangeContinue(t *testing.T) {
|
||||
listener := test.StartTestNoRangeServer()
|
||||
defer listener.Close()
|
||||
|
||||
downloadContinue(listener, 1, t)
|
||||
}
|
||||
|
||||
// TestFetcher_DownloadNoRangeNotExceedSize verifies that when a server returns
|
||||
// Content-Length but doesn't support Range requests, the downloaded bytes never
|
||||
// exceed the total file size after pause and resume.
|
||||
func TestFetcher_DownloadNoRangeNotExceedSize(t *testing.T) {
|
||||
listener := test.StartTestNoRangeServer()
|
||||
defer listener.Close()
|
||||
|
||||
fetcher := downloadReady(listener, 1, t)
|
||||
err := fetcher.Start()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Let it download some data
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
if err := fetcher.Pause(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
if err := fetcher.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = fetcher.Wait()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Verify total downloaded does not exceed file size
|
||||
totalDownloaded := fetcher.Progress().TotalDownloaded()
|
||||
if totalDownloaded > test.BuildSize {
|
||||
t.Errorf("Downloaded %d bytes exceeds file size %d", totalDownloaded, test.BuildSize)
|
||||
}
|
||||
|
||||
want := test.FileMd5(test.BuildFile)
|
||||
got := test.FileMd5(test.DownloadFile)
|
||||
if want != got {
|
||||
t.Errorf("Download() got = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFetcher_DownloadChunkedNotExceedSize verifies that when a server doesn't
|
||||
// return Content-Length (chunked transfer), the download properly handles
|
||||
// pause/resume without accumulating extra bytes.
|
||||
// return Content-Length (chunked transfer, no Range support), the download
|
||||
// properly handles pause/resume without accumulating extra bytes.
|
||||
// This simulates the real-world bug where servers like GitHub's zip downloads
|
||||
// don't return Content-Length.
|
||||
func TestFetcher_DownloadChunkedNotExceedSize(t *testing.T) {
|
||||
listener := test.StartTestCustomServer()
|
||||
defer listener.Close()
|
||||
|
||||
@@ -636,25 +636,6 @@ func StartTestRangeBugServer() net.Listener {
|
||||
})
|
||||
}
|
||||
|
||||
// StartTestNoRangeServer starts a server that returns Content-Length but does NOT support Range requests.
|
||||
// This simulates servers where file size is known but range downloads are not supported.
|
||||
func StartTestNoRangeServer() net.Listener {
|
||||
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/"+BuildName, func(writer http.ResponseWriter, request *http.Request) {
|
||||
file, err := os.Open(BuildFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
writer.Header().Set("Content-Length", fmt.Sprintf("%d", BuildSize))
|
||||
writer.WriteHeader(200)
|
||||
io.Copy(writer, file)
|
||||
})
|
||||
return mux
|
||||
})
|
||||
}
|
||||
|
||||
func rangeFileHandle(writer http.ResponseWriter, request *http.Request, modifyEnd func(end int64) int64, iocpN func(file *os.File, n int64)) {
|
||||
r := request.Header.Get("Range")
|
||||
|
||||
|
||||
Reference in new issue
Block a user