forked from WeidiDeng/caddy-cloudflare-ip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaddyfile_test.go
99 lines (80 loc) · 2.17 KB
/
caddyfile_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package caddy_ip_list
import (
"context"
"testing"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func TestDefault(t *testing.T) {
testDefault(t, `url`)
testDefault(t, `list { }`)
}
func testDefault(t *testing.T, input string) {
d := caddyfile.NewTestDispenser(input)
r := URLIPRange{}
err := r.UnmarshalCaddyfile(d)
if err != nil {
t.Errorf("unmarshal error for %q: %v", input, err)
}
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
defer cancel()
err = r.Provision(ctx)
if err != nil {
t.Errorf("error provisioning %q: %v", input, err)
}
}
func TestUnmarshal(t *testing.T) {
input := `
list {
url https://www.cloudflare.com/ips-v4
interval 1.5h
timeout 30s
}`
d := caddyfile.NewTestDispenser(input)
r := URLIPRange{}
err := r.UnmarshalCaddyfile(d)
if err != nil {
t.Errorf("unmarshal error: %v", err)
}
expectedInterval := caddy.Duration(90 * time.Minute)
if expectedInterval != r.Interval {
t.Errorf("incorrect interval: expected %v, got %v", expectedInterval, r.Interval)
}
expectedTimeout := caddy.Duration(30 * time.Second)
if expectedTimeout != r.Timeout {
t.Errorf("incorrect timeout: expected %v, got %v", expectedTimeout, r.Timeout)
}
}
// Simulates being nested in another block.
func TestUnmarshalNested(t *testing.T) {
input := `{
list {
url https://www.cloudflare.com/ips-v4
interval 1.5h
timeout 30s
}
other_module 10h
}`
d := caddyfile.NewTestDispenser(input)
// Enter the outer block.
d.Next()
d.NextBlock(d.Nesting())
r := URLIPRange{}
err := r.UnmarshalCaddyfile(d)
if err != nil {
t.Errorf("unmarshal error: %v", err)
}
expectedInterval := caddy.Duration(90 * time.Minute)
if expectedInterval != r.Interval {
t.Errorf("incorrect interval: expected %v, got %v", expectedInterval, r.Interval)
}
expectedTimeout := caddy.Duration(30 * time.Second)
if expectedTimeout != r.Timeout {
t.Errorf("incorrect timeout: expected %v, got %v", expectedTimeout, r.Timeout)
}
d.Next()
if d.Val() != "other_module" {
t.Errorf("cursor at unexpected position, expected 'other_module', got %v", d.Val())
}
}