mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
refactor(http): implement shared transport and clients (#1288)
* fix(http): flip to a shared transport and clients * nice threads * that is terrible * fake uri for magnet * lazy locking * why bother with r's * flip magic params to struct * refactor(http-clients): use separate clients with shared transport * refactor(http-clients): add missing license header * refactor(http-clients): defer and fix errors --------- Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
2a4fb7750b
commit
3234f0d919
48 changed files with 537 additions and 391 deletions
|
@ -1,6 +1,8 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
//go:build integration
|
||||
|
||||
package ggn
|
||||
|
||||
import (
|
||||
|
@ -8,13 +10,12 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
)
|
||||
|
||||
func Test_client_GetTorrentByID(t *testing.T) {
|
||||
|
@ -32,18 +33,26 @@ func Test_client_GetTorrentByID(t *testing.T) {
|
|||
return
|
||||
}
|
||||
|
||||
if !strings.Contains(r.RequestURI, "422368") {
|
||||
jsonPayload, _ := os.ReadFile("testdata/ggn_get_torrent_by_id_not_found.json")
|
||||
id := r.URL.Query().Get("id")
|
||||
var jsonPayload []byte
|
||||
switch id {
|
||||
case "422368":
|
||||
jsonPayload, _ = os.ReadFile("testdata/ggn_get_torrent_by_id.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
return
|
||||
break
|
||||
|
||||
case "100002":
|
||||
jsonPayload, _ = os.ReadFile("testdata/ggn_get_torrent_by_id_not_found.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
break
|
||||
}
|
||||
|
||||
// read json response
|
||||
jsonPayload, _ := os.ReadFile("testdata/ggn_get_torrent_by_id.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
//jsonPayload, _ := os.ReadFile("testdata/ggn_get_torrent_by_id.json")
|
||||
//w.Header().Set("Content-Type", "application/json")
|
||||
//w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonPayload)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
@ -77,7 +86,7 @@ func Test_client_GetTorrentByID(t *testing.T) {
|
|||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "get_by_id_2",
|
||||
name: "get_by_invalid_id",
|
||||
fields: fields{
|
||||
Url: ts.URL,
|
||||
APIKey: key,
|
||||
|
@ -89,9 +98,7 @@ func Test_client_GetTorrentByID(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
c := NewClient(tt.fields.APIKey)
|
||||
c.UseURL(tt.fields.Url)
|
||||
c := NewClient(tt.fields.APIKey, WithUrl(ts.URL))
|
||||
|
||||
got, err := c.GetTorrentByID(context.Background(), tt.args.torrentID)
|
||||
if tt.wantErr && assert.Error(t, err) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue