Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve URL building in the logcli to strip trailing /. #2000

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions pkg/logcli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"net/url"
"path"
"strings"
"time"

Expand Down Expand Up @@ -119,7 +120,11 @@ func (c *Client) doQuery(path string, quiet bool) (*loghttp.QueryResponse, error
}

func (c *Client) doRequest(path string, quiet bool, out interface{}) error {
us := c.Address + path

us, err := buildURL(c.Address, path)
if err != nil {
return err
}
if !quiet {
log.Print(us)
}
Expand Down Expand Up @@ -175,7 +180,10 @@ func (c *Client) LiveTailQueryConn(queryStr string, delayFor int, limit int, fro
}

func (c *Client) wsConnect(path string, quiet bool) (*websocket.Conn, error) {
us := c.Address + path
us, err := buildURL(c.Address, path)
if err != nil {
return nil, err
}

tlsConfig, err := config.NewTLSConfig(&c.TLSConfig)
if err != nil {
Expand Down Expand Up @@ -213,3 +221,13 @@ func (c *Client) wsConnect(path string, quiet bool) (*websocket.Conn, error) {

return conn, nil
}

// buildURL concats a url `http://foo/bar` with a path `/buzz`.
func buildURL(u, p string) (string, error) {
url, err := url.Parse(u)
if err != nil {
return "", err
}
url.Path = path.Join(url.Path, p)
return url.String(), nil
}
28 changes: 28 additions & 0 deletions pkg/logcli/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client

import "testing"

func Test_buildURL(t *testing.T) {
tests := []struct {
name string
u, p string
want string
wantErr bool
}{
{"err", "8://2", "/bar", "", true},
{"strip /", "http://localhost//", "//bar", "http://localhost/bar", false},
{"sub path", "https://localhost/loki/", "/bar/foo", "https://localhost/loki/bar/foo", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildURL(tt.u, tt.p)
if (err != nil) != tt.wantErr {
t.Errorf("buildURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("buildURL() = %v, want %v", got, tt.want)
}
})
}
}