fix: 修复 TLS ConnectionState、SPA 回退、健康检查与监听小问题
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package httpx_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"git.asio.asia/nixevol/NixMsg/internal/httpx"
|
||||
)
|
||||
|
||||
func TestSPAFallbackAndMissingAsset(t *testing.T) {
|
||||
fsys := fstest.MapFS{
|
||||
"index.html": &fstest.MapFile{Data: []byte("<html>app</html>")},
|
||||
"assets/app.js": &fstest.MapFile{Data: []byte("console.log(1)")},
|
||||
}
|
||||
h := httpx.SPA(fsys)
|
||||
|
||||
t.Run("frontend route", func(t *testing.T) {
|
||||
res := httptest.NewRecorder()
|
||||
h.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/endpoints", nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d", res.Code)
|
||||
}
|
||||
ct := res.Header().Get("Content-Type")
|
||||
if !strings.Contains(ct, "text/html") {
|
||||
t.Fatalf("content-type=%q", ct)
|
||||
}
|
||||
if !strings.Contains(res.Body.String(), "app") {
|
||||
t.Fatalf("body=%q", res.Body.String())
|
||||
}
|
||||
if res.Header().Get("Cache-Control") != "no-cache" {
|
||||
t.Fatalf("cache-control=%q", res.Header().Get("Cache-Control"))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("missing js", func(t *testing.T) {
|
||||
res := httptest.NewRecorder()
|
||||
h.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/assets/nope.js", nil))
|
||||
if res.Code != http.StatusNotFound {
|
||||
t.Fatalf("status=%d", res.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("directory no listing", func(t *testing.T) {
|
||||
res := httptest.NewRecorder()
|
||||
h.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/assets/", nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d", res.Code)
|
||||
}
|
||||
if strings.Contains(res.Body.String(), "app.js") {
|
||||
t.Fatal("directory listing leaked")
|
||||
}
|
||||
if !strings.Contains(res.Body.String(), "app") {
|
||||
t.Fatalf("want spa index, body=%q", res.Body.String())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("existing file", func(t *testing.T) {
|
||||
res := httptest.NewRecorder()
|
||||
h.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/assets/app.js", nil))
|
||||
if res.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d", res.Code)
|
||||
}
|
||||
if res.Body.String() != "console.log(1)" {
|
||||
t.Fatalf("body=%q", res.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user