This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathresponse.go
More file actions
89 lines (76 loc) · 2.82 KB
/
response.go
File metadata and controls
89 lines (76 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package expect
import (
"fmt"
"strconv"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
g "github.com/onsi/gomega"
"github.com/hyperledger-labs/cckit/serialize"
)
// ResponseOk expects peer.Response has shim.OK status and message has okMatcher matcher
func ResponseOk(response peer.Response, okMatcher ...interface{}) peer.Response {
g.Expect(int(response.Status)).To(g.Equal(shim.OK), response.Message)
if len(okMatcher) > 0 {
switch t := okMatcher[0].(type) {
case string:
g.Expect(response.Message).To(g.ContainSubstring(t), "ok message not match: "+response.Message)
case g.OmegaMatcher:
g.Expect(response.Message).To(t, "ok message not match: "+response.Message)
default:
panic("Matcher type not supported")
}
}
return response
}
// ResponseError expects peer.Response has shim.ERROR status and message has errMatcher matcher
func ResponseError(response peer.Response, errMatcher ...interface{}) peer.Response {
g.Expect(int(response.Status)).To(g.Equal(shim.ERROR), response.Message)
if len(errMatcher) > 0 {
switch t := errMatcher[0].(type) {
case string, error:
g.Expect(response.Message).To(g.ContainSubstring(fmt.Sprintf(`%s`, errMatcher[0])),
"error message not match: "+response.Message)
case g.OmegaMatcher:
g.Expect(response.Message).To(t,
"error message not match: "+response.Message)
default:
panic("Matcher type not supported")
}
}
return response
}
// PayloadIs expects peer.Response payload can be marshalled to target interface{} and returns converted value
// todo: fromBytesConverters temporaty optional
func PayloadIs(response peer.Response, target interface{}, fromBytesConverters ...serialize.FromBytesConverter) interface{} {
ResponseOk(response)
data, err := defaultFromBytesConverter(fromBytesConverters...).FromBytesTo(response.Payload, target)
description := ``
if err != nil {
description = err.Error()
}
g.Expect(err).To(g.BeNil(), description)
return data
}
func JSONPayloadIs(response peer.Response, target interface{}) interface{} {
return PayloadIs(response, target, serialize.PreferJSONSerializer)
}
// PayloadString expects payload content is string
func PayloadString(response peer.Response, expectedValue string) string {
ResponseOk(response)
str := string(response.Payload)
g.Expect(str).To(g.Equal(expectedValue))
return str
}
// PayloadBytes expects response is ok and compares response.Payload with expected value
func PayloadBytes(response peer.Response, expectedValue []byte) []byte {
ResponseOk(response)
g.Expect(response.Payload).To(g.Equal(expectedValue))
return response.Payload
}
func PayloadInt(response peer.Response, expectedValue int) int {
ResponseOk(response)
d, err := strconv.Atoi(string((response.Payload)))
g.Expect(err).To(g.BeNil())
g.Expect(d).To(g.Equal(expectedValue))
return d
}