@@ -4,20 +4,40 @@ import (
44 "net/http"
55 "testing"
66 "time"
7+
8+ "github.com/drduh/gone/settings"
79)
810
911// TestParseFormInt tests integer form values are parsed.
1012func TestParseFormInt (t * testing.T ) {
13+ def := 1
14+ maximum := 100
1115 tests := []struct {
12- name string
13- query string
14- field string
15- def int
16- want int
16+ name string
17+ query string
18+ field string
19+ def int
20+ want int
21+ maximum int
1722 }{
18- {"valid" , "/?downloads=5" , "downloads" , 10 , 5 },
19- {"missing" , "/" , "downloads" , 10 , 10 },
20- {"invalid" , "/?downloads=none" , "downloads" , 10 , 10 },
23+ {"valid" , "/?downloads=5" , "downloads" ,
24+ def , 5 , maximum },
25+ {"space" , "/?downloads= 5 " , "downloads" ,
26+ def , 5 , maximum },
27+ {"missing" , "/" , "downloads" ,
28+ def , 1 , maximum },
29+ {"invalid" , "/?downloads=none" , "downloads" ,
30+ def , 1 , maximum },
31+ {"zero" , "/?downloads=0" , "downloads" ,
32+ def , def , maximum },
33+ {"negative" , "/?downloads=-1" , "downloads" ,
34+ def , def , maximum },
35+ {"fraction" , "/?downloads=3.5" , "downloads" ,
36+ def , def , maximum },
37+ {"large" , "/?downloads=101" , "downloads" ,
38+ def , maximum , maximum },
39+ {"xlarge" , "/?downloads=999999999999999999999" ,
40+ "downloads" , def , def , maximum }, // overflows int64
2141 }
2242 for _ , tc := range tests {
2343 tc := tc
@@ -27,7 +47,7 @@ func TestParseFormInt(t *testing.T) {
2747 if err != nil {
2848 t .Fatal (err )
2949 }
30- got := parseFormInt (req , tc .field , tc .def )
50+ got := parseFormInt (req , tc .field , tc .def , tc . maximum )
3151 if got != tc .want {
3252 t .Fatalf ("parseFormInt(%q) = %d; want %d" ,
3353 tc .query , got , tc .want )
@@ -38,21 +58,34 @@ func TestParseFormInt(t *testing.T) {
3858
3959// TestParseFormDuration tests duration form values are parsed.
4060func TestParseFormDuration (t * testing.T ) {
61+ def := 1 * time .Hour
62+ maximum := 8 * 24 * time .Hour
4163 tests := []struct {
42- name string
43- query string
44- field string
45- def time.Duration
46- want time.Duration
64+ name string
65+ query string
66+ field string
67+ def time.Duration
68+ want time.Duration
69+ maximum time.Duration
4770 }{
4871 {"valid" , "/?duration=1h30m" , "duration" ,
49- 2 * time .Hour , 90 * time .Minute },
72+ def , 90 * time .Minute , maximum },
73+ {"space" , "/?duration= 15m " , "duration" ,
74+ def , 15 * time .Minute , maximum },
5075 {"missing" , "/" , "duration" ,
51- 2 * time . Hour , 2 * time . Hour },
76+ def , def , maximum },
5277 {"invalid" , "/?duration=none" , "duration" ,
53- 2 * time .Hour , 2 * time .Hour },
54- {"seconds" , "/?duration=123s" , "duration" ,
55- 2 * time .Hour , 2 * time .Minute + 3 * time .Second },
78+ def , def , maximum },
79+ {"zero" , "/?duration=0s" , "duration" ,
80+ def , def , maximum },
81+ {"negative" , "/?duration=-1h" , "duration" ,
82+ def , def , maximum },
83+ {"fraction" , "/?duration=1.5h" , "duration" ,
84+ def , 90 * time .Minute , maximum },
85+ {"large" , "/?duration=9999h" , "duration" ,
86+ def , maximum , maximum },
87+ {"xlarge" , "/?duration=99999999999h" , "duration" ,
88+ def , def , maximum }, // overflows int64
5689 }
5790 for _ , tc := range tests {
5891 tc := tc
@@ -62,7 +95,8 @@ func TestParseFormDuration(t *testing.T) {
6295 if err != nil {
6396 t .Fatal (err )
6497 }
65- got := parseFormDuration (req , tc .field , tc .def )
98+ got := parseFormDuration (req , tc .field , tc .def ,
99+ settings.Duration {Duration : tc .maximum })
66100 if got != tc .want {
67101 t .Fatalf ("parseFormDuration(%q) = %v; want %v" ,
68102 tc .query , got , tc .want )
0 commit comments