|
|
@ -5,9 +5,8 @@
|
|
|
|
package tsweb
|
|
|
|
package tsweb
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
@ -26,7 +25,7 @@ type Response struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestNewJSONHandler(t *testing.T) {
|
|
|
|
func TestNewJSONHandler(t *testing.T) {
|
|
|
|
checkStatus := func(w *httptest.ResponseRecorder, status string) *Response {
|
|
|
|
checkStatus := func(w *httptest.ResponseRecorder, status string, code int) *Response {
|
|
|
|
d := &Response{
|
|
|
|
d := &Response{
|
|
|
|
Data: &Data{},
|
|
|
|
Data: &Data{},
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -44,6 +43,10 @@ func TestNewJSONHandler(t *testing.T) {
|
|
|
|
t.Fatalf("wrong status: %s %s", d.Status, status)
|
|
|
|
t.Fatalf("wrong status: %s %s", d.Status, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if w.Code != code {
|
|
|
|
|
|
|
|
t.Fatalf("wrong status code: %d %d", w.Code, code)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if w.Header().Get("Content-Type") != "application/json" {
|
|
|
|
if w.Header().Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatalf("wrong content type: %s", w.Header().Get("Content-Type"))
|
|
|
|
t.Fatalf("wrong content type: %s", w.Header().Get("Content-Type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -51,163 +54,139 @@ func TestNewJSONHandler(t *testing.T) {
|
|
|
|
return d
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2 1
|
|
|
|
h21 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
|
|
|
h21 := JSONHandler(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
return http.StatusOK, nil, nil
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("2 1 simple", func(t *testing.T) {
|
|
|
|
t.Run("200 simple", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
h21.ServeHTTP(w, r)
|
|
|
|
h21.ServeHTTP(w, r)
|
|
|
|
checkStatus(w, "success")
|
|
|
|
checkStatus(w, "success", http.StatusOK)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("2 1 HTTPError", func(t *testing.T) {
|
|
|
|
t.Run("403 HTTPError", func(t *testing.T) {
|
|
|
|
h := JSONHandler(func(w http.ResponseWriter, r *http.Request) HTTPError {
|
|
|
|
h := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
|
|
|
return Error(http.StatusForbidden, "forbidden", nil)
|
|
|
|
return http.StatusForbidden, nil, fmt.Errorf("forbidden")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
if w.Code != http.StatusForbidden {
|
|
|
|
checkStatus(w, "error", http.StatusForbidden)
|
|
|
|
t.Fatalf("wrong code: %d %d", w.Code, http.StatusForbidden)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 2 2
|
|
|
|
h22 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
|
|
|
h22 := JSONHandler(func(w http.ResponseWriter, r *http.Request) (*Data, error) {
|
|
|
|
return http.StatusOK, &Data{Name: "tailscale"}, nil
|
|
|
|
return &Data{Name: "tailscale"}, nil
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("2 2 get data", func(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("200 get data", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
h22.ServeHTTP(w, r)
|
|
|
|
h22.ServeHTTP(w, r)
|
|
|
|
checkStatus(w, "success")
|
|
|
|
checkStatus(w, "success", http.StatusOK)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 3 1
|
|
|
|
h31 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
|
|
|
h31 := JSONHandler(func(w http.ResponseWriter, r *http.Request, d *Data) error {
|
|
|
|
body := new(Data)
|
|
|
|
if d.Name == "" {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
|
|
|
|
return errors.New("name is empty")
|
|
|
|
return http.StatusBadRequest, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
if body.Name == "" {
|
|
|
|
|
|
|
|
return http.StatusBadRequest, nil, Error(http.StatusBadGateway, "name is empty", nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return http.StatusOK, nil, nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("3 1 post data", func(t *testing.T) {
|
|
|
|
t.Run("200 post data", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Name": "tailscale"}`))
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Name": "tailscale"}`))
|
|
|
|
h31.ServeHTTP(w, r)
|
|
|
|
h31.ServeHTTP(w, r)
|
|
|
|
checkStatus(w, "success")
|
|
|
|
checkStatus(w, "success", http.StatusOK)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("3 1 bad json", func(t *testing.T) {
|
|
|
|
t.Run("400 bad json", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{`))
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{`))
|
|
|
|
h31.ServeHTTP(w, r)
|
|
|
|
h31.ServeHTTP(w, r)
|
|
|
|
checkStatus(w, "error")
|
|
|
|
checkStatus(w, "error", http.StatusBadRequest)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("3 1 post data error", func(t *testing.T) {
|
|
|
|
t.Run("400 post data error", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
|
|
|
|
h31.ServeHTTP(w, r)
|
|
|
|
h31.ServeHTTP(w, r)
|
|
|
|
resp := checkStatus(w, "error")
|
|
|
|
resp := checkStatus(w, "error", http.StatusBadRequest)
|
|
|
|
if resp.Error != "name is empty" {
|
|
|
|
if resp.Error != "name is empty" {
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 3 2
|
|
|
|
h32 := JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
|
|
|
h32 := JSONHandler(func(w http.ResponseWriter, r *http.Request, d *Data) (*Data, error) {
|
|
|
|
body := new(Data)
|
|
|
|
if d.Price == 0 {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
|
|
|
|
return nil, errors.New("price is empty")
|
|
|
|
return http.StatusBadRequest, nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if body.Name == "root" {
|
|
|
|
|
|
|
|
return http.StatusInternalServerError, nil, fmt.Errorf("invalid name")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if body.Price == 0 {
|
|
|
|
|
|
|
|
return http.StatusBadRequest, nil, Error(http.StatusBadGateway, "price is empty", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &Data{Price: d.Price * 2}, nil
|
|
|
|
return http.StatusOK, &Data{Price: body.Price * 2}, nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("3 2 post data", func(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("200 post data", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Price": 10}`))
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Price": 10}`))
|
|
|
|
h32.ServeHTTP(w, r)
|
|
|
|
h32.ServeHTTP(w, r)
|
|
|
|
resp := checkStatus(w, "success")
|
|
|
|
resp := checkStatus(w, "success", http.StatusOK)
|
|
|
|
t.Log(resp.Data)
|
|
|
|
t.Log(resp.Data)
|
|
|
|
if resp.Data.Price != 20 {
|
|
|
|
if resp.Data.Price != 20 {
|
|
|
|
t.Fatalf("wrong price: %d %d", resp.Data.Price, 10)
|
|
|
|
t.Fatalf("wrong price: %d %d", resp.Data.Price, 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("3 2 post data error", func(t *testing.T) {
|
|
|
|
t.Run("400 post data error", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
|
|
|
|
h32.ServeHTTP(w, r)
|
|
|
|
h32.ServeHTTP(w, r)
|
|
|
|
resp := checkStatus(w, "error")
|
|
|
|
resp := checkStatus(w, "error", http.StatusBadRequest)
|
|
|
|
if resp.Error != "price is empty" {
|
|
|
|
if resp.Error != "price is empty" {
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// fn check
|
|
|
|
t.Run("500 internal server error", func(t *testing.T) {
|
|
|
|
shouldPanic := func() {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := recover()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Name": "root"}`))
|
|
|
|
if r == nil {
|
|
|
|
h32.ServeHTTP(w, r)
|
|
|
|
t.Fatalf("should panic")
|
|
|
|
resp := checkStatus(w, "error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
if resp.Error != "internal server error" {
|
|
|
|
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Log(r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("2 0 panic", func(t *testing.T) {
|
|
|
|
|
|
|
|
defer shouldPanic()
|
|
|
|
|
|
|
|
JSONHandler(func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("2 1 panic return value", func(t *testing.T) {
|
|
|
|
t.Run("500 misuse", func(t *testing.T) {
|
|
|
|
defer shouldPanic()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
JSONHandler(func(w http.ResponseWriter, r *http.Request) string {
|
|
|
|
r := httptest.NewRequest("POST", "/", nil)
|
|
|
|
return ""
|
|
|
|
JSONHandlerFunc(func(r *http.Request) (int, interface{}, error) {
|
|
|
|
})
|
|
|
|
return http.StatusOK, make(chan int), nil
|
|
|
|
})
|
|
|
|
}).ServeHTTP(w, r)
|
|
|
|
|
|
|
|
resp := checkStatus(w, "error", http.StatusInternalServerError)
|
|
|
|
t.Run("2 1 panic arguments", func(t *testing.T) {
|
|
|
|
if resp.Error != "json marshal error" {
|
|
|
|
defer shouldPanic()
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
JSONHandler(func(r *http.Request, w http.ResponseWriter) error {
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("3 1 panic arguments", func(t *testing.T) {
|
|
|
|
|
|
|
|
defer shouldPanic()
|
|
|
|
|
|
|
|
JSONHandler(func(name string, r *http.Request, w http.ResponseWriter) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("3 2 panic return value", func(t *testing.T) {
|
|
|
|
|
|
|
|
defer shouldPanic()
|
|
|
|
|
|
|
|
//lint:ignore ST1008 intentional
|
|
|
|
|
|
|
|
JSONHandler(func(name string, r *http.Request, w http.ResponseWriter) (error, string) {
|
|
|
|
|
|
|
|
return nil, "panic"
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
t.Run("2 2 forbidden", func(t *testing.T) {
|
|
|
|
t.Run("500 empty status code", func(t *testing.T) {
|
|
|
|
code := http.StatusForbidden
|
|
|
|
|
|
|
|
body := []byte("forbidden")
|
|
|
|
|
|
|
|
h := JSONHandler(func(w http.ResponseWriter, r *http.Request) (*Data, error) {
|
|
|
|
|
|
|
|
w.WriteHeader(code)
|
|
|
|
|
|
|
|
w.Write(body)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r := httptest.NewRequest("POST", "/", nil)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
JSONHandlerFunc(func(r *http.Request) (status int, data interface{}, err error) {
|
|
|
|
if w.Code != http.StatusForbidden {
|
|
|
|
return
|
|
|
|
t.Fatalf("wrong code: %d %d", w.Code, code)
|
|
|
|
}).ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
checkStatus(w, "error", http.StatusInternalServerError)
|
|
|
|
if !bytes.Equal(w.Body.Bytes(), []byte("forbidden")) {
|
|
|
|
|
|
|
|
t.Fatalf("wrong body: %s %s", w.Body.Bytes(), body)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|