mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 08:51:11 +08:00
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:
parent
82099e26ee
commit
6339532d78
5 changed files with 1547 additions and 95 deletions
5
go.mod
5
go.mod
|
|
@ -9,4 +9,7 @@ require (
|
|||
github.com/go-json-experiment/json v0.0.0-20250517221953-25912455fbc8
|
||||
)
|
||||
|
||||
require github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
require (
|
||||
github.com/gorilla/schema v1.4.1 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue