2018-06-28 19:32:16 +00:00
|
|
|
package http_test
|
2018-06-22 12:10:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-08-08 19:23:43 +00:00
|
|
|
"encoding/json"
|
|
|
|
stderrors "errors"
|
|
|
|
"io"
|
2018-06-22 12:10:04 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2018-06-28 19:32:16 +00:00
|
|
|
|
2019-08-08 19:23:43 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-01-24 00:15:42 +00:00
|
|
|
"github.com/influxdata/influxdb"
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/http"
|
2020-02-03 19:07:43 +00:00
|
|
|
kithttp "github.com/influxdata/influxdb/kit/transport/http"
|
2019-08-08 19:23:43 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-06-22 12:10:04 +00:00
|
|
|
)
|
|
|
|
|
2019-08-08 19:23:43 +00:00
|
|
|
func TestCheckError(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
|
|
|
name string
|
|
|
|
write func(w *httptest.ResponseRecorder)
|
|
|
|
want error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "platform error",
|
|
|
|
write: func(w *httptest.ResponseRecorder) {
|
2020-02-03 19:07:43 +00:00
|
|
|
h := kithttp.ErrorHandler(0)
|
2019-08-08 19:23:43 +00:00
|
|
|
err := &influxdb.Error{
|
|
|
|
Msg: "expected",
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
}
|
|
|
|
h.HandleHTTPError(context.Background(), err, w)
|
|
|
|
},
|
|
|
|
want: &influxdb.Error{
|
|
|
|
Msg: "expected",
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "text error",
|
|
|
|
write: func(w *httptest.ResponseRecorder) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
_, _ = io.WriteString(w, "upstream timeout\n")
|
|
|
|
},
|
|
|
|
want: stderrors.New("upstream timeout"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error with bad json",
|
|
|
|
write: func(w *httptest.ResponseRecorder) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
_, _ = io.WriteString(w, "upstream timeout\n")
|
|
|
|
},
|
|
|
|
want: errors.Wrap(stderrors.New("upstream timeout"), "invalid character 'u' looking for beginning of value"),
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
tt.write(w)
|
|
|
|
|
|
|
|
resp := w.Result()
|
|
|
|
cmpopt := cmp.Transformer("error", func(e error) string {
|
|
|
|
if e, ok := e.(*influxdb.Error); ok {
|
|
|
|
out, _ := json.Marshal(e)
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
return e.Error()
|
|
|
|
})
|
|
|
|
if got, want := http.CheckError(resp), tt.want; !cmp.Equal(want, got, cmpopt) {
|
|
|
|
t.Fatalf("unexpected error -want/+got:\n%s", cmp.Diff(want, got, cmpopt))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|