From 8532612d9f10ffc68e3b1cc5719a6c4e0133178e Mon Sep 17 00:00:00 2001 From: marco Date: Wed, 28 Feb 2024 13:32:43 +0100 Subject: [PATCH] wip --- pkg/apiclient/client.go | 13 +++++++++++++ pkg/apiclient/client_http.go | 6 ------ pkg/apiclient/client_http_test.go | 20 +++----------------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/pkg/apiclient/client.go b/pkg/apiclient/client.go index 7add3157ffdc..fe8fd0b6596b 100644 --- a/pkg/apiclient/client.go +++ b/pkg/apiclient/client.go @@ -77,6 +77,14 @@ func NewClient(config *Config) (*ApiClient, error) { if transport != nil { t.Transport = transport } + + println("baseurl:", baseUrl.String()) + println("path:", baseUrl.Path) + + if !strings.HasSuffix(baseUrl.Path, "/") && baseUrl.Host != "unix" { + return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not (host: %s, path: %s)", baseUrl, baseUrl.Host, baseUrl.Path) + } + t.URL = baseUrl tlsconfig := tls.Config{InsecureSkipVerify: InsecureSkipVerify} @@ -105,6 +113,11 @@ func NewClient(config *Config) (*ApiClient, error) { func NewDefaultClient(URL *url.URL, prefix string, userAgent string, client *http.Client) (*ApiClient, error) { transport, baseUrl := createTransport(URL) + + if !strings.HasSuffix(baseUrl.Path, "/") && baseUrl.Host != "unix" { + return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not (host: %s, path: %s)", baseUrl, baseUrl.Host, baseUrl.Path) + } + if client == nil { client = &http.Client{} diff --git a/pkg/apiclient/client_http.go b/pkg/apiclient/client_http.go index 0240618f5356..f7ed5c3ab6af 100644 --- a/pkg/apiclient/client_http.go +++ b/pkg/apiclient/client_http.go @@ -5,21 +5,15 @@ import ( "context" "encoding/json" "errors" - "fmt" "io" "net/http" "net/http/httputil" "net/url" - "strings" log "github.com/sirupsen/logrus" ) func (c *ApiClient) NewRequest(method, url string, body interface{}) (*http.Request, error) { - if !strings.HasSuffix(c.BaseURL.Path, "/") { - return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", c.BaseURL) - } - u, err := c.BaseURL.Parse(url) if err != nil { return nil, err diff --git a/pkg/apiclient/client_http_test.go b/pkg/apiclient/client_http_test.go index a7582eaf4379..fde3f3b2d374 100644 --- a/pkg/apiclient/client_http_test.go +++ b/pkg/apiclient/client_http_test.go @@ -15,35 +15,21 @@ import ( ) func TestNewRequestInvalid(t *testing.T) { - mux, urlx, teardown := setup() + _, urlx, teardown := setup() defer teardown() //missing slash in uri apiURL, err := url.Parse(urlx) require.NoError(t, err) - client, err := NewClient(&Config{ + _, err = NewClient(&Config{ MachineID: "test_login", Password: "test_password", UserAgent: fmt.Sprintf("crowdsec/%s", version.String()), URL: apiURL, VersionPrefix: "v1", }) - require.NoError(t, err) - - /*mock login*/ - mux.HandleFunc("/watchers/login", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusUnauthorized) - w.Write([]byte(`{"code": 401, "message" : "bad login/password"}`)) - }) - - mux.HandleFunc("/alerts", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, "GET") - w.WriteHeader(http.StatusOK) - }) - - _, _, err = client.Alerts.List(context.Background(), AlertsListOpts{}) - cstest.RequireErrorContains(t, err, "building request: BaseURL must have a trailing slash, but ") + cstest.RequireErrorContains(t, err, "BaseURL must have a trailing slash, but ") } func TestNewRequestTimeout(t *testing.T) {