@@ -542,9 +542,45 @@ func (c *Client) assertResponseHeader(key, value string, resp *http.Response) er
542542 return c .JSONComparer .FailNotEqual (expected , received )
543543}
544544
545+ // ExpectResponseBodyCallback sets expectation for response body to be received as JSON payload.
546+ //
547+ // In concurrent mode such response must be met only once or for all calls.
548+ func (c * Client ) ExpectResponseBodyCallback (cb func (received []byte ) error ) error {
549+ if c .resp == nil {
550+ err := c .do ()
551+ if err != nil {
552+ return err
553+ }
554+ }
555+
556+ return c .checkBody (nil , c .respBody , cb )
557+ }
558+
559+ // ExpectOtherResponsesBodyCallback sets expectation for response body to be received one or more times during concurrent
560+ // calling.
561+ //
562+ // For example, it may describe "Not Found" response on multiple DELETE or "Conflict" response on multiple POST.
563+ // Does not affect single (non-concurrent) calls.
564+ func (c * Client ) ExpectOtherResponsesBodyCallback (cb func (received []byte ) error ) error {
565+ c .otherRespExpected = true
566+
567+ if c .resp == nil {
568+ err := c .do ()
569+ if err != nil {
570+ return err
571+ }
572+ }
573+
574+ if c .otherResp == nil {
575+ return errNoOtherResponses
576+ }
577+
578+ return c .checkBody (nil , c .otherRespBody , cb )
579+ }
580+
545581// ExpectResponseBody sets expectation for response body to be received.
546582//
547- // In concurrent mode such response mush be met only once or for all calls.
583+ // In concurrent mode such response must be met only once or for all calls.
548584func (c * Client ) ExpectResponseBody (body []byte ) error {
549585 if c .resp == nil {
550586 err := c .do ()
@@ -553,7 +589,7 @@ func (c *Client) ExpectResponseBody(body []byte) error {
553589 }
554590 }
555591
556- return c .checkBody (body , c .respBody )
592+ return c .checkBody (body , c .respBody , nil )
557593}
558594
559595// ExpectOtherResponsesBody sets expectation for response body to be received one or more times during concurrent
@@ -575,12 +611,12 @@ func (c *Client) ExpectOtherResponsesBody(body []byte) error {
575611 return errNoOtherResponses
576612 }
577613
578- return c .checkBody (body , c .otherRespBody )
614+ return c .checkBody (body , c .otherRespBody , nil )
579615}
580616
581- func (c * Client ) checkBody (expected , received []byte ) (err error ) {
617+ func (c * Client ) checkBody (expected , received []byte , cb func ( received [] byte ) error ) (err error ) {
582618 if len (received ) == 0 {
583- if len (expected ) == 0 {
619+ if len (expected ) == 0 && expected != nil {
584620 return nil
585621 }
586622
@@ -593,28 +629,41 @@ func (c *Client) checkBody(expected, received []byte) (err error) {
593629 }
594630 }()
595631
596- if json5 .Valid (expected ) && json5 .Valid (received ) {
597- expected , err := json5 .Downgrade (expected )
632+ if (expected == nil || json5 .Valid (expected )) && json5 .Valid (received ) {
633+ return c .checkJSONBody (expected , received , cb )
634+ }
635+
636+ if cb != nil {
637+ return cb (received )
638+ }
639+
640+ if ! bytes .Equal (expected , received ) {
641+ return fmt .Errorf ("%w, expected: %q, received: %q" ,
642+ errUnexpectedBody , string (expected ), string (received ))
643+ }
644+
645+ return nil
646+ }
647+
648+ func (c * Client ) checkJSONBody (expected , received []byte , cb func (received []byte ) error ) (err error ) {
649+ if cb != nil {
650+ err = cb (received )
651+ } else {
652+ expected , err = json5 .Downgrade (expected )
598653 if err != nil {
599654 return err
600655 }
601656
602657 err = c .JSONComparer .FailNotEqual (expected , received )
603- if err != nil {
604- recCompact , cerr := assertjson .MarshalIndentCompact (json .RawMessage (received ), "" , " " , 100 )
605- if cerr == nil {
606- received = recCompact
607- }
658+ }
608659
609- return fmt .Errorf ("%w\n received:\n %s " , err , string (received ))
660+ if err != nil {
661+ recCompact , cerr := assertjson .MarshalIndentCompact (json .RawMessage (received ), "" , " " , 100 )
662+ if cerr == nil {
663+ received = recCompact
610664 }
611665
612- return nil
613- }
614-
615- if ! bytes .Equal (expected , received ) {
616- return fmt .Errorf ("%w, expected: %q, received: %q" ,
617- errUnexpectedBody , string (expected ), string (received ))
666+ return fmt .Errorf ("%w\n received:\n %s " , err , string (received ))
618667 }
619668
620669 return nil
0 commit comments