@@ -40,10 +40,12 @@ import (
4040 "github.com/hyperledger/firefly-common/pkg/ffresty"
4141 "github.com/hyperledger/firefly-common/pkg/fftls"
4242 "github.com/hyperledger/firefly-common/pkg/fftypes"
43+ fflog "github.com/hyperledger/firefly-common/pkg/log"
4344 "github.com/hyperledger/firefly/internal/coreconfig"
4445 "github.com/hyperledger/firefly/mocks/eventsmocks"
4546 "github.com/hyperledger/firefly/pkg/core"
4647 "github.com/hyperledger/firefly/pkg/events"
48+ "github.com/sirupsen/logrus"
4749 "github.com/stretchr/testify/assert"
4850 "github.com/stretchr/testify/mock"
4951)
@@ -470,14 +472,10 @@ func TestRequestWithBodyReplyEndToEndWithTLS(t *testing.T) {
470472
471473 ctx , cancelCtx := context .WithCancel (context .Background ())
472474 go func () {
473- select {
474- case <- ctx .Done ():
475- shutdownContext , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
476- defer cancel ()
477- if err := server .Shutdown (shutdownContext ); err != nil {
478- return
479- }
480- }
475+ <- ctx .Done ()
476+ shutdownContext , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
477+ defer cancel ()
478+ _ = server .Shutdown (shutdownContext )
481479 }()
482480
483481 server .Handler = r
@@ -1490,3 +1488,68 @@ func TestRequestWithBodyReplyEndToEndWithBatch(t *testing.T) {
14901488func TestFirstDataNeverNil (t * testing.T ) {
14911489 assert .NotNil (t , (& whPayload {}).firstData ())
14921490}
1491+
1492+ // testHook captures logrus entries for assertions
1493+ type testHook struct { entries []* logrus.Entry }
1494+
1495+ func (h * testHook ) Levels () []logrus.Level { return logrus .AllLevels }
1496+ func (h * testHook ) Fire (e * logrus.Entry ) error {
1497+ h .entries = append (h .entries , e )
1498+ return nil
1499+ }
1500+
1501+ func TestLoggingContextPreserved (t * testing.T ) {
1502+ wh , cancel := newTestWebHooks (t )
1503+ defer cancel ()
1504+
1505+ // Capture logs at debug level
1506+ logger := logrus .StandardLogger ()
1507+ origHooks := logger .Hooks
1508+ hook := & testHook {}
1509+ logger .AddHook (hook )
1510+ logrus .SetLevel (logrus .DebugLevel )
1511+ defer logger .ReplaceHooks (origHooks )
1512+
1513+ // Minimal HTTP server to exercise delivery path
1514+ r := mux .NewRouter ()
1515+ r .HandleFunc ("/ping" , func (res http.ResponseWriter , req * http.Request ) {
1516+ res .WriteHeader (200 )
1517+ _ , _ = res .Write ([]byte (`ok` ))
1518+ }).Methods (http .MethodPost )
1519+ server := httptest .NewServer (r )
1520+ defer server .Close ()
1521+
1522+ sub := & core.Subscription {
1523+ SubscriptionRef : core.SubscriptionRef {Namespace : "ns1" },
1524+ }
1525+ to := sub .Options .TransportOptions ()
1526+ to ["url" ] = fmt .Sprintf ("http://%s/ping" , server .Listener .Addr ())
1527+ // Ensure we log via buildPayload/attemptRequest path
1528+ event := & core.EventDelivery {
1529+ EnrichedEvent : core.EnrichedEvent {Event : core.Event {ID : fftypes .NewUUID ()}},
1530+ Subscription : core.SubscriptionRef {ID : sub .ID },
1531+ }
1532+
1533+ parentCtx := fflog .WithLogField (context .Background (), "httpReq" , "req-123" )
1534+
1535+ // Expect the DeliveryResponse callback invoked along the non-reply path
1536+ mcb := wh .callbacks .handlers ["ns1" ].(* eventsmocks.Callbacks )
1537+ mcb .On ("DeliveryResponse" , mock .Anything , mock .MatchedBy (func (resp * core.EventDeliveryResponse ) bool {
1538+ return ! resp .Rejected
1539+ })).Return (nil )
1540+
1541+ err := wh .DeliveryRequest (parentCtx , mock .Anything , sub , event , nil )
1542+ assert .NoError (t , err )
1543+
1544+ // Find any log entry with the preserved fields
1545+ found := false
1546+ for _ , e := range hook .entries {
1547+ if e .Data ["httpReq" ] == "req-123" && e .Data ["webhook" ] != nil {
1548+ found = true
1549+ break
1550+ }
1551+ }
1552+ assert .True (t , found , "expected log entry with preserved httpReq and webhook fields" )
1553+
1554+ mcb .AssertExpectations (t )
1555+ }
0 commit comments