github.com/yandex/pandora@v0.5.32/components/providers/scenario/grpc/postprocessor/assert_response.go (about)

     1  package postprocessor
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  )
     9  
    10  type errAssert struct {
    11  	pattern string
    12  	t       string
    13  }
    14  
    15  func (e *errAssert) Error() string {
    16  	return "assert failed: " + e.t + " does not contain " + e.pattern
    17  }
    18  
    19  type AssertResponse struct {
    20  	Payload    []string
    21  	StatusCode int `config:"status_code"`
    22  }
    23  
    24  func (a AssertResponse) Process(out proto.Message, code int) (map[string]any, error) {
    25  	if a.StatusCode != 0 && a.StatusCode != code {
    26  		return nil, &errAssert{
    27  			pattern: fmt.Sprintf("expect code %d, recieve code %d", a.StatusCode, code),
    28  			t:       "code",
    29  		}
    30  	}
    31  	if len(a.Payload) == 0 {
    32  		return nil, nil
    33  	}
    34  	if out == nil {
    35  		return nil, &errAssert{pattern: "response is nil", t: "payload"}
    36  	}
    37  
    38  	o := out.String()
    39  	for _, v := range a.Payload {
    40  		if !strings.Contains(o, v) {
    41  			return nil, &errAssert{pattern: v, t: "body"}
    42  		}
    43  	}
    44  
    45  	return nil, nil
    46  }
    47  
    48  func NewAssertResponsePostprocessor(cfg AssertResponse) (Postprocessor, error) {
    49  	return &cfg, nil
    50  }