54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
|
|
package hotime
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
. "code.hoteas.com/golang/hotime/cache"
|
||
|
|
. "code.hoteas.com/golang/hotime/common"
|
||
|
|
"code.hoteas.com/golang/hotime/log"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestStaticFile_CacheControlPublic_WithLogLevel1
|
||
|
|
// 静态文件在 logLevel>=1 时仍应 Cache-Control: public(与日志级别解耦)。
|
||
|
|
func TestStaticFile_CacheControlPublic_WithLogLevel1(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
body := []byte("<!doctype html><title>static-cache</title>")
|
||
|
|
if err := os.WriteFile(filepath.Join(dir, "hello.html"), body, 0o644); err != nil {
|
||
|
|
t.Fatalf("写静态文件失败: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
app := &Application{
|
||
|
|
Config: Map{
|
||
|
|
"logLevel": 1,
|
||
|
|
"tpt": dir,
|
||
|
|
"sessionName": "HOTIME",
|
||
|
|
"defFile": Slice{"index.html"},
|
||
|
|
"crossDomain": "",
|
||
|
|
},
|
||
|
|
Log: log.NewLogger(1, "", 0),
|
||
|
|
MethodRouter: MethodRouter{},
|
||
|
|
HoTimeCache: &HoTimeCache{},
|
||
|
|
}
|
||
|
|
|
||
|
|
req := httptest.NewRequest(http.MethodGet, "/hello.html", nil)
|
||
|
|
req.RequestURI = "/hello.html"
|
||
|
|
w := httptest.NewRecorder()
|
||
|
|
app.ServeHTTP(w, req)
|
||
|
|
|
||
|
|
if w.Code != http.StatusOK {
|
||
|
|
t.Fatalf("期望 200,实际 %d loc=%q cc=%q body=%q", w.Code, w.Header().Get("Location"), w.Header().Get("Cache-Control"), w.Body.String())
|
||
|
|
}
|
||
|
|
cc := w.Header().Get("Cache-Control")
|
||
|
|
if !strings.EqualFold(cc, "public") {
|
||
|
|
t.Fatalf("期望 Cache-Control=public,实际 %q", cc)
|
||
|
|
}
|
||
|
|
if !strings.Contains(w.Body.String(), "static-cache") {
|
||
|
|
t.Fatalf("响应体未包含静态内容: %q", w.Body.String())
|
||
|
|
}
|
||
|
|
}
|