-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_rule_test.go
67 lines (60 loc) · 1.37 KB
/
log_rule_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
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
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
* Created: Thu Feb 29 20:21:55 2024 mstenber
* Last modified: Fri Apr 26 11:32:38 2024 mstenber
* Edit time: 14 min
*
*/
package main
import (
"reflect"
"strconv"
"testing"
"github.com/fingon/lixie/cm"
"github.com/fingon/lixie/data"
"gotest.tools/v3/assert"
)
func logRuleToWrapper(rule *data.LogRule) cm.URLWrapper {
w := cm.URLWrapper{}
// TODO: Manually populate corresponding values
// (normally browser does it so we don't have corresponding 'prod' code)
add := func(key, value string) {
w[key] = append(w[key], value)
}
add(idKey, strconv.Itoa(rule.ID))
add(versionKey, strconv.Itoa(rule.Version))
add(commentKey, rule.Comment)
if rule.Ham {
add(hamKey, "1")
}
if rule.Disabled {
add(disabledKey, "1")
}
for i, m := range rule.Matchers {
add(fieldID(i, fieldField), m.Field)
add(fieldID(i, opField), m.Op)
add(fieldID(i, valueField), m.Value)
}
return w
}
func TestLogRuleEndecode(t *testing.T) {
rule := data.LogRule{
ID: 42,
Disabled: true,
Ham: true,
Matchers: []data.LogFieldMatcher{{
Field: "key",
Op: "=",
Value: "value",
}},
Comment: "a comment",
Version: 7,
}
w := logRuleToWrapper(&rule)
rule2, err := NewLogRuleFromForm(w)
assert.Equal(t, err, nil)
assert.Assert(t, reflect.DeepEqual(rule, *rule2))
}