filebrowser/http/resource_recursive_test.go
Henrique Dias 0b925cbca9
fix: unblock stalled and frozen uploads
A TUS PATCH rejected before its body was read left the client still streaming
a chunk. net/http gives up draining an unread request body after 256KiB and
then closes the connection, so the status we replied with was lost and the
browser saw a bare transport error instead. The client could not tell a
conflict from a network fault, retried blindly, and the upload stalled after
the first chunk — while curl, which reads the response as it writes, still
reported the status and appeared to work. Drain what is left of a rejected
chunk so the answer arrives on a connection that stays usable. The explicit
r.Body.Close() had to go with it: it ran before the drain, and the server
closes the request body itself once the handler returns.

Pressing Upload awaited a recursive listing of the whole destination subtree
before a single byte was sent, with no sign that anything was happening. On a
large destination that reads as a dead button, and the walk kept running after
the client gave up, then reported the failed write as a 500. A flat upload can
only collide with a direct child of the destination, so walk recursively only
for folder uploads, stop the walk once the request is done, and show the action
as busy while the destination is checked.

Along the way: a failed upload was credited as fully sent, so the progress bar
reported 100% when nothing had been written; the progress timer was recreated
without clearing the previous one; and the chunk size field threw on a value
with no unit and raced its own debounce on submit, so it appeared to refuse
every change.

Fixes #6006
Refs #5987
2026-07-27 16:28:17 +02:00

81 lines
2.5 KiB
Go

package fbhttp
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/users"
)
func recursiveTestHandler(t *testing.T, userScope string) (http.Handler, string) {
t.Helper()
key := []byte("test-signing-key")
perm := users.Permissions{Create: true, Modify: true, Download: true}
st := scopedUserStorage(t, userScope, perm, key)
return handle(resourceGetRecursiveHandler, "/api/resources/recursive", st, &settings.Server{}), signToken(t, perm, key)
}
func TestResourceRecursiveListsTree(t *testing.T) {
userScope := t.TempDir()
if err := os.MkdirAll(filepath.Join(userScope, "target", "nested"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(userScope, "target", "nested", "file.txt"), []byte("hi"), 0o600); err != nil {
t.Fatal(err)
}
handler, token := recursiveTestHandler(t, userScope)
req, _ := http.NewRequest(http.MethodGet, "/api/resources/recursive/target", http.NoBody)
req.Header.Set("X-Auth", token)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body = %q", rec.Code, rec.Body.String())
}
var entries []RecursiveEntry
if err := json.Unmarshal(rec.Body.Bytes(), &entries); err != nil {
t.Fatalf("decode response: %v", err)
}
if len(entries) != 2 {
t.Fatalf("got %d entries, want 2: %+v", len(entries), entries)
}
}
// Walking the tree stats every entry through the scoped filesystem, which is
// expensive on a large destination. When the client has already hung up there
// is nothing to answer, so the walk must stop instead of running to completion
// and then reporting a write failure as a 500.
func TestResourceRecursiveStopsWhenClientDisconnects(t *testing.T) {
userScope := t.TempDir()
if err := os.MkdirAll(filepath.Join(userScope, "target"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(userScope, "target", "file.txt"), []byte("hi"), 0o600); err != nil {
t.Fatal(err)
}
handler, token := recursiveTestHandler(t, userScope)
ctx, cancel := context.WithCancel(context.Background())
cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/api/resources/recursive/target", http.NoBody)
req.Header.Set("X-Auth", token)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if body := rec.Body.String(); body != "" {
t.Fatalf("wrote a response for a disconnected client: %q", body)
}
}