Files
Wormhole/internal/forwardplugin/plugin_test.go
T
rose_cat707 612d68f56f feat: Wormhole 内网穿透与反向代理网关
Go + Vue 管理台,SQLite 持久化;支持隧道/域名穿透、域名转发插件链、访客认证、
IP 规则、API Key、Docker 单镜像部署;配置通过 Web 系统设置管理,无需 YAML 文件。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 12:15:02 +08:00

94 lines
2.4 KiB
Go

package forwardplugin
import (
"encoding/json"
"testing"
)
func TestValidateAndNormalizePlugins(t *testing.T) {
entries := []PluginEntry{
{
Type: "sub_filter",
Enabled: true,
Config: json.RawMessage(`{"filters":[{"from":"a","to":"b"}]}`),
},
{
Type: "response_cache",
Enabled: true,
Config: json.RawMessage(`{"max_mb":10,"ttl_sec":0}`),
},
}
out, err := ValidateAndNormalizePlugins(entries)
if err != nil {
t.Fatal(err)
}
if len(out) != 2 {
t.Fatalf("expected 2 plugins, got %d", len(out))
}
var cacheCfg responseCacheConfig
if err := json.Unmarshal(out[1].Config, &cacheCfg); err != nil {
t.Fatal(err)
}
if cacheCfg.TTLSec != 300 {
t.Fatalf("expected default ttl 300, got %d", cacheCfg.TTLSec)
}
}
func TestSubFilterReplace(t *testing.T) {
p, err := newSubFilterPlugin(json.RawMessage(`{"filters":[{"from":"old","to":"new"}],"types":["text/html"]}`))
if err != nil {
t.Fatal(err)
}
plugin := p.(*subFilterPlugin)
resp := &BufferedResponse{
StatusCode: 200,
Header: make(map[string][]string),
Body: []byte("<html>old page</html>"),
}
resp.Header.Set("Content-Type", "text/html; charset=utf-8")
if err := plugin.HandleResponse(nil, resp); err != nil {
t.Fatal(err)
}
if string(resp.Body) != "<html>new page</html>" {
t.Fatalf("unexpected body: %s", resp.Body)
}
}
func TestInjectJS(t *testing.T) {
p, err := newInjectJSPlugin(json.RawMessage(`{"script":"<script>x</script>"}`))
if err != nil {
t.Fatal(err)
}
plugin := p.(*injectJSPlugin)
resp := &BufferedResponse{
Header: make(map[string][]string),
Body: []byte("<html><body>hi</body></html>"),
}
resp.Header.Set("Content-Type", "text/html")
if err := plugin.HandleResponse(nil, resp); err != nil {
t.Fatal(err)
}
if want := "<html><body>hi<script>x</script></body></html>"; string(resp.Body) != want {
t.Fatalf("got %q want %q", resp.Body, want)
}
}
func TestCookieDomainRewrite(t *testing.T) {
p, err := newCookieDomainPlugin(json.RawMessage(`{"from":".old.com","to":".new.com"}`))
if err != nil {
t.Fatal(err)
}
plugin := p.(*cookieDomainPlugin)
resp := &BufferedResponse{
Header: make(map[string][]string),
}
resp.Header.Add("Set-Cookie", "sid=1; Domain=.old.com; Path=/")
if err := plugin.HandleResponse(nil, resp); err != nil {
t.Fatal(err)
}
got := resp.Header.Get("Set-Cookie")
if got != "sid=1; Domain=.new.com; Path=/" {
t.Fatalf("unexpected cookie: %s", got)
}
}