chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,142 @@
|
||||
package agentcli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseEndpoint_HTTP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
wantURL string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "bare http host normalizes to /agent-stub",
|
||||
input: "http://localhost:8080",
|
||||
wantURL: "http://localhost:8080/agent-stub",
|
||||
},
|
||||
{
|
||||
name: "bare https host normalizes to /agent-stub",
|
||||
input: "https://agent.example.com",
|
||||
wantURL: "https://agent.example.com/agent-stub",
|
||||
},
|
||||
{
|
||||
name: "explicit /agent-stub path is preserved",
|
||||
input: "http://localhost:8080/agent-stub",
|
||||
wantURL: "http://localhost:8080/agent-stub",
|
||||
},
|
||||
{
|
||||
name: "trailing slash normalized",
|
||||
input: "http://localhost:8080/",
|
||||
wantURL: "http://localhost:8080/agent-stub",
|
||||
},
|
||||
{
|
||||
name: "invalid path rejects",
|
||||
input: "http://localhost:8080/other-path",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "query string rejects",
|
||||
input: "http://localhost:8080?foo=bar",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ep, err := ParseEndpoint(tc.input)
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if ep.URL != tc.wantURL {
|
||||
t.Errorf("URL = %q, want %q", ep.URL, tc.wantURL)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseEndpoint_Invalid(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
}{
|
||||
{"empty", ""},
|
||||
{"spaces only", " "},
|
||||
{"unsupported scheme", "ftp://example.com"},
|
||||
{"no host", "http://"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := ParseEndpoint(tc.input)
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewStubClient_NormalizesServiceRootWithoutMutatingEnvironment(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/agent-stub/connections" {
|
||||
t.Errorf("request path = %q, want %q", r.URL.Path, "/agent-stub/connections")
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(ConnectResponse{ConnectionID: "connection-1", Status: "connected"})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
env := &Environment{URL: server.URL, AuthJWE: "test-token"}
|
||||
client, err := NewStubClient(env)
|
||||
if err != nil {
|
||||
t.Fatalf("NewStubClient() error = %v", err)
|
||||
}
|
||||
defer func() { _ = client.Close() }()
|
||||
|
||||
response, err := client.Connect(context.Background(), nil, "")
|
||||
if err != nil {
|
||||
t.Fatalf("Connect() error = %v", err)
|
||||
}
|
||||
if response.ConnectionID != "connection-1" {
|
||||
t.Errorf("connection ID = %q, want %q", response.ConnectionID, "connection-1")
|
||||
}
|
||||
if env.URL != server.URL {
|
||||
t.Errorf("caller environment URL = %q, want unchanged %q", env.URL, server.URL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadEnvironment_Missing(t *testing.T) {
|
||||
t.Setenv(EnvAPIBaseURL, "")
|
||||
t.Setenv(EnvAuthJWE, "")
|
||||
|
||||
_, err := ReadEnvironment()
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing env vars")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadEnvironment_Valid(t *testing.T) {
|
||||
t.Setenv(EnvAPIBaseURL, "http://localhost:8080")
|
||||
t.Setenv(EnvAuthJWE, "test-token")
|
||||
|
||||
env, err := ReadEnvironment()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if env.URL != "http://localhost:8080/agent-stub" {
|
||||
t.Errorf("URL = %q, want %q", env.URL, "http://localhost:8080/agent-stub")
|
||||
}
|
||||
if env.AuthJWE != "test-token" {
|
||||
t.Errorf("AuthJWE = %q, want %q", env.AuthJWE, "test-token")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user