UnderlyingRequest and UnderlyingResponseWriter
Some checks are pending
Tests / Run Tests (1.23.x) (push) Waiting to run
Tests / Run Tests (1.24.x) (push) Waiting to run
Tests / Lint Code (push) Waiting to run
Tests / Build (push) Waiting to run

This commit is contained in:
Hein 2025-12-02 17:40:44 +02:00
parent abd045493a
commit 93df33e274
3 changed files with 23 additions and 0 deletions

View File

@ -141,6 +141,12 @@ func (b *BunRouterRequest) AllHeaders() map[string]string {
return headers return headers
} }
// UnderlyingRequest returns the underlying *http.Request
// This is useful when you need to pass the request to other handlers
func (b *BunRouterRequest) UnderlyingRequest() *http.Request {
return b.req.Request
}
// StandardBunRouterAdapter creates routes compatible with standard bunrouter handlers // StandardBunRouterAdapter creates routes compatible with standard bunrouter handlers
type StandardBunRouterAdapter struct { type StandardBunRouterAdapter struct {
*BunRouterAdapter *BunRouterAdapter

View File

@ -122,6 +122,7 @@ type Request interface {
PathParam(key string) string PathParam(key string) string
QueryParam(key string) string QueryParam(key string) string
AllQueryParams() map[string]string // Get all query parameters as a map AllQueryParams() map[string]string // Get all query parameters as a map
UnderlyingRequest() *http.Request // Get the underlying *http.Request for forwarding to other handlers
} }
// ResponseWriter interface abstracts HTTP response // ResponseWriter interface abstracts HTTP response
@ -130,6 +131,7 @@ type ResponseWriter interface {
WriteHeader(statusCode int) WriteHeader(statusCode int)
Write(data []byte) (int, error) Write(data []byte) (int, error)
WriteJSON(data interface{}) error WriteJSON(data interface{}) error
UnderlyingResponseWriter() http.ResponseWriter // Get the underlying http.ResponseWriter for forwarding to other handlers
} }
// HTTPHandlerFunc type for HTTP handlers // HTTPHandlerFunc type for HTTP handlers
@ -164,6 +166,10 @@ func (s *StandardResponseWriter) WriteJSON(data interface{}) error {
return json.NewEncoder(s.w).Encode(data) return json.NewEncoder(s.w).Encode(data)
} }
func (s *StandardResponseWriter) UnderlyingResponseWriter() http.ResponseWriter {
return s.w
}
// StandardRequest adapts *http.Request to Request interface // StandardRequest adapts *http.Request to Request interface
type StandardRequest struct { type StandardRequest struct {
r *http.Request r *http.Request
@ -228,6 +234,10 @@ func (s *StandardRequest) AllQueryParams() map[string]string {
return params return params
} }
func (s *StandardRequest) UnderlyingRequest() *http.Request {
return s.r
}
// TableNameProvider interface for models that provide table names // TableNameProvider interface for models that provide table names
type TableNameProvider interface { type TableNameProvider interface {
TableName() string TableName() string

View File

@ -1,6 +1,7 @@
package restheadspec package restheadspec
import ( import (
"net/http"
"testing" "testing"
) )
@ -42,6 +43,12 @@ func (m *MockRequest) AllQueryParams() map[string]string {
return m.queryParams return m.queryParams
} }
func (m *MockRequest) UnderlyingRequest() *http.Request {
// For testing purposes, return nil
// In real scenarios, you might want to construct a proper http.Request
return nil
}
func TestParseOptionsFromQueryParams(t *testing.T) { func TestParseOptionsFromQueryParams(t *testing.T) {
handler := NewHandler(nil, nil) handler := NewHandler(nil, nil)