-
Notifications
You must be signed in to change notification settings - Fork 158
/
util_test.go
159 lines (150 loc) · 4.8 KB
/
util_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
)
func TestEncodeURL(t *testing.T) {
testCases := []struct {
actual string
expected string
}{
{"/entries?category=Science%20%26%20Math", "/entries?category=Science%20%26%20Math"},
{"/entries?category=Science & Math", "/entries?category=Science & Math"},
{"/entries?category=Science%20Math", "/entries?category=Science%20Math"},
}
for _, tc := range testCases {
req, _ := http.NewRequest("GET", tc.actual, nil)
rr := httptest.NewRecorder()
encodeURL(rr, req,
http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {}))
if req.URL.String() != tc.expected {
t.Errorf("incorrect encoding: expected %q, got %q", tc.expected, tc.actual)
}
}
}
func TestGetCategories(t *testing.T) {
actual := parseCategories([]Entry{
Entry{Category: "A"},
Entry{Category: "B"},
Entry{Category: "B"},
Entry{Category: "C"},
Entry{Category: "D"},
})
expected := []string{"A", "B", "C", "D"}
if len(actual) != len(expected) {
t.Fatalf("bad parsing: expected %v, got %v", expected, actual)
}
for i := 0; i < len(expected); i++ {
if actual[i] != expected[i] {
t.Errorf("bad element: expected %q, got %q", actual[i], expected[i])
}
}
}
func TestCheckEntryMatches(t *testing.T) {
entry := Entry{
API: "examplesAsAService",
Description: "provide classic examples",
Auth: "apiKey",
HTTPS: true,
Cors: "Unknown",
Link: "http://www.example.com",
Category: "Development",
}
entryEmptyAuth := Entry{
API: "examplesAsAService",
Description: "provide classic examples",
Auth: "",
HTTPS: true,
Cors: "Unknown",
Link: "http://www.example.com",
Category: "Development",
}
testCases := []struct {
name string
entry Entry
search *SearchRequest
shouldPass bool
}{
{"Full search", entry, &SearchRequest{}, true},
{"Desc valid full", entry, &SearchRequest{Description: "provide classic examples"}, true},
{"Desc valid match", entry, &SearchRequest{Description: "provide class"}, true},
{"Desc invalid", entry, &SearchRequest{Description: "this will not match"}, false},
{"Auth valid full", entry, &SearchRequest{Auth: "apiKey"}, true},
{"Auth valid match", entry, &SearchRequest{Auth: "apiK"}, true},
{"Auth empty", entry, &SearchRequest{Auth: ""}, true},
{"Auth empty entry", entryEmptyAuth, &SearchRequest{Auth: ""}, true},
{"Auth null", entry, &SearchRequest{Auth: "null"}, false},
{"Auth null empty entry", entryEmptyAuth, &SearchRequest{Auth: "null"}, true},
{"Auth invalid", entry, &SearchRequest{Auth: "foo"}, false},
{"HTTPS true", entry, &SearchRequest{HTTPS: "1"}, true},
{"HTTPS false", entry, &SearchRequest{HTTPS: "false"}, false},
{"CORS valid full", entry, &SearchRequest{Cors: "unknown"}, true},
{"CORS valid match", entry, &SearchRequest{Cors: "unk"}, true},
{"CORS invalid", entry, &SearchRequest{Cors: "bar"}, false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if checkEntryMatches(tc.entry, tc.search) != tc.shouldPass {
if tc.shouldPass {
t.Errorf("was expecting to pass, but failed")
} else {
t.Errorf("was expecting to fail, but passed")
}
}
})
}
}
func TestProcessSearchRequestToMatchingEntries(t *testing.T) {
apiList.Entries = []Entry{
Entry{
API: "examplesAsAService",
Description: "provide classic examples",
Auth: "apiKey",
HTTPS: true,
Cors: "Unknown",
Link: "http://www.example.com",
Category: "Development",
},
Entry{
API: "examplesAsAServiceToo",
Description: "provide classic examples",
Auth: "",
HTTPS: true,
Cors: "Yes",
Link: "http://www.example.com",
Category: "Development",
},
}
testCases := []struct {
name string
query string
expected []Entry
}{
{"null auth", "?auth=null", []Entry{apiList.Entries[1]}},
{"apiKey auth", "?auth=apiKey", []Entry{apiList.Entries[0]}},
{"multi-key query", "?auth=null&description=example", []Entry{apiList.Entries[1]}},
{"multi-key query full match", "?category=development&description=example", apiList.Entries},
{"fully-matching description", "?description=example", apiList.Entries},
{"unkwown cors", "?cors=unknown", []Entry{apiList.Entries[0]}},
{"yes cors", "?cors=yes", []Entry{apiList.Entries[1]}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
u, err := url.Parse(tc.query)
if err != nil {
t.Fatal(err)
}
req := &http.Request{URL: u}
actual, err := processSearchRequestToMatchingEntries(req)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(actual, tc.expected) {
t.Errorf("unexpected matched entries:\nreceived %+v\nexpected %+v\n", actual, tc.expected)
}
})
}
}