@@ -843,3 +843,135 @@ func TestExpect_Adapters(t *testing.T) {
843843 assert .Contains (t , message , "test logger called" )
844844 })
845845}
846+
847+ type contextAssertionHandler struct {
848+ Formatter Formatter
849+ Reporter Reporter
850+ Logger Logger
851+ AssertionContext * AssertionContext
852+ }
853+
854+ // Success implements AssertionHandler.Success.
855+ func (h * contextAssertionHandler ) Success (ctx * AssertionContext ) {
856+ if h .Formatter == nil {
857+ panic ("DefaultAssertionHandler.Formatter is nil" )
858+ }
859+ h .Formatter .FormatSuccess (ctx )
860+ h .AssertionContext = ctx
861+
862+ }
863+
864+ // Failure implements AssertionHandler.Failure.
865+ func (h * contextAssertionHandler ) Failure (
866+ ctx * AssertionContext , failure * AssertionFailure ,
867+ ) {
868+ if h .Formatter == nil {
869+ panic ("DefaultAssertionHandler.Formatter is nil" )
870+ }
871+
872+ switch failure .Severity {
873+ case SeverityError :
874+ if h .Reporter == nil {
875+ panic ("DefaultAssertionHandler.Reporter is nil" )
876+ }
877+
878+ h .Formatter .FormatFailure (ctx , failure )
879+ h .AssertionContext = ctx
880+
881+ case SeverityLog :
882+ if h .Logger == nil {
883+ return
884+ }
885+
886+ h .Formatter .FormatFailure (ctx , failure )
887+ h .AssertionContext = ctx
888+
889+ }
890+ }
891+
892+ func TestExpect_AssertionContext_Success (t * testing.T ) {
893+ client := & mockClient {}
894+
895+ reporter := NewAssertReporter (t )
896+ formatter := & DefaultFormatter {}
897+
898+ handler := & contextAssertionHandler {Reporter : reporter , Formatter : formatter }
899+
900+ config := Config {
901+ BaseURL : "http://example.com" ,
902+ Client : client ,
903+ Reporter : reporter ,
904+ Formatter : formatter ,
905+ AssertionHandler : handler ,
906+ }
907+
908+ e := WithConfig (config )
909+
910+ req := e .GET ("/test" ).WithText ("test" )
911+ resp := req .Expect ()
912+ body := resp .Body ()
913+ assert .Equal (t , req , handler .AssertionContext .Request )
914+ assert .Equal (t , body .value , handler .AssertionContext .Response .Body ().value )
915+ }
916+
917+ func TestExpect_AssertionContextResponseRequest (t * testing.T ) {
918+
919+ prepare := func (client Client ) (* Expect , Config , * contextAssertionHandler ) {
920+ reporter := NewAssertReporter (t )
921+ formatter := & DefaultFormatter {}
922+
923+ handler := & contextAssertionHandler {Reporter : reporter , Formatter : formatter }
924+
925+ config := Config {
926+ BaseURL : "http://example.com" ,
927+ Client : client ,
928+ Reporter : reporter ,
929+ Formatter : formatter ,
930+ AssertionHandler : handler ,
931+ }
932+ return WithConfig (config ), config , handler
933+ }
934+
935+ t .Run ("success" , func (t * testing.T ) {
936+ e , _ , handler := prepare (& mockClient {})
937+ req := e .GET ("/test" ).WithText ("test" )
938+ resp := req .Expect ()
939+ resp .Body ()
940+ assert .Equal (t , & req , & handler .AssertionContext .Request )
941+ assert .Equal (t , & resp , & handler .AssertionContext .Response )
942+ })
943+
944+ t .Run ("fail request" , func (t * testing.T ) {
945+ e , _ , handler := prepare (& mockClient {err : errors .New ("test" )})
946+ req := e .GET ("/test" ).WithText ("test" )
947+ resp := req .Expect ()
948+ resp .Body ()
949+ assert .Equal (t , & req , & handler .AssertionContext .Request )
950+ assert .Nil (t , handler .AssertionContext .Response )
951+ })
952+
953+ t .Run ("fail response" , func (t * testing.T ) {
954+ client := ClientFunc (func (_ * http.Request ) (* http.Response , error ) {
955+ return & http.Response {
956+ Status : "Test Status" ,
957+ StatusCode : 504 ,
958+ }, nil
959+ })
960+ e , _ , handler := prepare (client )
961+ req := e .GET ("/test" )
962+ resp := req .Expect ()
963+ resp .Body ()
964+ assert .Equal (t , & req , & handler .AssertionContext .Request )
965+ assert .Equal (t , & resp , & handler .AssertionContext .Response )
966+ })
967+
968+ t .Run ("fail response children" , func (t * testing.T ) {
969+ e , _ , handler := prepare (& mockClient {})
970+ req := e .GET ("/test" ).WithText ("{{}" )
971+ resp := req .Expect ()
972+ resp .JSON ().Array ()
973+ req .chain .assertFlags (t , flagFailedChildren )
974+ assert .Equal (t , & req , & handler .AssertionContext .Request )
975+ assert .Equal (t , & resp , & handler .AssertionContext .Response )
976+ })
977+ }
0 commit comments