Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Feb 28, 2024
1 parent 0fe3ec1 commit f387ead
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
12 changes: 11 additions & 1 deletion pkg/apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func NewClient(config *Config) (*ApiClient, error) {
if transport != nil {
t.Transport = transport
}

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}
Expand Down Expand Up @@ -105,6 +110,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)
}

Check warning on line 116 in pkg/apiclient/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/apiclient/client.go#L115-L116

Added lines #L115 - L116 were not covered by tests

if client == nil {
client = &http.Client{}

Expand Down Expand Up @@ -177,7 +187,7 @@ func createTransport(url *url.URL) (*http.Transport, *url.URL) {
urlString := url.String()

// TCP transport
if !strings.HasPrefix(urlString, "/") {
if strings.HasPrefix(urlString, "http") {
return nil, url
}

Expand Down
6 changes: 0 additions & 6 deletions pkg/apiclient/client_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 3 additions & 17 deletions pkg/apiclient/client_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f387ead

Please sign in to comment.