2018-12-15 15:33:54 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}{
|
|
|
|
{
|
2018-12-21 13:44:52 +00:00
|
|
|
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",
|
2018-12-21 13:44:52 +00:00
|
|
|
"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"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-12-21 13:44:52 +00:00
|
|
|
for _, tt := range tests {
|
2018-12-15 15:33:54 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
router := NewRouter()
|
|
|
|
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")
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
|
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-01-03 22:29:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
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",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"code": "internal error",
|
2018-12-21 13:44:52 +00:00
|
|
|
"message": "a panic has occurred",
|
|
|
|
"error": "not implemented"
|
2018-12-21 06:48:58 +00:00
|
|
|
}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests[1:] {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
router := NewRouter()
|
|
|
|
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")
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
|
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-01-03 22:29:22 +00:00
|
|
|
if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq {
|
|
|
|
t.Errorf("%q. get ***%s***", tt.name, diff)
|
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",
|
2018-12-21 13:44:52 +00:00
|
|
|
"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()
|
|
|
|
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")
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
|
|
|
|
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-01-03 22:29:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|