feat: initialize shared tenant workbench data

This commit is contained in:
richjohnrambo
2026-09-23 17:04:30 +08:00
parent 5a07f63716
commit abadcceb19
8 changed files with 122 additions and 18 deletions
+5 -5
View File
@@ -7,11 +7,11 @@ import (
)
func TestNormalizeMySQLTimeOptions(t *testing.T) {
link, err := normalizeMySQLTimeOptions("user:pass@tcp(127.0.0.1:3306)/tinode?charset=utf8mb4")
link, err := normalizeMySQLTimeOptions("user:pass@tcp(127.0.0.1:3306)/meeting_im?charset=utf8mb4")
if err != nil {
t.Fatalf("normalize raw mysql dsn: %v", err)
}
if !strings.HasPrefix(link, "mysql:user:pass@tcp(127.0.0.1:3306)/tinode?") {
if !strings.HasPrefix(link, "mysql:user:pass@tcp(127.0.0.1:3306)/meeting_im?") {
t.Fatalf("unexpected normalized link: %s", link)
}
if !strings.Contains(link, "loc=UTC") || !strings.Contains(link, "parseTime=true") {
@@ -20,17 +20,17 @@ func TestNormalizeMySQLTimeOptions(t *testing.T) {
}
func TestNormalizeMySQLTimeOptionsRejectsUnsupportedFormat(t *testing.T) {
if _, err := normalizeMySQLTimeOptions("postgres://user:pass@127.0.0.1:5432/tinode"); err == nil {
if _, err := normalizeMySQLTimeOptions("postgres://user:pass@127.0.0.1:5432/meeting_im"); err == nil {
t.Fatal("expected unsupported format error")
}
}
func TestReplaceMySQLDatabaseUsesUTC(t *testing.T) {
link, err := replaceMySQLDatabase("mysql:user:pass@tcp(127.0.0.1:3306)/hotgo?loc=Local&parseTime=false&charset=utf8mb4", "tinode")
link, err := replaceMySQLDatabase("mysql:user:pass@tcp(127.0.0.1:3306)/hotgo?loc=Local&parseTime=false&charset=utf8mb4", "meeting_im")
if err != nil {
t.Fatal(err)
}
for _, expected := range []string{"/tinode?", "loc=UTC", "parseTime=true", "charset=utf8mb4"} {
for _, expected := range []string{"/meeting_im?", "loc=UTC", "parseTime=true", "charset=utf8mb4"} {
if !strings.Contains(link, expected) {
t.Fatalf("link %q does not contain %q", link, expected)
}
+17 -3
View File
@@ -65,27 +65,41 @@ func DefaultClient(ctx context.Context) *Client {
}
func (c *Client) Get(ctx context.Context, path string, values url.Values, out any) error {
return c.GetForTenant(ctx, path, values, 0, "", out)
}
func (c *Client) GetForTenant(ctx context.Context, path string, values url.Values, tenantID int64, tenantCode string, out any) error {
if values != nil {
if query := values.Encode(); query != "" {
path += "?" + query
}
}
return c.doJSON(ctx, http.MethodGet, path, nil, out)
return c.doJSON(ctx, http.MethodGet, path, nil, tenantID, tenantCode, out)
}
func (c *Client) Post(ctx context.Context, path string, in any, out any) error {
return c.PostForTenant(ctx, path, in, 0, "", out)
}
func (c *Client) PostForTenant(ctx context.Context, path string, in any, tenantID int64, tenantCode string, out any) error {
body, err := json.Marshal(in)
if err != nil {
return gerror.Wrap(err, "序列化工作台请求失败")
}
return c.doJSON(ctx, http.MethodPost, path, body, out)
return c.doJSON(ctx, http.MethodPost, path, body, tenantID, tenantCode, out)
}
func (c *Client) doJSON(ctx context.Context, method string, path string, body []byte, out any) error {
func (c *Client) doJSON(ctx context.Context, method string, path string, body []byte, tenantID int64, tenantCode string, out any) error {
req, err := c.newRequest(ctx, method, path, body)
if err != nil {
return err
}
if tenantID > 0 {
req.Header.Set("X-Tenant-ID", strconv.FormatInt(tenantID, 10))
}
if tenantCode = strings.TrimSpace(tenantCode); tenantCode != "" {
req.Header.Set("X-Tenant-Code", tenantCode)
}
resp, err := c.http.Do(req)
if err != nil {
g.Log().Warningf(ctx, "IM workbench request failed url:%s err:%+v", req.URL.String(), err)
@@ -0,0 +1,37 @@
package imworkbench
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestPostForTenantAddsTenantHeaders(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("X-Tenant-ID"); got != "23" {
t.Fatalf("X-Tenant-ID = %q, want 23", got)
}
if got := r.Header.Get("X-Tenant-Code"); got != "merchant-23" {
t.Fatalf("X-Tenant-Code = %q, want merchant-23", got)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code":0,"data":{"tenant_id":23}}`))
}))
defer server.Close()
client := &Client{
conf: Config{BaseURL: server.URL, ServiceName: "test", Secret: "secret", Timeout: time.Second},
http: server.Client(),
}
var response struct {
TenantID int64 `json:"tenant_id"`
}
if err := client.PostForTenant(context.Background(), "/initialize", map[string]any{"tenant_id": 23}, 23, "merchant-23", &response); err != nil {
t.Fatal(err)
}
if response.TenantID != 23 {
t.Fatalf("tenant_id = %d, want 23", response.TenantID)
}
}