119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*
|
|
* This work is published and licensed under a Creative Commons
|
|
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
|
*
|
|
* Distribution of this work is permitted, but commercial use and
|
|
* modifications are strictly prohibited.
|
|
*/
|
|
package router
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRouterStatus(t *testing.T) {
|
|
|
|
reqPath := "/hello"
|
|
handler := NewRouter()
|
|
helloHandler := func(rctx *Context) {
|
|
rctx.SetStatus(http.StatusOK)
|
|
}
|
|
handler.Get(reqPath, helloHandler)
|
|
|
|
request, err := http.NewRequest("GET", reqPath, nil)
|
|
require.NoError(t, err)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
require.Equal(t, http.StatusOK, recorder.Code)
|
|
|
|
fmt.Printf("Response code: %d\n", recorder.Code)
|
|
}
|
|
|
|
func TestRouterSendText(t *testing.T) {
|
|
testText := "hello, world"
|
|
reqPath := "/hello"
|
|
rout := NewRouter()
|
|
helloHandler := func(rctx *Context) {
|
|
rctx.SendText(testText)
|
|
}
|
|
rout.Get(reqPath, helloHandler)
|
|
|
|
request, err := http.NewRequest("GET", reqPath, nil)
|
|
require.NoError(t, err)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
rout.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)
|
|
|
|
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
|
require.Equal(t, string(bodyBytes), testText)
|
|
}
|
|
|
|
func TestRouterSendJSON(t *testing.T) {
|
|
|
|
type testStruct struct {
|
|
Message string `json:"message"`
|
|
}
|
|
testVar := testStruct{
|
|
Message: "hello, world",
|
|
}
|
|
testData, err := json.Marshal(testVar)
|
|
require.NoError(t, err)
|
|
|
|
reqPath := "/hello"
|
|
handler := NewRouter()
|
|
helloHandler := func(rctx *Context) {
|
|
rctx.SendJSON(&testVar)
|
|
}
|
|
handler.Get(reqPath, helloHandler)
|
|
|
|
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))
|
|
require.Equal(t, string(testData), string(bodyBytes))
|
|
}
|
|
|
|
func BenchmarkLoggerL(b *testing.B) {
|
|
reqPath := "/hello"
|
|
helloHandler := func(rctx *Context) {
|
|
rctx.SetStatus(http.StatusOK)
|
|
}
|
|
handler := NewRouter()
|
|
handler.Get(reqPath, helloHandler)
|
|
|
|
request, err := http.NewRequest("GET", reqPath, nil)
|
|
require.NoError(b, err)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
require.Equal(b, http.StatusOK, recorder.Code)
|
|
}
|
|
}
|