influxdb/http/router_test.go

336 lines
7.4 KiB
Go
Raw Normal View History

2018-12-15 15:33:54 +00:00
package http
import (
2019-01-21 15:29:15 +00:00
"fmt"
2022-04-13 20:24:27 +00:00
"io"
2018-12-15 15:33:54 +00:00
"net/http"
"net/http/httptest"
"testing"
2019-01-21 15:29:15 +00:00
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
2019-01-21 15:29:15 +00:00
"go.uber.org/zap/zaptest"
2018-12-15 15:33:54 +00:00
)
func TestRouter_NotFound(t *testing.T) {
type fields struct {
method string
path string
handlerFn http.HandlerFunc
}
type args struct {
method string
path string
}
type wants struct {
statusCode int
contentType string
body string
}
tests := []struct {
name string
fields fields
args args
wants wants
}{
{
name: "path not found",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
encodeResponse(r.Context(), w, http.StatusOK, map[string]string{"message": "pong"})
},
},
2018-12-15 15:33:54 +00:00
args: args{
method: "GET",
path: "/404",
},
wants: wants{
statusCode: http.StatusNotFound,
contentType: "application/json; charset=utf-8",
body: `
{
"code": "not found",
"message": "path not found"
2018-12-15 15:33:54 +00:00
}`,
},
},
{
name: "path found",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
encodeResponse(r.Context(), w, http.StatusOK, map[string]string{"message": "pong"})
},
},
args: args{
method: "GET",
path: "/ping",
},
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: `
{
"message": "pong"
}
`,
},
},
}
for _, tt := range tests {
2018-12-15 15:33:54 +00:00
t.Run(tt.name, func(t *testing.T) {
router := NewRouter(kithttp.NewErrorHandler(zaptest.NewLogger(t)))
2018-12-15 15:33:54 +00:00
router.HandlerFunc(tt.fields.method, tt.fields.path, tt.fields.handlerFn)
r := httptest.NewRequest(tt.args.method, tt.args.path, nil)
w := httptest.NewRecorder()
router.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-15 15:33:54 +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)
}
if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq {
t.Errorf("%q. get ***%s***", tt.name, diff)
2018-12-15 15:33:54 +00:00
}
})
}
}
2018-12-21 06:48:58 +00:00
func TestRouter_Panic(t *testing.T) {
type fields struct {
method string
path string
handlerFn http.HandlerFunc
}
type args struct {
method string
path string
}
type wants struct {
statusCode int
contentType string
body string
2019-01-21 15:29:15 +00:00
logged bool
2018-12-21 06:48:58 +00:00
}
tests := []struct {
name string
fields fields
args args
wants wants
}{
{
name: "no panic",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
encodeResponse(r.Context(), w, http.StatusOK, map[string]string{"message": "pong"})
},
},
args: args{
method: "GET",
path: "/ping",
},
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
2019-01-21 15:29:15 +00:00
logged: false,
2018-12-21 06:48:58 +00:00
body: `
{
"message": "pong"
}
`,
},
},
{
name: "panic",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
panic("not implemented")
},
},
args: args{
method: "GET",
path: "/ping",
},
wants: wants{
statusCode: http.StatusInternalServerError,
contentType: "application/json; charset=utf-8",
2019-01-21 15:29:15 +00:00
logged: true,
2018-12-21 06:48:58 +00:00
body: `
{
"code": "internal error",
"message": "a panic has occurred: /ping: not implemented"
2018-12-21 06:48:58 +00:00
}`,
},
},
}
for _, tt := range tests[1:] {
t.Run(tt.name, func(t *testing.T) {
2019-01-21 15:29:15 +00:00
logger := getPanicLogger()
defer func() {
panicLogger = logger
}()
tw := newTestLogWriter(t)
panicLogger = zaptest.NewLogger(tw)
router := NewRouter(kithttp.NewErrorHandler(zaptest.NewLogger(t)))
2018-12-21 06:48:58 +00:00
router.HandlerFunc(tt.fields.method, tt.fields.path, tt.fields.handlerFn)
r := httptest.NewRequest(tt.args.method, tt.args.path, nil)
w := httptest.NewRecorder()
router.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-21 06:48:58 +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)
}
refactor: http error serialization matches the new error schema (#15196) The http error schema has been changed to simplify the outward facing API. The `op` and `error` attributes have been dropped because they confused people. The `error` attribute will likely be readded in some form in the future, but only as additional context and will not be required or even suggested for the UI to use. Errors are now output differently both when they are serialized to JSON and when they are output as strings. The `op` is no longer used if it is present. It will only appear as an optional attribute if at all. The `message` attribute for an error is always output and it will be the prefix for any nested error. When this is serialized to JSON, the message is automatically flattened so a nested error such as: influxdb.Error{ Msg: errors.New("something bad happened"), Err: io.EOF, } This would be written to the message as: something bad happened: EOF This matches a developers expectations much more easily as most programmers assume that wrapping an error will act as a prefix for the inner error. This is flattened when written out to HTTP in order to make this logic immaterial to a frontend developer. The code is still present and plays an important role in categorizing the error type. On the other hand, the code will not be output as part of the message as it commonly plays a redundant and confusing role when humans read it. The human readable message usually gives more context and a message like with the code acting as a prefix is generally not desired. But, the code plays a very important role in helping to identify categories of errors and so it is very important as part of the return response.
2019-09-19 15:06:47 +00:00
if eq, diff, _ := jsonEqual(tt.wants.body, string(body)); tt.wants.body != "" && !eq {
t.Errorf("%q. get ***%s***", tt.name, diff)
2018-12-21 06:48:58 +00:00
}
2019-01-21 15:29:15 +00:00
if tt.wants.logged != tw.Logged() {
t.Errorf("%q. get %v, want %v", tt.name, tt.wants.logged, tw.Logged())
}
2018-12-21 06:48:58 +00:00
})
}
}
2018-12-23 07:53:11 +00:00
func TestRouter_MethodNotAllowed(t *testing.T) {
type fields struct {
method string
path string
handlerFn http.HandlerFunc
}
type args struct {
method string
path string
}
type wants struct {
statusCode int
contentType string
body string
}
tests := []struct {
name string
fields fields
args args
wants wants
}{
{
name: "method not allowed",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
encodeResponse(r.Context(), w, http.StatusOK, map[string]string{"message": "pong"})
},
},
args: args{
method: "POST",
path: "/ping",
},
wants: wants{
statusCode: http.StatusMethodNotAllowed,
contentType: "application/json; charset=utf-8",
body: `
{
"code": "method not allowed",
"message": "allow: GET, OPTIONS"
2018-12-23 07:53:11 +00:00
}`,
},
},
{
name: "method allowed",
fields: fields{
method: "GET",
path: "/ping",
handlerFn: func(w http.ResponseWriter, r *http.Request) {
encodeResponse(r.Context(), w, http.StatusOK, map[string]string{"message": "pong"})
},
},
args: args{
method: "GET",
path: "/ping",
},
wants: wants{
statusCode: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: `
{
"message": "pong"
}
`,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := NewRouter(kithttp.NewErrorHandler(zaptest.NewLogger(t)))
2018-12-23 07:53:11 +00:00
router.HandlerFunc(tt.fields.method, tt.fields.path, tt.fields.handlerFn)
r := httptest.NewRequest(tt.args.method, tt.args.path, nil)
w := httptest.NewRecorder()
router.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-23 07:53:11 +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)
}
if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq {
t.Errorf("%q. get ***%s***", tt.name, diff)
2018-12-23 07:53:11 +00:00
}
})
}
}
2019-01-21 15:29:15 +00:00
// testLogWriter is a zaptest.TestingT that captures logged messages.
type testLogWriter struct {
*testing.T
Messages []string
}
func newTestLogWriter(t *testing.T) *testLogWriter {
return &testLogWriter{T: t}
}
func (t *testLogWriter) Logf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
t.Messages = append(t.Messages, msg)
t.T.Log(msg)
}
func (t *testLogWriter) Logged() bool {
return len(t.Messages) > 0
}