working commit
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user