38 lines
1012 B
Go
38 lines
1012 B
Go
package main
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"net/http"
|
|||
|
|
"net/http/httptest"
|
|||
|
|
"strings"
|
|||
|
|
"testing"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// TestHandlerServesAppShell 在进程内验证 go-app Handler 能返回 app shell:
|
|||
|
|
// - 根路径返回 200;
|
|||
|
|
// - home 组件被服务端预渲染进 HTML(最小页面可见);
|
|||
|
|
// - 引导 wasm 的脚本(wasm_exec.js / app.js)已注入。
|
|||
|
|
//
|
|||
|
|
// 用 httptest 而非真实网络,避免依赖本机端口。
|
|||
|
|
func TestHandlerServesAppShell(t *testing.T) {
|
|||
|
|
h := newHandler()
|
|||
|
|
|
|||
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|||
|
|
rec := httptest.NewRecorder()
|
|||
|
|
h.ServeHTTP(rec, req)
|
|||
|
|
|
|||
|
|
if rec.Code != http.StatusOK {
|
|||
|
|
t.Fatalf("根路径状态 = %d, 期望 200", rec.Code)
|
|||
|
|
}
|
|||
|
|
body := rec.Body.String()
|
|||
|
|
for _, want := range []string{
|
|||
|
|
`lang="zh"`, // 语言
|
|||
|
|
"走遍美国 · 精听", // home 组件预渲染内容
|
|||
|
|
"/wasm_exec.js", // wasm 运行时
|
|||
|
|
"/app.js", // 加载 app.wasm 的引导脚本
|
|||
|
|
} {
|
|||
|
|
if !strings.Contains(body, want) {
|
|||
|
|
t.Errorf("app shell 未包含 %q", want)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|