/
verification.go
89 lines (74 loc) · 1.85 KB
/
verification.go
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 eventing
import (
"net/http"
"net/url"
)
type Verification struct {
IsBodyConsistent bool `json:"is_body_consistent"`
IsCookieConsistent bool `json:"is_cookie_consistent"`
}
func (v *Verification) verifyCookie(r *http.Request) bool {
cookieKey := "cookie-key"
expected := "cookie-value"
actual, err := r.Cookie(cookieKey)
if err != nil {
return false
}
return actual.Value == expected
}
func (v *Verification) HandleCookie(r *http.Request) {
if v.verifyCookie(r) {
v.IsCookieConsistent = true
}
}
func (v *Verification) verifyBinary(body []byte) bool {
expected := []uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0}
actual := []uint8(body)
if len(actual) != len(expected) {
return false
}
for i := 0; i < len(actual); i++ {
if actual[i] != expected[i] {
return false
}
}
return true
}
func (v *Verification) HandleBinary(body []byte) {
if v.verifyBinary(body) {
v.IsBodyConsistent = true
}
}
func (v *Verification) verifyForm(body []byte) bool {
actual, err := url.QueryUnescape(string(body))
if err != nil {
return false
}
expected := `1={"nested-key":"nested-value"}&2=this%2Cis%2Can%2Carray&key=value&another-key=another-value`
return actual == expected
}
func (v *Verification) HandleForm(body []byte) {
if v.verifyForm(body) {
v.IsBodyConsistent = true
}
}
func (v *Verification) verifyJSON(body []byte) bool {
expected := `{"key":"here comes some value from Couchbase Function"}`
actual := string(body)
return actual == expected
}
func (v *Verification) HandleJSON(body []byte) {
if v.verifyJSON(body) {
v.IsBodyConsistent = true
}
}
func (v *Verification) verifyText(body []byte) bool {
expected := "here comes some text from Couchbase Function"
actual := string(body)
return actual == expected
}
func (v *Verification) HandleText(body []byte) {
if v.verifyText(body) {
v.IsBodyConsistent = true
}
}