2015-01-22 22:23:55 +00:00
|
|
|
package client_test
|
|
|
|
|
|
|
|
import (
|
2015-01-23 00:18:24 +00:00
|
|
|
"encoding/json"
|
2015-01-22 23:40:32 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-01-23 22:49:23 +00:00
|
|
|
"net/url"
|
2015-01-22 22:23:55 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-01-23 00:18:24 +00:00
|
|
|
"github.com/influxdb/influxdb"
|
2015-01-22 22:23:55 +00:00
|
|
|
"github.com/influxdb/influxdb/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
|
|
config := client.Config{}
|
|
|
|
_, err := client.NewClient(config)
|
|
|
|
if err != nil {
|
2015-01-22 23:40:32 +00:00
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
2015-01-22 22:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 23:40:32 +00:00
|
|
|
func TestClient_Ping(t *testing.T) {
|
|
|
|
ts := emptyTestServer()
|
|
|
|
defer ts.Close()
|
|
|
|
|
2015-01-23 22:49:23 +00:00
|
|
|
u, _ := url.Parse(ts.URL)
|
|
|
|
config := client.Config{URL: *u}
|
2015-01-22 23:40:32 +00:00
|
|
|
c, err := client.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
|
|
|
}
|
|
|
|
d, err := c.Ping()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
|
|
|
}
|
|
|
|
if d == 0 {
|
|
|
|
t.Fatalf("expected a duration greater than zero. actual %v", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 22:23:55 +00:00
|
|
|
func TestClient_Query(t *testing.T) {
|
2015-01-23 00:18:24 +00:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var data influxdb.Results
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_ = json.NewEncoder(w).Encode(data)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
2015-01-23 22:49:23 +00:00
|
|
|
u, _ := url.Parse(ts.URL)
|
|
|
|
config := client.Config{URL: *u}
|
2015-01-22 22:23:55 +00:00
|
|
|
c, err := client.NewClient(config)
|
|
|
|
if err != nil {
|
2015-01-22 23:40:32 +00:00
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
2015-01-22 22:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
query := client.Query{}
|
|
|
|
_, err = c.Query(query)
|
|
|
|
if err != nil {
|
2015-01-22 23:40:32 +00:00
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
2015-01-22 22:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:05:04 +00:00
|
|
|
func TestClient_BasicAuth(t *testing.T) {
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
u, p, ok := r.BasicAuth()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("basic auth failed")
|
|
|
|
}
|
|
|
|
if u != "username" {
|
|
|
|
t.Errorf("unexpected username, expected %q, actual %q", "username", u)
|
|
|
|
}
|
|
|
|
if p != "password" {
|
|
|
|
t.Errorf("unexpected password, expected %q, actual %q", "password", p)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
2015-01-23 22:49:23 +00:00
|
|
|
u, _ := url.Parse(ts.URL)
|
|
|
|
u.User = url.UserPassword("username", "password")
|
|
|
|
config := client.Config{URL: *u, Username: "username", Password: "password"}
|
2015-01-23 18:05:04 +00:00
|
|
|
c, err := client.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Ping()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 22:23:55 +00:00
|
|
|
func TestClient_Write(t *testing.T) {
|
2015-01-23 20:37:53 +00:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var data influxdb.Results
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_ = json.NewEncoder(w).Encode(data)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
2015-01-23 22:49:23 +00:00
|
|
|
u, _ := url.Parse(ts.URL)
|
|
|
|
config := client.Config{URL: *u}
|
2015-01-22 22:23:55 +00:00
|
|
|
c, err := client.NewClient(config)
|
|
|
|
if err != nil {
|
2015-01-22 23:40:32 +00:00
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
2015-01-22 22:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
write := client.Write{}
|
|
|
|
_, err = c.Write(write)
|
|
|
|
if err != nil {
|
2015-01-22 23:40:32 +00:00
|
|
|
t.Fatalf("unexpected error. expected %v, actual %v", nil, err)
|
2015-01-22 22:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-22 23:40:32 +00:00
|
|
|
|
|
|
|
// helper functions
|
|
|
|
|
|
|
|
func emptyTestServer() *httptest.Server {
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
}))
|
|
|
|
}
|