forked from browserutils/kooky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_example_test.go
38 lines (30 loc) · 936 Bytes
/
filter_example_test.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
package kooky_test
import (
"context"
"fmt"
"net/http"
"regexp"
"github.com/browserutils/kooky"
)
// example regex matching base64 strings
var reBase64 = regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$`)
func ExampleFilter_regex() {
var cookies = []*kooky.Cookie{{Cookie: http.Cookie{Name: `test`, Value: `dGVzdA==`}}}
ctx := context.Background()
cookies = kooky.FilterCookies(
ctx,
cookies,
ValueRegexMatch(reBase64), // filter cookies with the regex filter
// kooky.Debug, // print cookies after applying the regex filter
).Collect(ctx)
for _, cookie := range cookies {
fmt.Println(cookie.Value)
break // only first element
}
// Output: dGVzdA==
}
func ValueRegexMatch(re *regexp.Regexp) kooky.Filter {
return kooky.FilterFunc(func(cookie *kooky.Cookie) bool {
return cookie != nil && re != nil && re.Match([]byte(cookie.Value))
})
}