Log error if scheme is unsupported

This commit is contained in:
Daniel Nelson 2018-04-05 11:08:31 -07:00
parent bf915fa79c
commit df78133bf3
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
2 changed files with 29 additions and 13 deletions

View File

@ -159,12 +159,18 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) {
serializer = influx.NewSerializer() serializer = influx.NewSerializer()
} }
writeURL := makeWriteURL( writeURL, err := makeWriteURL(
config.URL, config.URL,
database, database,
config.RetentionPolicy, config.RetentionPolicy,
config.Consistency) config.Consistency)
queryURL := makeQueryURL(config.URL) if err != nil {
return nil, err
}
queryURL, err := makeQueryURL(config.URL)
if err != nil {
return nil, err
}
var transport *http.Transport var transport *http.Transport
switch config.URL.Scheme { switch config.URL.Scheme {
@ -399,7 +405,7 @@ func (c *httpClient) addHeaders(req *http.Request) {
} }
} }
func makeWriteURL(loc *url.URL, db, rp, consistency string) string { func makeWriteURL(loc *url.URL, db, rp, consistency string) (string, error) {
params := url.Values{} params := url.Values{}
params.Set("db", db) params.Set("db", db)
@ -417,24 +423,26 @@ func makeWriteURL(loc *url.URL, db, rp, consistency string) string {
u.Scheme = "http" u.Scheme = "http"
u.Host = "127.0.0.1" u.Host = "127.0.0.1"
u.Path = "/write" u.Path = "/write"
case "http": case "http", "https":
case "https":
u.Path = path.Join(u.Path, "write") u.Path = path.Join(u.Path, "write")
default:
return "", fmt.Errorf("unsupported scheme: %q", loc.Scheme)
} }
u.RawQuery = params.Encode() u.RawQuery = params.Encode()
return u.String() return u.String(), nil
} }
func makeQueryURL(loc *url.URL) string { func makeQueryURL(loc *url.URL) (string, error) {
u := *loc u := *loc
switch u.Scheme { switch u.Scheme {
case "unix": case "unix":
u.Scheme = "http" u.Scheme = "http"
u.Host = "127.0.0.1" u.Host = "127.0.0.1"
u.Path = "/query" u.Path = "/query"
case "http": case "http", "https":
case "https":
u.Path = path.Join(u.Path, "query") u.Path = path.Join(u.Path, "query")
default:
return "", fmt.Errorf("unsupported scheme: %q", loc.Scheme)
} }
return u.String() return u.String(), nil
} }

View File

@ -46,6 +46,17 @@ func TestHTTP_MinimalConfig(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
func TestHTTP_UnsupportedScheme(t *testing.T) {
config := &influxdb.HTTPConfig{
URL: &url.URL{
Scheme: "foo",
Host: "localhost",
},
}
_, err := influxdb.NewHTTPClient(config)
require.Error(t, err)
}
func TestHTTP_CreateDatabase(t *testing.T) { func TestHTTP_CreateDatabase(t *testing.T) {
ts := httptest.NewServer(http.NotFoundHandler()) ts := httptest.NewServer(http.NotFoundHandler())
defer ts.Close() defer ts.Close()
@ -576,9 +587,6 @@ func TestHTTP_UnixSocket(t *testing.T) {
ts.Start() ts.Start()
defer ts.Close() defer ts.Close()
x, _ := url.Parse("unix://" + sock)
fmt.Println(x)
successResponse := []byte(`{"results": [{"statement_id": 0}]}`) successResponse := []byte(`{"results": [{"statement_id": 0}]}`)
tests := []struct { tests := []struct {