| 1 |
package client_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/influxdb/enterprise-client/v1" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
type SimpleSaveable struct { |
| 15 |
} |
| 16 |
|
| 17 |
func (s SimpleSaveable) Path() string { |
| 18 |
return "/foo" |
| 19 |
} |
| 20 |
|
| 21 |
func Test_ClientSave(t *testing.T) { |
| 22 |
r := require.New(t) |
| 23 |
|
| 24 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 25 |
w.WriteHeader(200) |
| 26 |
})) |
| 27 |
defer ts.Close() |
| 28 |
|
| 29 |
c := client.New("") |
| 30 |
c.URL = ts.URL |
| 31 |
|
| 32 |
res, err := c.Save(SimpleSaveable{}) |
| 33 |
r.NoError(err) |
| 34 |
r.Equal(200, res.StatusCode) |
| 35 |
} |
| 36 |
|
| 37 |
func Test_ClientSave_AuthHeaderSet(t *testing.T) { |
| 38 |
r := require.New(t) |
| 39 |
|
| 40 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 |
w.Write([]byte(r.Header.Get("X-Authorization"))) |
| 42 |
})) |
| 43 |
defer ts.Close() |
| 44 |
|
| 45 |
c := client.New("my-token") |
| 46 |
c.URL = ts.URL |
| 47 |
|
| 48 |
res, err := c.Save(SimpleSaveable{}) |
| 49 |
r.NoError(err) |
| 50 |
r.Equal(200, res.StatusCode) |
| 51 |
|
| 52 |
b, _ := ioutil.ReadAll(res.Body) |
| 53 |
r.Equal("my-token", string(b)) |
| 54 |
} |
| 55 |
|
| 56 |
func Test_ClientSave_500(t *testing.T) { |
| 57 |
r := require.New(t) |
| 58 |
|
| 59 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 |
w.WriteHeader(500) |
| 61 |
fmt.Fprint(w, `{"error":"oops!"}`) |
| 62 |
})) |
| 63 |
defer ts.Close() |
| 64 |
|
| 65 |
c := client.New("") |
| 66 |
c.URL = ts.URL |
| 67 |
|
| 68 |
res, err := c.Save(SimpleSaveable{}) |
| 69 |
r.Equal(500, res.StatusCode) |
| 70 |
r.Error(err) |
| 71 |
r.Equal("oops!", err.Error()) |
| 72 |
} |
| 73 |
|
| 74 |
func Test_ClientSave_422(t *testing.T) { |
| 75 |
r := require.New(t) |
| 76 |
|
| 77 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 78 |
w.WriteHeader(422) |
| 79 |
fmt.Fprint(w, ` |
| 80 |
{ |
| 81 |
"errors": { |
| 82 |
"cluster_id": [ |
| 83 |
"ClusterID can not be blank." |
| 84 |
], |
| 85 |
"host": [ |
| 86 |
"Host can not be blank." |
| 87 |
], |
| 88 |
"product": [ |
| 89 |
"Product can not be blank." |
| 90 |
], |
| 91 |
"server_id": [ |
| 92 |
"ServerID can not be blank." |
| 93 |
], |
| 94 |
"version": [ |
| 95 |
"Version can not be blank." |
| 96 |
] |
| 97 |
} |
| 98 |
} |
| 99 |
`) |
| 100 |
})) |
| 101 |
defer ts.Close() |
| 102 |
|
| 103 |
c := client.New("") |
| 104 |
c.URL = ts.URL |
| 105 |
|
| 106 |
res, err := c.Save(SimpleSaveable{}) |
| 107 |
r.Equal(422, res.StatusCode) |
| 108 |
r.Error(err) |
| 109 |
|
| 110 |
ve := err.(client.ValidationErrors) |
| 111 |
r.Equal([]string{"Version can not be blank."}, ve.Errors["version"]) |
| 112 |
} |