-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_test.go
145 lines (126 loc) · 3.51 KB
/
url_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package tea
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi"
"github.com/stretchr/testify/assert"
)
func newRequest() (*http.Request, *chi.Context) {
req := httptest.NewRequest("GET", "/", nil)
ctx := chi.NewRouteContext()
ctx.RouteMethod = "GET"
ctx.RoutePath = "/"
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, ctx))
return req, ctx
}
func TestURLIntErrorsWhenParamDoesNotExist(t *testing.T) {
r, _ := newRequest()
i, err := URLInt(r, "id", "")
assert.Equal(t, ErrNoURLParam, err)
assert.EqualValues(t, 0, i)
}
func TestURLIntErrorsWhenNotAnInt(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "a")
i, err := URLInt(r, "id", "")
assert.Error(t, err)
assert.EqualValues(t, 0, i)
}
func TestURLIntErrorsWhenValidationFails(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "10")
_, err := URLInt(r, "id", "gt=10")
assert.Error(t, err)
}
func TestURLIntReturnsTheValue(t *testing.T) {
const expected = 10
r, ctx := newRequest()
ctx.URLParams.Add("id", fmt.Sprintf("%d", expected))
i, err := URLInt(r, "id", "")
assert.NoError(t, err)
assert.EqualValues(t, expected, i)
}
func TestURLInt64ErrorsWhenParamDoesNotExist(t *testing.T) {
r, _ := newRequest()
i, err := URLInt64(r, "id", "")
assert.Equal(t, ErrNoURLParam, err)
assert.EqualValues(t, 0, i)
}
func TestURLInt64ErrorsWhenNotAnInt64(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "a")
i, err := URLInt64(r, "id", "")
assert.Error(t, err)
assert.EqualValues(t, 0, i)
}
func TestURLInt64ErrorsWhenValidationFails(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "10")
_, err := URLInt64(r, "id", "gt=10")
assert.Error(t, err)
}
func TestURLInt64ReturnsTheValue(t *testing.T) {
const expected = 10
r, ctx := newRequest()
ctx.URLParams.Add("id", fmt.Sprintf("%d", expected))
i, err := URLInt64(r, "id", "")
assert.NoError(t, err)
assert.EqualValues(t, expected, i)
}
func TestURLUintErrorsWhenParamDoesNotExist(t *testing.T) {
r, _ := newRequest()
i, err := URLUint(r, "id", "")
assert.Equal(t, ErrNoURLParam, err)
assert.EqualValues(t, 0, i)
}
func TestURLUintErrorsWhenNotAUint(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "a")
i, err := URLUint(r, "id", "")
assert.Error(t, err)
assert.EqualValues(t, 0, i)
}
func TestURLUintErrorsWhenValidationFails(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "10")
_, err := URLUint(r, "id", "gt=10")
assert.Error(t, err)
}
func TestURLUintReturnsTheValue(t *testing.T) {
const expected = 10
r, ctx := newRequest()
ctx.URLParams.Add("id", fmt.Sprintf("%d", expected))
i, err := URLUint(r, "id", "")
assert.NoError(t, err)
assert.EqualValues(t, expected, i)
}
func TestURLFloatErrorsWhenParamDoesNotExist(t *testing.T) {
r, _ := newRequest()
i, err := URLFloat(r, "id", "")
assert.Equal(t, ErrNoURLParam, err)
assert.EqualValues(t, 0, i)
}
func TestURLFloatErrorsWhenNotAFloat(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "a")
i, err := URLFloat(r, "id", "")
assert.Error(t, err)
assert.EqualValues(t, 0, i)
}
func TestURLFloatErrorsWhenValidationFails(t *testing.T) {
r, ctx := newRequest()
ctx.URLParams.Add("id", "10")
_, err := URLFloat(r, "id", "gt=10")
assert.Error(t, err)
}
func TestURLFloatReturnsTheValue(t *testing.T) {
const expected = 10
r, ctx := newRequest()
ctx.URLParams.Add("id", fmt.Sprintf("%d", expected))
i, err := URLFloat(r, "id", "")
assert.NoError(t, err)
assert.EqualValues(t, expected, i)
}