2018-12-25 02:32:29 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-11-04 15:33:31 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-13 20:24:27 +00:00
|
|
|
"io"
|
2018-12-25 02:32:29 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2019-01-03 22:29:22 +00:00
|
|
|
|
2020-11-04 15:33:31 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-04-03 17:39:20 +00:00
|
|
|
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/pkg/httpc"
|
2021-07-27 17:35:29 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-04 15:33:31 +00:00
|
|
|
"github.com/yudai/gojsondiff"
|
|
|
|
"github.com/yudai/gojsondiff/formatter"
|
2019-12-04 23:10:23 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-12-25 02:32:29 +00:00
|
|
|
)
|
|
|
|
|
2021-07-27 17:35:29 +00:00
|
|
|
func TestAPIHandlerServeLinks(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
path string
|
|
|
|
method string
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "correct path - GET",
|
|
|
|
path: "/api/v2",
|
|
|
|
method: "GET",
|
|
|
|
want: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "correct path with slash - GET",
|
|
|
|
path: "/api/v2/",
|
|
|
|
method: "GET",
|
|
|
|
want: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "correct path - POST",
|
|
|
|
path: "/api/v2",
|
|
|
|
method: "POST",
|
|
|
|
want: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "incorrect arbitrary path",
|
|
|
|
path: "/api/v2/asdf",
|
|
|
|
method: "GET",
|
|
|
|
want: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// regression test for https://github.com/influxdata/influxdb/issues/21620
|
|
|
|
name: "incorrect path at a subroute",
|
|
|
|
path: "/api/v2/query&foo=bar",
|
|
|
|
method: "GET",
|
|
|
|
want: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := httptest.NewRequest(tt.method, tt.path, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
h := NewAPIHandler(&APIBackend{Logger: zaptest.NewLogger(t)})
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
require.Equal(t, tt.want, res.StatusCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-25 02:32:29 +00:00
|
|
|
func TestAPIHandler_NotFound(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
method string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
statusCode int
|
|
|
|
contentType string
|
|
|
|
body string
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "path not found",
|
|
|
|
args: args{
|
|
|
|
method: "GET",
|
|
|
|
path: "/404",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
statusCode: http.StatusNotFound,
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"code": "not found",
|
2018-12-21 13:44:52 +00:00
|
|
|
"message": "path not found"
|
2018-12-25 02:32:29 +00:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
|
|
|
r := httptest.NewRequest(tt.args.method, tt.args.path, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
2019-06-27 01:33:20 +00:00
|
|
|
b := &APIBackend{
|
2021-09-13 19:12:35 +00:00
|
|
|
HTTPErrorHandler: kithttp.NewErrorHandler(zaptest.NewLogger(t)),
|
2019-12-04 23:10:23 +00:00
|
|
|
Logger: zaptest.NewLogger(t),
|
2019-06-27 01:33:20 +00:00
|
|
|
}
|
2018-12-25 02:32:29 +00:00
|
|
|
|
|
|
|
h := NewAPIHandler(b)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
res := w.Result()
|
|
|
|
content := res.Header.Get("Content-Type")
|
2022-04-13 20:24:27 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2018-12-25 02:32:29 +00:00
|
|
|
|
|
|
|
if res.StatusCode != tt.wants.statusCode {
|
|
|
|
t.Errorf("%q. get %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
|
|
|
|
}
|
|
|
|
if tt.wants.contentType != "" && content != tt.wants.contentType {
|
|
|
|
t.Errorf("%q. get %v, want %v", tt.name, content, tt.wants.contentType)
|
|
|
|
}
|
2019-05-08 19:51:03 +00:00
|
|
|
if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
|
2020-11-11 18:54:21 +00:00
|
|
|
t.Errorf("%q, error unmarshalling json %v", tt.name, err)
|
2019-05-08 19:51:03 +00:00
|
|
|
} else if tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. ***%s***", tt.name, diff)
|
2018-12-25 02:32:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 15:33:31 +00:00
|
|
|
|
|
|
|
func jsonEqual(s1, s2 string) (eq bool, diff string, err error) {
|
|
|
|
if s1 == s2 {
|
|
|
|
return true, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s1 == "" {
|
|
|
|
return false, s2, fmt.Errorf("s1 is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if s2 == "" {
|
|
|
|
return false, s1, fmt.Errorf("s2 is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
var o1 interface{}
|
|
|
|
if err = json.Unmarshal([]byte(s1), &o1); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var o2 interface{}
|
|
|
|
if err = json.Unmarshal([]byte(s2), &o2); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
differ := gojsondiff.New()
|
|
|
|
d, err := differ.Compare([]byte(s1), []byte(s2))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config := formatter.AsciiFormatterConfig{}
|
|
|
|
|
|
|
|
formatter := formatter.NewAsciiFormatter(o1, config)
|
|
|
|
diff, err = formatter.Format(d)
|
|
|
|
|
|
|
|
return cmp.Equal(o1, o2), diff, err
|
|
|
|
}
|
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
func mustNewHTTPClient(t *testing.T, addr, token string) *httpc.Client {
|
2020-11-04 15:33:31 +00:00
|
|
|
t.Helper()
|
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
httpClient, err := NewHTTPClient(addr, token, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return httpClient
|
2020-11-04 15:33:31 +00:00
|
|
|
}
|