-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
200 lines (159 loc) · 4.59 KB
/
main_test.go
File metadata and controls
200 lines (159 loc) · 4.59 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"sync"
"testing"
"github.com/gorilla/mux"
)
func resetQuotes() {
quotes = nil
quotesOnce = sync.Once{}
quotesSource = ""
}
func setQuotesDirectly(q []interface{}) {
quotes = q
quotesOnce.Do(func() {}) // mark as already loaded
}
func setupTestRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", getIndex).Methods("GET")
r.HandleFunc("/quotes", getAllQuotes).Methods("GET")
r.HandleFunc("/quotes/{index}", getQuoteByIndex).Methods("GET")
return r
}
func TestGetIndex(t *testing.T) {
r := setupTestRouter()
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
var result map[string]string
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if _, ok := result["GET /"]; !ok {
t.Error("Expected 'GET /' in response")
}
if _, ok := result["GET /quotes"]; !ok {
t.Error("Expected 'GET /quotes' in response")
}
if _, ok := result["GET /quotes/{index}"]; !ok {
t.Error("Expected 'GET /quotes/{index}' in response")
}
}
func TestLoadQuotesFromFile(t *testing.T) {
resetQuotes()
tmpFile := filepath.Join(t.TempDir(), "test_quotes.json")
testData := []string{"Quote 1", "Quote 2", "Quote 3"}
data, _ := json.Marshal(testData)
if err := os.WriteFile(tmpFile, data, 0644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
loadQuotesFromFile(tmpFile)
if len(quotes) != 3 {
t.Errorf("Expected 3 quotes, got %d", len(quotes))
}
}
func TestLoadQuotesFromFileNotFound(t *testing.T) {
resetQuotes()
_, err := os.Stat("/nonexistent/file.json")
if !os.IsNotExist(err) {
t.Skip("Test file unexpectedly exists")
}
}
func TestGetAllQuotes(t *testing.T) {
resetQuotes()
setQuotesDirectly([]interface{}{"Quote A", "Quote B"})
r := setupTestRouter()
req := httptest.NewRequest("GET", "/quotes", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
var result []interface{}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if len(result) != 2 {
t.Errorf("Expected 2 quotes, got %d", len(result))
}
}
func TestGetQuoteByIndex(t *testing.T) {
resetQuotes()
setQuotesDirectly([]interface{}{"First", "Second", "Third"})
r := setupTestRouter()
tests := []struct {
name string
index string
wantStatus int
wantQuote string
}{
{"valid index 0", "0", http.StatusOK, "First"},
{"valid index 1", "1", http.StatusOK, "Second"},
{"valid index 2", "2", http.StatusOK, "Third"},
{"index out of bounds", "99", http.StatusNotFound, ""},
{"negative index", "-1", http.StatusNotFound, ""},
{"invalid index", "abc", http.StatusNotFound, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", "/quotes/"+tt.index, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != tt.wantStatus {
t.Errorf("Expected status %d, got %d", tt.wantStatus, w.Code)
}
if tt.wantStatus == http.StatusOK {
var result string
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if result != tt.wantQuote {
t.Errorf("Expected %q, got %q", tt.wantQuote, result)
}
}
})
}
}
func TestGetAllQuotesContentType(t *testing.T) {
resetQuotes()
setQuotesDirectly([]interface{}{"Test"})
r := setupTestRouter()
req := httptest.NewRequest("GET", "/quotes", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
contentType := w.Header().Get("Content-Type")
if contentType != "application/json" {
t.Errorf("Expected Content-Type application/json, got %s", contentType)
}
}
func TestGetQuoteByIndexContentType(t *testing.T) {
resetQuotes()
setQuotesDirectly([]interface{}{"Test"})
r := setupTestRouter()
req := httptest.NewRequest("GET", "/quotes/0", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
contentType := w.Header().Get("Content-Type")
if contentType != "application/json" {
t.Errorf("Expected Content-Type application/json, got %s", contentType)
}
}
func TestEmptyQuotes(t *testing.T) {
resetQuotes()
setQuotesDirectly([]interface{}{})
r := setupTestRouter()
req := httptest.NewRequest("GET", "/quotes/0", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusNotFound {
t.Errorf("Expected status 404 for empty quotes, got %d", w.Code)
}
}