github.com/s7techlab/cckit@v0.10.5/testing/expect/response.go (about)

     1  package expect
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/hyperledger/fabric-chaincode-go/shim"
     8  	"github.com/hyperledger/fabric-protos-go/peer"
     9  	g "github.com/onsi/gomega"
    10  	"github.com/s7techlab/cckit/convert"
    11  )
    12  
    13  // ResponseOk expects peer.Response has shim.OK status and message has okMatcher matcher
    14  func ResponseOk(response peer.Response, okMatcher ...interface{}) peer.Response {
    15  	g.Expect(int(response.Status)).To(g.Equal(shim.OK), response.Message)
    16  
    17  	if len(okMatcher) > 0 {
    18  		switch t := okMatcher[0].(type) {
    19  		case string:
    20  			g.Expect(response.Message).To(g.ContainSubstring(t), "ok message not match: "+response.Message)
    21  		case g.OmegaMatcher:
    22  			g.Expect(response.Message).To(t, "ok message not match: "+response.Message)
    23  		default:
    24  			panic("Matcher type not supported")
    25  		}
    26  	}
    27  	return response
    28  }
    29  
    30  // ResponseError expects peer.Response has shim.ERROR status and message has errMatcher matcher
    31  func ResponseError(response peer.Response, errMatcher ...interface{}) peer.Response {
    32  	g.Expect(int(response.Status)).To(g.Equal(shim.ERROR), response.Message)
    33  
    34  	if len(errMatcher) > 0 {
    35  		switch t := errMatcher[0].(type) {
    36  		case string, error:
    37  			g.Expect(response.Message).To(g.ContainSubstring(fmt.Sprintf(`%s`, errMatcher[0])),
    38  				"error message not match: "+response.Message)
    39  		case g.OmegaMatcher:
    40  			g.Expect(response.Message).To(t,
    41  				"error message not match: "+response.Message)
    42  		default:
    43  			panic("Matcher type not supported")
    44  		}
    45  	}
    46  	return response
    47  }
    48  
    49  // PayloadIs expects peer.Response payload can be marshalled to target interface{} and returns converted value
    50  func PayloadIs(response peer.Response, target interface{}) interface{} {
    51  	ResponseOk(response)
    52  	data, err := convert.FromBytes(response.Payload, target)
    53  	description := ``
    54  	if err != nil {
    55  		description = err.Error()
    56  	}
    57  	g.Expect(err).To(g.BeNil(), description)
    58  	return data
    59  }
    60  
    61  // PayloadString expects payload content is string
    62  func PayloadString(response peer.Response, expectedValue string) string {
    63  	ResponseOk(response)
    64  	str := string(response.Payload)
    65  	g.Expect(str).To(g.Equal(expectedValue))
    66  	return str
    67  }
    68  
    69  // PayloadBytes expects response is ok and compares response.Payload with expected value
    70  func PayloadBytes(response peer.Response, expectedValue []byte) []byte {
    71  	ResponseOk(response)
    72  	g.Expect(response.Payload).To(g.Equal(expectedValue))
    73  	return response.Payload
    74  }
    75  
    76  func PayloadInt(response peer.Response, expectedValue int) int {
    77  	ResponseOk(response)
    78  	d, err := strconv.Atoi(string((response.Payload)))
    79  	g.Expect(err).To(g.BeNil())
    80  	g.Expect(d).To(g.Equal(expectedValue))
    81  	return d
    82  }