-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
127 lines (123 loc) · 2.54 KB
/
helper_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMapQueries(t *testing.T) {
const host = "http://localhost"
tests := []struct {
name string
inputPath string
want *FizzBuzzParams
}{
{
"With empty input",
"",
&FizzBuzzParams{},
},
{
"With good path input",
"/?int1=3&int2=5&limit=100&str1=fizz&str2=buzz",
&FizzBuzzParams{3, 5, 100, "fizz", "buzz"},
},
{
"With bad int1 query parameter",
"/?int1=@&int2=5&limit=100&str1=fizz&str2=buzz",
&FizzBuzzParams{0, 5, 100, "fizz", "buzz"},
},
{
"Without int1 query parameter",
"/?int2=5&limit=100&str1=fizz&str2=buzz",
&FizzBuzzParams{0, 5, 100, "fizz", "buzz"},
},
{
"Without int2 query parameter",
"/?int1=3&limit=100&str1=fizz&str2=buzz",
&FizzBuzzParams{3, 0, 100, "fizz", "buzz"},
},
{
"Without limit query parameter",
"/?int1=3&int2=5&str1=fizz&str2=buzz",
&FizzBuzzParams{3, 5, 0, "fizz", "buzz"},
},
{
"Without str1 query parameter",
"/?int1=3&int2=5&limit=100&str2=buzz",
&FizzBuzzParams{3, 5, 100, "", "buzz"},
},
{
"Without str2 query parameter",
"/?int1=3&int2=5&limit=100&str1=fizz",
&FizzBuzzParams{3, 5, 100, "fizz", ""},
},
{
"Without str1 and str2 queries parameters",
"/?int1=3&int2=5&limit=100",
&FizzBuzzParams{3, 5, 100, "", ""},
},
{
"Without int1 and int2 queries parameters",
"/?limit=100&str1=fizz&str2=buzz",
&FizzBuzzParams{0, 0, 100, "fizz", "buzz"},
},
{
"Without int1, int2 and limit queries parameters",
"/?str1=fizz&str2=buzz",
&FizzBuzzParams{0, 0, 0, "fizz", "buzz"},
},
{
"Without int1, int2, limit and str1 queries parameters",
"/?str2=buzz",
&FizzBuzzParams{0, 0, 0, "", "buzz"},
},
}
for _, tc := range tests {
url := fmt.Sprintf("%s%s", host, tc.inputPath)
req, err := http.NewRequest(http.MethodGet, url, nil)
assert.NoError(t, err)
t.Run(tc.name, func(t *testing.T) {
got := mapQueries(req)
assert.Equal(t, tc.want, got)
})
}
}
func TestParseInt(t *testing.T) {
tests := []struct {
name string
input string
want int
expError bool
}{
{
"With empty input",
"",
0,
true,
},
{
"With good input string",
"10",
10,
false,
},
{
"With bad input string",
"@",
0,
true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := parseInt(tc.input)
if tc.expError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tc.want, got)
})
}
}