-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
137 lines (95 loc) · 3.08 KB
/
query_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
package waggy
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/syke99/waggy/internal/resources"
"net/http"
"testing"
)
func TestQuery(t *testing.T) {
// Arrange
r, _ := http.NewRequest(http.MethodGet, resources.TestRoute, nil)
ctx := context.WithValue(r.Context(), resources.QueryParams, resources.TestQueryMap())
r = r.Clone(ctx)
// Act
q := Query(r)
// Assert
assert.IsType(t, &QueryParams{}, q)
assert.Equal(t, 3, len(q.qp))
assert.Equal(t, 1, len(q.qp[resources.TestMapKey1]))
assert.Equal(t, resources.TestMapValue1, q.qp[resources.TestMapKey1])
assert.Equal(t, 2, len(q.qp[resources.TestMapKey2]))
assert.Equal(t, resources.TestMapValue2, q.qp[resources.TestMapKey2])
assert.Equal(t, 0, len(q.qp[resources.TestMapKey3]))
assert.Equal(t, resources.TestMapValue3, q.qp[resources.TestMapKey3])
}
func TestQueryParams_Get(t *testing.T) {
// Arrange
r, _ := http.NewRequest(http.MethodGet, resources.TestRoute, nil)
ctx := context.WithValue(r.Context(), resources.QueryParams, resources.TestQueryMap())
r = r.Clone(ctx)
q := Query(r)
// Act
v := q.Get(resources.TestMapKey1)
// Assert
assert.Equal(t, resources.TestMapValue1[0], v)
}
func TestQueryParams_Get_NoValue(t *testing.T) {
// Arrange
r, _ := http.NewRequest(http.MethodGet, resources.TestRoute, nil)
ctx := context.WithValue(r.Context(), resources.QueryParams, resources.TestQueryMap())
r = r.Clone(ctx)
q := Query(r)
// Act
v := q.Get("agaga")
// Assert
assert.Equal(t, "", v)
}
func TestQueryParams_Set(t *testing.T) {
// Arrange
r, _ := http.NewRequest(http.MethodGet, resources.TestRoute, nil)
ctx := context.WithValue(r.Context(), resources.QueryParams, resources.TestQueryMap())
r = r.Clone(ctx)
q := Query(r)
// Act
q.Set(resources.TestMapKey2, resources.Hello)
// Assert
assert.Equal(t, 1, len(q.qp[resources.TestMapKey2]))
assert.Equal(t, resources.Hello, q.qp[resources.TestMapKey2][0])
}
func TestQueryParams_Add(t *testing.T) {
// Arrange
r, _ := http.NewRequest(http.MethodGet, resources.TestRoute, nil)
ctx := context.WithValue(r.Context(), resources.QueryParams, resources.TestQueryMap())
r = r.Clone(ctx)
q := Query(r)
// Act
q.Add(resources.TestMapKey2, resources.WhereAmI)
// Asset
assert.Equal(t, 3, len(q.qp[resources.TestMapKey2]))
assert.Equal(t, resources.WhereAmI, q.qp[resources.TestMapKey2][2])
}
func TestQueryParams_Del(t *testing.T) {
// Arrange
r, _ := http.NewRequest(http.MethodGet, resources.TestRoute, nil)
ctx := context.WithValue(r.Context(), resources.QueryParams, resources.TestQueryMap())
r = r.Clone(ctx)
q := Query(r)
// Act
q.Del(resources.TestMapKey2)
// Act
assert.Equal(t, 0, len(q.qp[resources.TestMapKey2]))
}
func TestQueryParams_Values(t *testing.T) {
// Arrange
r, _ := http.NewRequest(http.MethodGet, resources.TestRoute, nil)
ctx := context.WithValue(r.Context(), resources.QueryParams, resources.TestQueryMap())
r = r.Clone(ctx)
q := Query(r)
// Act
v := q.Values(resources.TestMapKey2)
// Assert
assert.Equal(t, 2, len(v))
assert.Equal(t, resources.Hello, v[0])
assert.Equal(t, resources.Goodbye, v[1])
}