38 lines
1020 B
Go
38 lines
1020 B
Go
package main
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestRoutes(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
path string
|
|
code int
|
|
}{{"/", 200}, {"/healthz", 200}, {"/data/stream.json", 200}, {"/missing", 404}, {"/etc/passwd", 404}} {
|
|
r := httptest.NewRequest("GET", tc.path, nil)
|
|
w := httptest.NewRecorder()
|
|
handler().ServeHTTP(w, r)
|
|
if w.Code != tc.code {
|
|
t.Fatalf("%s: %d", tc.path, w.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCompressionAndCache(t *testing.T) {
|
|
r := httptest.NewRequest("GET", "/data/stream.json", nil)
|
|
r.Header.Set("Accept-Encoding", "gzip")
|
|
w := httptest.NewRecorder()
|
|
handler().ServeHTTP(w, r)
|
|
if w.Header().Get("Content-Encoding") != "gzip" || w.Header().Get("Cache-Control") != "no-cache" {
|
|
t.Fatal(w.Header())
|
|
}
|
|
r = httptest.NewRequest("GET", "/data/stream.json", nil)
|
|
r.Header.Set("Accept-Encoding", "gzip;q=0")
|
|
w = httptest.NewRecorder()
|
|
handler().ServeHTTP(w, r)
|
|
if w.Header().Get("Content-Encoding") != "" {
|
|
t.Fatal("不接受压缩时应返回原始资源")
|
|
}
|
|
}
|