mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-07-13 00:13:33 +08:00
- 新增 web-server 运行模式与浏览器端运行时桥接 - 支持管理员密码、会话策略、Google Authenticator 与恢复码 - 设置中心补充浏览器访问认证状态与改密入口 - 优化初始化向导,未启用 2FA 时跳过验证器步骤 Refs #618
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package webserver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type webserverTestReceiver struct{}
|
|
|
|
func (webserverTestReceiver) Echo(value string) (map[string]any, error) {
|
|
return map[string]any{"value": value}, nil
|
|
}
|
|
|
|
func (webserverTestReceiver) Sum(left int, right int) int {
|
|
return left + right
|
|
}
|
|
|
|
func TestInjectRuntimeBridgeAddsScriptOnce(t *testing.T) {
|
|
indexHTML := "<html><head><title>GoNavi</title></head><body></body></html>"
|
|
|
|
injected := injectRuntimeBridge(indexHTML)
|
|
if !strings.Contains(injected, internalRoutePrefix+"/web-runtime.js") {
|
|
t.Fatalf("expected injected HTML to contain runtime bridge script, got: %s", injected)
|
|
}
|
|
|
|
reinjected := injectRuntimeBridge(injected)
|
|
if strings.Count(reinjected, internalRoutePrefix+"/web-runtime.js") != 1 {
|
|
t.Fatalf("expected runtime bridge script to be injected once, got: %s", reinjected)
|
|
}
|
|
}
|
|
|
|
func TestMethodInvokerInvokeDecodesArgumentsAndReturnsResult(t *testing.T) {
|
|
invoker := &methodInvoker{
|
|
targets: map[string]reflect.Value{
|
|
"test.receiver": reflect.ValueOf(webserverTestReceiver{}),
|
|
},
|
|
}
|
|
|
|
rawLeft, _ := json.Marshal(2)
|
|
rawRight, _ := json.Marshal(5)
|
|
result, err := invoker.Invoke(invokeRequest{
|
|
Namespace: "test",
|
|
Receiver: "receiver",
|
|
Method: "Sum",
|
|
Args: []json.RawMessage{rawLeft, rawRight},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("expected invoke success, got error: %v", err)
|
|
}
|
|
if result != 7 {
|
|
t.Fatalf("expected sum result 7, got %#v", result)
|
|
}
|
|
}
|
|
|
|
func TestMethodInvokerInvokeSupportsStructuredReturnValues(t *testing.T) {
|
|
invoker := &methodInvoker{
|
|
targets: map[string]reflect.Value{
|
|
"test.receiver": reflect.ValueOf(webserverTestReceiver{}),
|
|
},
|
|
}
|
|
|
|
rawValue, _ := json.Marshal("hello")
|
|
result, err := invoker.Invoke(invokeRequest{
|
|
Namespace: "test",
|
|
Receiver: "receiver",
|
|
Method: "Echo",
|
|
Args: []json.RawMessage{rawValue},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("expected invoke success, got error: %v", err)
|
|
}
|
|
payload, ok := result.(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("expected structured result map, got %#v", result)
|
|
}
|
|
if payload["value"] != "hello" {
|
|
t.Fatalf("expected echoed value hello, got %#v", payload["value"])
|
|
}
|
|
}
|