working commit

This commit is contained in:
2026-02-15 18:47:43 +02:00
parent c5454acb05
commit b76ea1a9ac
3 changed files with 93 additions and 7 deletions
+4 -1
View File
@@ -14,7 +14,10 @@ import (
"strconv" "strconv"
) )
// Don't ask me how it works. ;) // The code reflect string-string map to taged structure
// Limited, used only base types
// Don't ask me how it works. I'm only writer ;)
func bindObj(obj interface{}, kvmap map[string]string, sTag string) error { func bindObj(obj interface{}, kvmap map[string]string, sTag string) error {
var err error var err error
vElem := reflect.ValueOf(obj).Elem() vElem := reflect.ValueOf(obj).Elem()
+4 -4
View File
@@ -86,9 +86,9 @@ func (rctx *Context) GetContext() context.Context {
const emptyJSON = "{}" const emptyJSON = "{}"
func (ctx *Context) BindJSON(obj any) error { func (rctx *Context) BindJSON(obj any) error {
buffer := bytes.NewBuffer(nil) buffer := bytes.NewBuffer(nil)
_, err := io.Copy(buffer, ctx.Request.Body) _, err := io.Copy(buffer, rctx.Request.Body)
if err != nil { if err != nil {
return err return err
} }
@@ -103,9 +103,9 @@ func (ctx *Context) BindJSON(obj any) error {
return err return err
} }
func (ctx *Context) BindQuery(obj any) error { func (rctx *Context) BindQuery(obj any) error {
qMap := make(map[string]string) qMap := make(map[string]string)
for key, val := range ctx.Request.URL.Query() { for key, val := range rctx.Request.URL.Query() {
if len(val) == 1 { if len(val) == 1 {
qMap[key] = val[0] qMap[key] = val[0]
} }
+85 -2
View File
@@ -45,7 +45,7 @@ func TestRouterSendText(t *testing.T) {
reqPath := "/hello" reqPath := "/hello"
rout := NewRouter() rout := NewRouter()
helloHandler := func(rctx *Context) { helloHandler := func(rctx *Context) {
rctx.SendText(testText) rctx.SendText(http.StatusOK, testText)
} }
rout.Get(reqPath, helloHandler) rout.Get(reqPath, helloHandler)
@@ -68,17 +68,20 @@ func TestRouterSendJSON(t *testing.T) {
type testStruct struct { type testStruct struct {
Message string `json:"message"` Message string `json:"message"`
Code int64 `json:"code"`
} }
testVar := testStruct{ testVar := testStruct{
Message: "hello, world", Message: "hello, world",
Code: 123,
} }
testData, err := json.Marshal(testVar) testData, err := json.Marshal(testVar)
require.NoError(t, err) require.NoError(t, err)
reqPath := "/hello" reqPath := "/hello"
handler := NewRouter() handler := NewRouter()
helloHandler := func(rctx *Context) { helloHandler := func(rctx *Context) {
rctx.SendJSON(&testVar) rctx.SendJSON(http.StatusOK, &testVar)
} }
handler.Get(reqPath, helloHandler) handler.Get(reqPath, helloHandler)
@@ -99,6 +102,86 @@ func TestRouterSendJSON(t *testing.T) {
require.Equal(t, string(testData), string(bodyBytes)) require.Equal(t, string(testData), string(bodyBytes))
} }
func TestRouterBindJSON(t *testing.T) {
type testStruct struct {
Message string `json:"message"`
Code int64 `json:code"`
}
testVar := testStruct{
Message: "hello, world",
Code: 123,
}
testData, err := json.Marshal(testVar)
require.NoError(t, err)
buffer := bytes.NewBuffer(testData)
reqPath := "/hello"
handler := NewRouter()
helloHandler := func(rctx *Context) {
handVar := testStruct{}
rctx.BindJSON(&handVar)
fmt.Printf("Received message: %s - %d\n", handVar.Message, handVar.Code)
require.Equal(t, handVar.Code, int64(123))
rctx.SendJSON(http.StatusOK, &handVar)
}
handler.Post(reqPath, helloHandler)
request, err := http.NewRequest("POST", reqPath, buffer)
require.NoError(t, err)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
fmt.Printf("Response code: %d\n", recorder.Code)
require.Equal(t, http.StatusOK, recorder.Code)
bodyReader := recorder.Body
bodyBytes, err := io.ReadAll(bodyReader)
require.NoError(t, err)
bodyBytes = bytes.Trim(bodyBytes, "\n\r")
fmt.Printf("Response body: %s\n", string(bodyBytes))
require.Equal(t, string(testData), string(bodyBytes))
}
func TestRouterBindParams(t *testing.T) {
reqPath := "/hello"
handler := NewRouter()
helloHandler := func(rctx *Context) {
type Params struct {
Name string `param:"name"`
Code int64 `param:"code"`
}
params := &Params{}
rctx.BindQuery(params)
fmt.Printf("Received name: %s\n", params.Name)
fmt.Printf("Received code: %d\n", params.Code)
rctx.SendText(http.StatusOK, "hello")
}
handler.Get(reqPath, helloHandler)
reqPath = reqPath + `?name=world&code=123`
request, err := http.NewRequest("GET", reqPath, nil)
require.NoError(t, err)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
fmt.Printf("Response code: %d\n", recorder.Code)
require.Equal(t, http.StatusOK, recorder.Code)
bodyReader := recorder.Body
bodyBytes, err := io.ReadAll(bodyReader)
require.NoError(t, err)
bodyBytes = bytes.Trim(bodyBytes, "\n\r")
fmt.Printf("Response body: %s\n", string(bodyBytes))
}
func BenchmarkLoggerL(b *testing.B) { func BenchmarkLoggerL(b *testing.B) {
reqPath := "/hello" reqPath := "/hello"
helloHandler := func(rctx *Context) { helloHandler := func(rctx *Context) {