-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform_test.go
More file actions
106 lines (102 loc) · 2.64 KB
/
form_test.go
File metadata and controls
106 lines (102 loc) · 2.64 KB
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
100
101
102
103
104
105
106
package handlers
import (
"net/http"
"testing"
"time"
"github.com/drduh/gone/settings"
)
// TestParseFormInt tests integer form values are parsed.
func TestParseFormInt(t *testing.T) {
def := 1
maximum := 100
tests := []struct {
name string
query string
field string
def int
want int
maximum int
}{
{"valid", "/?downloads=5", "downloads",
def, 5, maximum},
{"space", "/?downloads= 5 ", "downloads",
def, 5, maximum},
{"missing", "/", "downloads",
def, 1, maximum},
{"invalid", "/?downloads=none", "downloads",
def, 1, maximum},
{"zero", "/?downloads=0", "downloads",
def, def, maximum},
{"negative", "/?downloads=-1", "downloads",
def, def, maximum},
{"fraction", "/?downloads=3.5", "downloads",
def, def, maximum},
{"large", "/?downloads=101", "downloads",
def, maximum, maximum},
{"xlarge", "/?downloads=999999999999999999999",
"downloads", def, def, maximum}, // overflows int64
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
req, err := http.NewRequest("GET", tc.query, nil)
if err != nil {
t.Fatal(err)
}
got := parseFormInt(req, tc.field, tc.def, tc.maximum)
if got != tc.want {
t.Fatalf("parseFormInt(%q) = %d; want %d",
tc.query, got, tc.want)
}
})
}
}
// TestParseFormDuration tests duration form values are parsed.
func TestParseFormDuration(t *testing.T) {
def := 1 * time.Hour
maximum := 8 * 24 * time.Hour
tests := []struct {
name string
query string
field string
def time.Duration
want time.Duration
maximum time.Duration
}{
{"valid", "/?duration=1h30m", "duration",
def, 90 * time.Minute, maximum},
{"space", "/?duration= 15m ", "duration",
def, 15 * time.Minute, maximum},
{"missing", "/", "duration",
def, def, maximum},
{"invalid", "/?duration=none", "duration",
def, def, maximum},
{"zero", "/?duration=0s", "duration",
def, def, maximum},
{"negative", "/?duration=-1h", "duration",
def, def, maximum},
{"fraction", "/?duration=1.5h", "duration",
def, 90 * time.Minute, maximum},
{"large", "/?duration=9999h", "duration",
def, maximum, maximum},
{"xlarge", "/?duration=99999999999h", "duration",
def, def, maximum}, // overflows int64
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
req, err := http.NewRequest("GET", tc.query, nil)
if err != nil {
t.Fatal(err)
}
got := parseFormDuration(req, tc.field, tc.def,
settings.Duration{Duration: tc.maximum})
if got != tc.want {
t.Fatalf("parseFormDuration(%q) = %v; want %v",
tc.query, got, tc.want)
}
})
}
}