fix(redis): numeric db index and cli flag salvage on url import
This commit is contained in:
8 files changed
+197
-7
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { parseConnectionUrl } from "@/lib/connection/connectionUrl";
|
||||
|
||||
describe("Redis connection URLs", () => {
|
||||
it("parses a numeric database index from the path", () => {
|
||||
const parsed = parseConnectionUrl("rediss://coupon@cache.example.com:6379/3?insecure=true");
|
||||
expect(parsed.dbType).toBe("redis");
|
||||
expect(parsed.host).toBe("cache.example.com");
|
||||
expect(parsed.port).toBe(6379);
|
||||
expect(parsed.username).toBe("coupon");
|
||||
expect(parsed.database).toBe("3");
|
||||
expect(parsed.ssl).toBe(true);
|
||||
expect(parsed.urlParams).toBe("insecure=true");
|
||||
});
|
||||
|
||||
it("treats the rediss scheme as TLS", () => {
|
||||
expect(parseConnectionUrl("rediss://cache.example.com:6379/0").ssl).toBe(true);
|
||||
expect(parseConnectionUrl("redis://cache.example.com:6379/0").ssl).toBe(false);
|
||||
});
|
||||
|
||||
it("salvages pasted redis-cli --tls and --insecure flags from the path", () => {
|
||||
const parsed = parseConnectionUrl("redis://coupon:secret@qas-cnapacdd-invisclub-cnn2.redis.rds.aliyuncs.com:6379/0 --tls --insecure");
|
||||
expect(parsed.dbType).toBe("redis");
|
||||
expect(parsed.host).toBe("qas-cnapacdd-invisclub-cnn2.redis.rds.aliyuncs.com");
|
||||
expect(parsed.username).toBe("coupon");
|
||||
expect(parsed.password).toBe("secret");
|
||||
expect(parsed.database).toBe("0");
|
||||
expect(parsed.ssl).toBe(true);
|
||||
expect(parsed.urlParams).toBe("insecure=true");
|
||||
});
|
||||
|
||||
it("salvages flags from an already percent-encoded dirty path", () => {
|
||||
const parsed = parseConnectionUrl("rediss://coupon@cache.example.com:6379/0%20--tls%20--insecure?insecure=true");
|
||||
expect(parsed.database).toBe("0");
|
||||
expect(parsed.ssl).toBe(true);
|
||||
// The explicit query param already carries insecure; it must not be duplicated.
|
||||
expect(parsed.urlParams).toBe("insecure=true");
|
||||
});
|
||||
|
||||
it("salvages --tls without --insecure", () => {
|
||||
const parsed = parseConnectionUrl("redis://cache.example.com:6379/2 --tls");
|
||||
expect(parsed.database).toBe("2");
|
||||
expect(parsed.ssl).toBe(true);
|
||||
expect(parsed.urlParams).toBe("");
|
||||
});
|
||||
|
||||
it("salvages flags when the path carries no db index", () => {
|
||||
const parsed = parseConnectionUrl("redis://cache.example.com:6379/--tls --insecure");
|
||||
expect(parsed.database).toBeUndefined();
|
||||
expect(parsed.ssl).toBe(true);
|
||||
expect(parsed.urlParams).toBe("insecure=true");
|
||||
});
|
||||
|
||||
it("collapses a non-numeric path with no flags to the db index the backend uses", () => {
|
||||
expect(parseConnectionUrl("redis://cache.example.com:6379/mydb").database).toBe("0");
|
||||
});
|
||||
|
||||
it("leaves the database unset when the URL has no path", () => {
|
||||
expect(parseConnectionUrl("redis://cache.example.com:6379").database).toBeUndefined();
|
||||
expect(parseConnectionUrl("redis://cache.example.com:6379/").database).toBeUndefined();
|
||||
});
|
||||
|
||||
it("maps the #insecure fragment to the insecure url param", () => {
|
||||
const parsed = parseConnectionUrl("rediss://cache.example.com:6379/0#insecure");
|
||||
expect(parsed.database).toBe("0");
|
||||
expect(parsed.urlParams).toBe("insecure=true");
|
||||
});
|
||||
});
|
||||
@@ -80,6 +80,14 @@ describe("buildConnectionUrlCopy standard URL", () => {
|
||||
expect(buildConnectionUrlCopy(redis, "urlWithPassword")).toBe("rediss://:secret@cache.example.com:6379/0");
|
||||
});
|
||||
|
||||
it("falls back to db 0 for non-numeric Redis database values instead of percent-encoding them", () => {
|
||||
// Mirrors dbx-core redis_database_index(): "0 --tls --insecure" fails to
|
||||
// parse as an index and the backend silently uses 0.
|
||||
const redis = config({ db_type: "redis", host: "cache.example.com", port: 6379, username: "coupon", password: "", database: "0 --tls --insecure", url_params: "insecure=true", ssl: true });
|
||||
expect(buildConnectionUrlCopy(redis, "url")).toBe("rediss://coupon@cache.example.com:6379/0?insecure=true");
|
||||
expect(buildConnectionUrlCopy(config({ db_type: "redis", database: "3" }), "url")).toBe("redis://app_user@db.example.com:5432/3");
|
||||
});
|
||||
|
||||
it("maps the MySQL family profiles to their own schemes", () => {
|
||||
expect(buildConnectionUrlCopy(config({ db_type: "mysql", port: 3306 }), "url")).toBe("mysql://app_user@db.example.com:3306/appdb");
|
||||
expect(buildConnectionUrlCopy(config({ db_type: "mysql", port: 3306, driver_profile: "mariadb" }), "urlWithPassword")).toBe("mariadb://app_user:secret@db.example.com:3306/appdb");
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { effectiveRedisDatabaseIndex, normalizeRedisDatabaseValue } from "@/lib/redis/redisDatabaseIndex";
|
||||
|
||||
describe("effectiveRedisDatabaseIndex", () => {
|
||||
it("keeps numeric indexes as-is", () => {
|
||||
expect(effectiveRedisDatabaseIndex("0")).toBe("0");
|
||||
expect(effectiveRedisDatabaseIndex("3")).toBe("3");
|
||||
expect(effectiveRedisDatabaseIndex(" 15 ")).toBe("15");
|
||||
});
|
||||
|
||||
it("falls back to db 0 for dirty values, mirroring dbx-core redis_database_index()", () => {
|
||||
expect(effectiveRedisDatabaseIndex("0 --tls --insecure")).toBe("0");
|
||||
expect(effectiveRedisDatabaseIndex("3 --foo")).toBe("0");
|
||||
expect(effectiveRedisDatabaseIndex("mydb")).toBe("0");
|
||||
expect(effectiveRedisDatabaseIndex("")).toBe("0");
|
||||
expect(effectiveRedisDatabaseIndex(undefined)).toBe("0");
|
||||
expect(effectiveRedisDatabaseIndex(null)).toBe("0");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeRedisDatabaseValue", () => {
|
||||
it("passes numeric indexes through trimmed", () => {
|
||||
expect(normalizeRedisDatabaseValue("3")).toBe("3");
|
||||
expect(normalizeRedisDatabaseValue(" 3 ")).toBe("3");
|
||||
});
|
||||
|
||||
it("collapses dirty values to the index the backend actually uses", () => {
|
||||
expect(normalizeRedisDatabaseValue("0 --tls --insecure")).toBe("0");
|
||||
expect(normalizeRedisDatabaseValue("cache")).toBe("0");
|
||||
});
|
||||
|
||||
it("keeps empty values unset", () => {
|
||||
expect(normalizeRedisDatabaseValue("")).toBeUndefined();
|
||||
expect(normalizeRedisDatabaseValue(" ")).toBeUndefined();
|
||||
expect(normalizeRedisDatabaseValue(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user