Добавил реализацию

This commit is contained in:
av
2026-06-28 12:20:10 +03:00
parent 4f2fb5998a
commit d8ef7063f4
12 changed files with 731 additions and 39 deletions
+4
View File
@@ -73,6 +73,7 @@ type AddRequest struct {
Torrents [][]byte // .torrent-файлы (Ф1 не использует)
Category string
SavePath string
Rename string // отображаемое имя торрента (param rename); пустое — не задаём
Paused bool
}
@@ -173,6 +174,9 @@ func (c *Client) Add(ctx context.Context, ar AddRequest) error {
if ar.SavePath != "" {
_ = mw.WriteField("savepath", ar.SavePath)
}
if ar.Rename != "" {
_ = mw.WriteField("rename", ar.Rename)
}
_ = mw.WriteField("paused", strconv.FormatBool(ar.Paused))
for i, data := range ar.Torrents {
fw, err := mw.CreateFormFile("torrents", fmt.Sprintf("file%d.torrent", i))
+45
View File
@@ -69,6 +69,51 @@ func newClient(t *testing.T, url string) *Client {
return c
}
func TestAddSendsRename(t *testing.T) {
tests := []struct {
name string
rename string
wantField string
wantHasKey bool
}{
{name: "with rename", rename: "Дюна: Часть вторая (2024)", wantField: "Дюна: Часть вторая (2024)", wantHasKey: true},
{name: "empty rename omits field", rename: "", wantHasKey: false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var gotField string
var hasKey bool
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/torrents/add", func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(1 << 20); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
_, hasKey = r.MultipartForm.Value["rename"]
gotField = r.FormValue("rename")
_, _ = w.Write([]byte("Ok."))
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
c := newClient(t, srv.URL)
err := c.Add(context.Background(), AddRequest{
URLs: []string{"magnet:?xt=urn:btih:541adcff3b6dd5dba7088ea83317d9d6fac331d6"},
Rename: tc.rename,
})
if err != nil {
t.Fatalf("Add: %v", err)
}
if hasKey != tc.wantHasKey {
t.Errorf("наличие поля rename = %v, want %v", hasKey, tc.wantHasKey)
}
if gotField != tc.wantField {
t.Errorf("rename = %q, want %q", gotField, tc.wantField)
}
})
}
}
func TestAddPerformsLazyLogin(t *testing.T) {
srv := fakeQBittorrent(t, "[]")
c := newClient(t, srv.URL)