-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeprecated_test.go
More file actions
189 lines (167 loc) · 5.31 KB
/
Copy pathdeprecated_test.go
File metadata and controls
189 lines (167 loc) · 5.31 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
package nasa
import (
"html/template"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
type httpTestList struct {
method string
path string
code int
contains string
}
// apodMockHandler returns an http.HandlerFunc that serves different fixtures
// depending on whether the request looks like a Today call (single object) or
// a Random call (array, triggered by the "count" query param).
func apodMockHandler(t *testing.T, todayFixture, randomFixture string) http.HandlerFunc {
t.Helper()
todayData := loadFixture(t, todayFixture)
randomData := loadFixture(t, randomFixture)
return func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/planetary/apod") {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
if r.URL.Query().Get("count") != "" {
_, _ = w.Write(randomData)
} else {
_, _ = w.Write(todayData)
}
}
}
func TestDeprecatedHandleIndex(t *testing.T) {
// Wire up a mock client so APODToday() does not hit the real API.
c := setupMockServer(t, apodMockHandler(t, "apod_single.json", "apod_range.json"))
setDefaultClientForTest(t, c)
var err error
tmpl, err := template.New("tmpl").Parse(deprecatedTmplHTML)
if err != nil {
t.Fatal(err)
}
testList := []httpTestList{
{"GET", "/", http.StatusOK, "#explanation"},
{"GET", "/abcd", http.StatusNotFound, ""},
}
for _, v := range testList {
req, err := http.NewRequest(v.method, v.path, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := &deprecatedIndexHandler{tmpl: tmpl}
handler.ServeHTTP(rr, req)
if rr.Code != v.code {
t.Errorf("deprecatedIndexHandler %s returned wrong status got %d, want %d", v.path, rr.Code, v.code)
}
if v.contains != "" && !strings.Contains(rr.Body.String(), v.contains) {
t.Errorf("deprecatedIndexHandler %s missing expected text %q", v.path, v.contains)
}
}
}
func TestDeprecatedHandleIndex_Fallback(t *testing.T) {
// Mock server that always fails, forcing the fallback path.
c := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"code":500,"msg":"internal"}`, http.StatusInternalServerError)
})
setDefaultClientForTest(t, c)
var err error
tmpl, err := template.New("tmpl").Parse(deprecatedTmplHTML)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := &deprecatedIndexHandler{tmpl: tmpl}
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("fallback: got status %d, want %d", rr.Code, http.StatusOK)
}
if !strings.Contains(rr.Body.String(), "temporarily unavailable") {
t.Error("fallback: body should contain fallback explanation text")
}
}
func TestDeprecatedRandomHandler(t *testing.T) {
// Wire up a mock client so RandomAPOD() does not hit the real API.
c := setupMockServer(t, apodMockHandler(t, "apod_single.json", "apod_range.json"))
setDefaultClientForTest(t, c)
tmpl, err := template.New("tmpl").Parse(deprecatedTmplHTML)
if err != nil {
t.Fatal(err)
}
testList := []httpTestList{
{"GET", "/abcd", http.StatusNotFound, ""},
{"GET", "/random-apod/abcd", http.StatusNotFound, ""},
{"GET", "/random-apod/", http.StatusOK, "#explanation"},
}
for _, v := range testList {
req, err := http.NewRequest(v.method, v.path, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := &deprecatedRandomHandler{
lastUpdate: time.Now().Add(-10 * time.Hour),
cachedApod: &Image{},
tmpl: tmpl,
}
handler.ServeHTTP(rr, req)
if rr.Code != v.code {
t.Errorf("deprecatedRandomHandler %s returned wrong status got %d, want %d", v.path, rr.Code, v.code)
}
if v.contains != "" && !strings.Contains(rr.Body.String(), v.contains) {
t.Errorf("deprecatedRandomHandler %s missing expected text %q", v.path, v.contains)
}
}
}
func TestDeprecatedRandomHandler_Fallback(t *testing.T) {
// Mock server that always fails, forcing the fallback path.
c := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"code":500,"msg":"internal"}`, http.StatusInternalServerError)
})
setDefaultClientForTest(t, c)
tmpl, err := template.New("tmpl").Parse(deprecatedTmplHTML)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("GET", "/random-apod/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := &deprecatedRandomHandler{
lastUpdate: time.Now().Add(-10 * time.Hour),
cachedApod: &Image{}, // empty, so fallback will be used
tmpl: tmpl,
}
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Errorf("fallback: got status %d, want %d", rr.Code, http.StatusOK)
}
if !strings.Contains(rr.Body.String(), "temporarily unavailable") {
t.Error("fallback: body should contain fallback explanation text")
}
}
// Compile-time assertion: NewServer signature must remain stable.
func TestNewServer_SignatureCompiles(t *testing.T) {
assertType[func(string) (*http.Server, error)](NewServer)
}
// Compile-time assertion: TmplData must remain an exported type with Render.
func TestTmplData_SignatureCompiles(t *testing.T) {
var td TmplData
td.Apod = Image{}
_ = td.Page
_ = td.Title
_ = td.SD
_ = td.AutoReload
_ = td.Legacy
_ = td.IsYoutube
_ = td.AutoReloadInterval
assertType[func(http.ResponseWriter)](td.Render)
}