-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_helpers.go
165 lines (133 loc) · 3.37 KB
/
url_helpers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package helpers
import (
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// Custom URL matcher for outgoing pubnub server requests
func UrlsEqual(expectedString, actualString string,
ignoreKeys, mixedKeys []string) (bool, error) {
expected, err := url.Parse(expectedString)
if err != nil {
return false, err
}
actual, err := url.Parse(actualString)
if err != nil {
return false, err
}
if expected.Scheme != actual.Scheme {
return false, nil
}
if expected.Host != actual.Host {
return false, nil
}
if !PathsEqual(expected.Path, actual.Path, []int{}) {
return false, nil
}
eQuery := expected.Query()
aQuery := actual.Query()
if !QueriesEqual(&eQuery, &aQuery, ignoreKeys, mixedKeys) {
return false, nil
}
return true, nil
}
// PathsEqual mixedPositions - a position of items which can contain unsorted items like
// multiple unsorted channels. If no such positions expected use an empty slice.
// Like in arrays, the first position is 0.
func PathsEqual(expectedString, actualString string,
mixedPositions []int) bool {
if expectedString == actualString {
return true
}
expected := strings.Split(expectedString, "/")
actual := strings.Split(actualString, "/")
if len(actual) != len(expected) {
return false
}
for k, v := range expected {
if !isValueInSlice(k, mixedPositions) {
if v != actual[k] {
return false
}
} else {
expectedItems := strings.Split(v, ",")
actualItems := strings.Split(actual[k], ",")
if len(expectedItems) != len(actualItems) {
return false
}
for _, v := range expectedItems {
if !isValueInSlice(v, expectedItems) {
return false
}
}
}
}
return true
}
func QueriesEqual(expectedString, actualString *url.Values,
ignoreKeys []string, mixedKeys []string) bool {
if expectedString.Encode() == actualString.Encode() {
return true
}
for k, aVal := range *actualString {
if isValueInSlice(k, ignoreKeys) {
continue
}
if eVal, ok := (*expectedString)[k]; ok {
if isValueInSlice(k, mixedKeys) {
eParts := strings.Split(eVal[0], ",")
aParts := strings.Split(aVal[0], ",")
if len(aParts) != len(eParts) {
return false
}
for _, e := range eParts {
if !isValueInSlice(e, aParts) {
return false
}
}
} else {
if aVal[0] != eVal[0] {
return false
}
}
} else {
return false
}
}
for k, _ := range *expectedString {
if val := actualString.Get(k); val == "" {
return false
}
}
return true
}
func isValueInSlice(item interface{}, slice interface{}) bool {
if s, ok := slice.([]string); ok {
for _, v := range s {
if item == v {
return true
}
}
} else if s, ok := slice.([]int); ok {
for _, v := range s {
if item == v {
return true
}
}
}
return false
}
// Assertion wrappers for tests
func AssertPathsEqual(t *testing.T, expectedString, actualString string,
itemsPositions []int) {
match := PathsEqual(expectedString, actualString, itemsPositions)
assert.True(t, match, "Paths are not equal:\nExpected: %s\nActual: %s\n",
expectedString, actualString)
}
func AssertQueriesEqual(t *testing.T, expectedString, actualString *url.Values,
ignoreKeys, mixedKeys []string) {
match := QueriesEqual(expectedString, actualString, ignoreKeys, mixedKeys)
assert.True(t, match, "Queries are not equal:\nExpected: %s\nActual: %s\n",
expectedString, actualString)
}