fc3a66dc83
Go 控制面(SQLite + REST API)嵌入 Mihomo 数据面,Vue 3 多语言 Web 管理台。 含订阅/节点/规则/出站/监控/日志、OpenAPI、API Key、Gitea Actions CI(runs-on: ubuntu-latest) 与开源文档;镜像发布至 git.rc707blog.top Container Registry。 Co-authored-by: Cursor <cursoragent@cursor.com>
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package ruleio
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/prism/proxy/internal/store"
|
|
)
|
|
|
|
func TestParseLine(t *testing.T) {
|
|
r, err := ParseLine("DOMAIN-SUFFIX,google.com,country:HK")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.RuleType != "DOMAIN-SUFFIX" || r.Payload != "google.com" || r.Target != "country:HK" {
|
|
t.Fatalf("unexpected %+v", r)
|
|
}
|
|
}
|
|
|
|
func TestParseLineSkipsMatch(t *testing.T) {
|
|
_, err := ParseLine("MATCH,DIRECT")
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
}
|
|
|
|
func TestParseText(t *testing.T) {
|
|
rules, errs := ParseText("# comment\nDOMAIN,google.com,DIRECT\n\nDOMAIN-SUFFIX,foo.com,PROXY")
|
|
if len(errs) != 0 {
|
|
t.Fatal(errs)
|
|
}
|
|
if len(rules) != 2 {
|
|
t.Fatalf("got %d rules", len(rules))
|
|
}
|
|
}
|
|
|
|
func TestParseJSONDoc(t *testing.T) {
|
|
rules, err := ParseJSON([]byte(`{"version":1,"rules":[{"rule_type":"DOMAIN","payload":"a.com","target":"DIRECT"}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(rules) != 1 || rules[0].Target != "DIRECT" {
|
|
t.Fatalf("unexpected %+v", rules)
|
|
}
|
|
}
|
|
|
|
func TestExportText(t *testing.T) {
|
|
text := ExportText([]store.Rule{{
|
|
RuleType: "DOMAIN-SUFFIX",
|
|
Payload: "x.com",
|
|
Target: "PROXY",
|
|
Enabled: true,
|
|
}})
|
|
if !strings.Contains(text, "DOMAIN-SUFFIX,x.com,PROXY") {
|
|
t.Fatalf("got %q", text)
|
|
}
|
|
}
|
|
|
|
func TestToStoreRulesDefaultsEnabled(t *testing.T) {
|
|
rules, errs := ToStoreRules([]ExportRule{{RuleType: "DOMAIN", Payload: "a.com", Target: "DIRECT"}})
|
|
if len(errs) != 0 || len(rules) != 1 || !rules[0].Enabled {
|
|
t.Fatalf("rules=%+v errs=%v", rules, errs)
|
|
}
|
|
}
|