@@ -14,9 +14,12 @@ import (
1414 "testing"
1515 "time"
1616
17+ "github.com/stretchr/testify/assert"
18+ "github.com/stretchr/testify/require"
1719 "github.com/uptrace/bunrouter"
1820
1921 "github.com/danielgtaylor/huma/v2"
22+ "github.com/danielgtaylor/huma/v2/humatest"
2023)
2124
2225var lastModified = time .Now ()
@@ -320,3 +323,47 @@ func BenchmarkRawBunRouterFast(b *testing.B) {
320323 r .ServeHTTP (w , req )
321324 }
322325}
326+
327+ // See https://github.com/danielgtaylor/huma/issues/859
328+ func TestWithValueShouldPropagateContext (t * testing.T ) {
329+ r := bunrouter .New ()
330+ app := New (r , huma .DefaultConfig ("Test" , "1.0.0" ))
331+
332+ type (
333+ testInput struct {}
334+ testOutput struct {}
335+ ctxKey struct {}
336+ )
337+
338+ ctxValue := "sentinelValue"
339+
340+ huma .Register (app , huma.Operation {
341+ OperationID : "test" ,
342+ Path : "/test" ,
343+ Method : http .MethodGet ,
344+ Middlewares : huma.Middlewares {
345+ func (ctx huma.Context , next func (huma.Context )) {
346+ ctx = huma .WithValue (ctx , ctxKey {}, ctxValue )
347+ next (ctx )
348+ },
349+ middleware (func (next bunrouter.HandlerFunc ) bunrouter.HandlerFunc {
350+ return func (w http.ResponseWriter , r bunrouter.Request ) error {
351+ val , _ := r .Context ().Value (ctxKey {}).(string )
352+ _ , err := io .WriteString (w , val )
353+ return err
354+ }
355+ }),
356+ },
357+ }, func (ctx context.Context , input * testInput ) (* testOutput , error ) {
358+ out := & testOutput {}
359+ return out , nil
360+ })
361+
362+ tapi := humatest .Wrap (t , app )
363+
364+ resp := tapi .Get ("/test" )
365+ assert .Equal (t , http .StatusOK , resp .Code )
366+ out , err := io .ReadAll (resp .Body )
367+ require .NoError (t , err )
368+ assert .Equal (t , ctxValue , string (out ))
369+ }
0 commit comments