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 introduces two main improvements to the Touka web framework:
1. **Configurable Request Body Size Limit:**
- Added `MaxRequestBodySize int64` to `touka.Engine` (default 10MB).
- You can customize this via `engine.SetMaxRequestBodySize()`.
- The context methods `GetReqBodyFull()`, `GetReqBodyBuffer()`, and `ShouldBindJSON()` now adhere to this limit. They check `Content-Length` upfront and use `http.MaxBytesReader` to prevent reading excessively large request bodies into memory, enhancing protection against potential DoS attacks or high memory usage.
- Added comprehensive unit tests in `context_test.go` for this feature, covering scenarios where the limit is active, disabled, and exceeded.
2. **Enhanced Error Logging in Default Handler:**
- The `defaultErrorHandle` in `engine.go` now logs not only the primary error passed to it but also any additional errors collected in `Context.Errors` (via `c.AddError()`).
- This provides more comprehensive diagnostic information in the logs without altering the JSON error response structure sent to the client, ensuring backward compatibility.
These changes aim to improve the framework's robustness, memory safety, and debuggability.