feat: Implement data binding, enhance HTML rendering, add context tests

This commit introduces several significant enhancements and completes key features:

1.  **Enhanced HTML Rendering:**
    - Defined a new `touka.HTMLRender` interface in `engine.go` for pluggable HTML rendering.
    - `Engine.HTMLRender` now uses this interface type.
    - `Context.HTML` is updated to use the configured `HTMLRender` instance, with improved error handling if no renderer is set or if rendering fails. The previous `fmt.Sprintf` fallback has been removed.
    - Added `DefaultHTMLRenderer` (using `html/template`) and helper methods `Engine.LoadHTMLGlob()` and `Engine.SetHTMLTemplate()` for easy setup.

2.  **Comprehensive Data Binding:**
    - **`Context.ShouldBindForm`**: Implemented using `gorilla/schema` to bind `x-www-form-urlencoded` and `multipart/form-data` (fields) from the request body.
    - **`Context.ShouldBindQuery`**: Implemented using `gorilla/schema` to bind URL query parameters.
    - **`Context.ShouldBindXML`**: Implemented using `encoding/xml` to bind XML data from the request body. This method also respects the `Engine.MaxRequestBodySize` limit.
    - **`Context.ShouldBind`**: Implemented as a generic binder that inspects the `Content-Type` header and dispatches to the appropriate specific binder (JSON, XML, Form). It handles missing or unsupported content types.

3.  **Comprehensive Unit Tests for `context.go`:**
    - Massively expanded `context_test.go` to provide extensive test coverage for nearly all methods in `context.go`.
    - This includes detailed tests for all new data binding methods, the updated HTML rendering logic, state management (`Keys`), request/response utilities, error handling, header and cookie manipulation, streaming, Go context integration, and logging.
    - Mocks for `HTMLRender`, `ErrorHandler`, and `reco.Logger` were used to facilitate thorough testing.

These changes significantly improve the framework's feature set, robustness, and maintainability due to increased test coverage.
This commit is contained in:
google-labs-jules[bot] 2025-06-20 07:12:05 +00:00
parent 82099e26ee
commit 6339532d78
5 changed files with 1547 additions and 95 deletions

View file

@ -9,15 +9,31 @@ import (
"runtime"
"strings"
"html/template"
"io"
"net/http"
"path"
"sync"
"github.com/WJQSERVER-STUDIO/httpc"
"github.com/fenthope/reco"
)
// HTMLRender defines the interface for HTML rendering.
type HTMLRender interface {
Render(writer io.Writer, name string, data interface{}, c *Context) error
}
// DefaultHTMLRenderer is a basic implementation of HTMLRender using html/template.
type DefaultHTMLRenderer struct {
Templates *template.Template
}
// Render executes the template and writes to the writer.
func (r *DefaultHTMLRenderer) Render(writer io.Writer, name string, data interface{}, c *Context) error {
return r.Templates.ExecuteTemplate(writer, name, data)
}
// Last 返回链中的最后一个处理函数
// 如果链为空,则返回 nil
func (c HandlersChain) Last() HandlerFunc {
@ -50,7 +66,7 @@ type Engine struct {
LogReco *reco.Logger
HTMLRender interface{} // 用于 HTML 模板渲染,可以设置为 *template.Template 或自定义渲染器接口
HTMLRender HTMLRender // 用于 HTML 模板渲染
routesInfo []RouteInfo // 存储所有注册的路由信息
@ -219,6 +235,18 @@ func Default() *Engine {
// === 外部操作方法 ===
// LoadHTMLGlob loads HTML templates from a glob pattern and sets them as the HTML renderer.
func (engine *Engine) LoadHTMLGlob(pattern string) {
tpl := template.Must(template.ParseGlob(pattern))
engine.HTMLRender = &DefaultHTMLRenderer{Templates: tpl}
}
// SetHTMLTemplate sets a custom *template.Template as the HTML renderer.
// This will wrap the *template.Template with the DefaultHTMLRenderer.
func (engine *Engine) SetHTMLTemplate(tpl *template.Template) {
engine.HTMLRender = &DefaultHTMLRenderer{Templates: tpl}
}
// SetMaxRequestBodySize 设置读取Body的最大字节数
func (engine *Engine) SetMaxRequestBodySize(size int64) {
engine.MaxRequestBodySize = size