diff --git a/app/router/bindobj.go b/app/router/bindobj.go index ef1d4f6..63b122a 100644 --- a/app/router/bindobj.go +++ b/app/router/bindobj.go @@ -14,7 +14,10 @@ import ( "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 { var err error vElem := reflect.ValueOf(obj).Elem() diff --git a/app/router/context.go b/app/router/context.go index 298d63b..bce4ab1 100644 --- a/app/router/context.go +++ b/app/router/context.go @@ -86,9 +86,9 @@ func (rctx *Context) GetContext() context.Context { const emptyJSON = "{}" -func (ctx *Context) BindJSON(obj any) error { +func (rctx *Context) BindJSON(obj any) error { buffer := bytes.NewBuffer(nil) - _, err := io.Copy(buffer, ctx.Request.Body) + _, err := io.Copy(buffer, rctx.Request.Body) if err != nil { return err } @@ -103,9 +103,9 @@ func (ctx *Context) BindJSON(obj any) error { return err } -func (ctx *Context) BindQuery(obj any) error { +func (rctx *Context) BindQuery(obj any) error { 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 { qMap[key] = val[0] } diff --git a/app/router/router_test.go b/app/router/router_test.go index c9f6ac0..446dc67 100644 --- a/app/router/router_test.go +++ b/app/router/router_test.go @@ -45,7 +45,7 @@ func TestRouterSendText(t *testing.T) { reqPath := "/hello" rout := NewRouter() helloHandler := func(rctx *Context) { - rctx.SendText(testText) + rctx.SendText(http.StatusOK, testText) } rout.Get(reqPath, helloHandler) @@ -68,17 +68,20 @@ func TestRouterSendJSON(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) reqPath := "/hello" handler := NewRouter() + helloHandler := func(rctx *Context) { - rctx.SendJSON(&testVar) + rctx.SendJSON(http.StatusOK, &testVar) } handler.Get(reqPath, helloHandler) @@ -99,6 +102,86 @@ func TestRouterSendJSON(t *testing.T) { 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) { reqPath := "/hello" helloHandler := func(rctx *Context) {