[BUGFIX] client url path

pull/9232/head
Martin Geno 2017-12-15 13:18:11 +01:00 committed by Martin/Geno
parent 3108eea330
commit 6cbbd2be2d
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 45 additions and 7 deletions

View File

@ -14,6 +14,7 @@ import (
"net"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
@ -196,8 +197,8 @@ func (c *Client) Query(q Query) (*Response, error) {
// It uses a context that can be cancelled by the command line client
func (c *Client) QueryContext(ctx context.Context, q Query) (*Response, error) {
u := c.url
u.Path = path.Join(u.Path, "query")
u.Path = "query"
values := u.Query()
values.Set("q", q.Command)
values.Set("db", q.Database)
@ -276,7 +277,7 @@ func (c *Client) QueryContext(ctx context.Context, q Query) (*Response, error) {
// If an error occurs, Response may contain additional information if populated.
func (c *Client) Write(bp BatchPoints) (*Response, error) {
u := c.url
u.Path = "write"
u.Path = path.Join(u.Path, "write")
var b bytes.Buffer
for _, p := range bp.Points {
@ -354,7 +355,7 @@ func (c *Client) Write(bp BatchPoints) (*Response, error) {
// If an error occurs, Response may contain additional information if populated.
func (c *Client) WriteLineProtocol(data, database, retentionPolicy, precision, writeConsistency string) (*Response, error) {
u := c.url
u.Path = "write"
u.Path = path.Join(u.Path, "write")
r := strings.NewReader(data)
@ -399,8 +400,9 @@ func (c *Client) WriteLineProtocol(data, database, retentionPolicy, precision, w
// Ping returns how long the request took, the version of the server it connected to, and an error if one occurred.
func (c *Client) Ping() (time.Duration, string, error) {
now := time.Now()
u := c.url
u.Path = "ping"
u.Path = path.Join(u.Path, "ping")
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {

View File

@ -12,6 +12,7 @@ import (
"mime"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"time"
@ -119,8 +120,9 @@ func NewHTTPClient(conf HTTPConfig) (Client, error) {
// Ping returns how long the request took, the version of the server it connected to, and an error if one occurred.
func (c *client) Ping(timeout time.Duration) (time.Duration, string, error) {
now := time.Now()
u := c.url
u.Path = "ping"
u.Path = path.Join(u.Path, "ping")
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
@ -367,7 +369,8 @@ func (c *client) Write(bp BatchPoints) error {
}
u := c.url
u.Path = "write"
u.Path = path.Join(u.Path, "write")
req, err := http.NewRequest("POST", u.String(), &b)
if err != nil {
return err
@ -473,7 +476,7 @@ type Result struct {
// Query sends a command to the server and returns the Response.
func (c *client) Query(q Query) (*Response, error) {
u := c.url
u.Path = "query"
u.Path = path.Join(u.Path, "query")
jsonParameters, err := json.Marshal(q.Parameters)

View File

@ -6,6 +6,8 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"path"
"reflect"
"strings"
"sync"
@ -826,3 +828,34 @@ func TestBatchPoints_SettersGetters(t *testing.T) {
t.Errorf("Expected: %s, got %s", bp.WriteConsistency(), "wc2")
}
}
func TestClientConcatURLPath(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.URL.String(), "/influxdbproxy/ping") || strings.Contains(r.URL.String(), "/ping/ping") {
t.Errorf("unexpected error. expected %v contains in %v", "/influxdbproxy/ping", r.URL)
}
var data Response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNoContent)
_ = json.NewEncoder(w).Encode(data)
}))
defer ts.Close()
url, _ := url.Parse(ts.URL)
url.Path = path.Join(url.Path, "influxdbproxy")
fmt.Println("TestClientConcatURLPath: concat with path 'influxdbproxy' result ", url.String())
c, _ := NewHTTPClient(HTTPConfig{Addr: url.String()})
defer c.Close()
_, _, err := c.Ping(0)
if err != nil {
t.Errorf("unexpected error. expected %v, actual %v", nil, err)
}
_, _, err = c.Ping(0)
if err != nil {
t.Errorf("unexpected error. expected %v, actual %v", nil, err)
}
}