-
Notifications
You must be signed in to change notification settings - Fork 1
/
rule_test.go
80 lines (64 loc) · 1.99 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
package goformkeeper
import (
"net/http"
"net/url"
"os"
"path/filepath"
"testing"
"github.com/kr/pretty"
)
func TestLoadRule(t *testing.T) {
path := "./tests/rules.yml"
dir, _ := os.Getwd()
path = filepath.Join(dir, path)
rule, err := LoadRuleFromFile(path)
if err != nil {
t.Errorf("Failed to load rule %s", err.Error())
}
if len(rule.Forms) == 2 {
t.Errorf("RULE:", pretty.Formatter(rule))
//t.Errorf("Form: got %v\nwant %v", conf.Template.Path, expectedTemplatePath)
}
req := &http.Request{Method: "GET"}
url, _ := url.Parse("http://www.example.org/?username=foobar&password=foobarfoobar")
req.URL = url
result, err := rule.Validate("signin", req)
if err != nil {
t.Errorf("Failed to validate: %s", err.Error())
return
}
if result.ValidParam("other") != "default" {
t.Errorf("RESULT:", pretty.Formatter(result.Messages()))
t.Errorf("Failed validation: want %s, got %s", "default", result.ValidParam("other"))
}
if result.ValidParam("username") != "FOOBAR" {
t.Errorf("RESULT:", pretty.Formatter(result.Messages()))
t.Errorf("Failed validation: want %s, got %s", "FOOBAR", result.ValidParam("username"))
}
if !result.FailedOnConstraint("password", "length") {
t.Errorf("RESULT:", pretty.Formatter(result.Messages()))
}
req2 := &http.Request{Method: "GET"}
url2, _ := url.Parse("http://www.example.org/?username=foobar&password=foobarfoobar&choise=2")
req2.URL = url2
result2, err := rule.Validate("signin", req2)
if err != nil {
t.Errorf("found error: %s", err.Error())
return
}
if !result2.FailedOnConstraint("choise", "included") {
t.Errorf("RESULT:", pretty.Formatter(result2.Messages()))
}
req3 := &http.Request{Method: "GET"}
url3, _ := url.Parse("http://www.example.org/?username=foobar&password=foobarfoobar&choise=3")
req3.URL = url3
result3, err := rule.Validate("signin", req3)
if err != nil {
t.Errorf("Failed to validate: %s", err.Error())
return
}
if result3.ValidParam("choise") != "3" {
t.Errorf("RESULT: %v", result)
return
}
}