File size: 5,756 Bytes
f8b5d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* eslint-env jest */
const { ConnectionStringParser } = require("../../../utils/agents/aibitat/plugins/sql-agent/SQLConnectors/utils");

describe("ConnectionStringParser", () => {
  describe("Basic Parsing", () => {
    test("should parse a basic connection string without options", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user:pass@localhost:1433/mydb");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user",
        password: "pass",
        hosts: [{ host: "localhost", port: 1433 }],
        endpoint: "mydb",
        options: undefined
      });
    });

    test("should parse a connection string with options", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user:pass@localhost:1433/mydb?encrypt=true&trustServerCertificate=true");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user",
        password: "pass",
        hosts: [{ host: "localhost", port: 1433 }],
        endpoint: "mydb",
        options: {
          encrypt: "true",
          trustServerCertificate: "true"
        }
      });
    });

    test("should handle empty passwords", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user@localhost:1433/mydb");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user",
        password: undefined,
        hosts: [{ host: "localhost", port: 1433 }],
        endpoint: "mydb",
        options: undefined
      });
    });
  });

  describe("Error Handling", () => {
    test("should throw error for invalid scheme", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      expect(() => parser.parse("mysql://user:pass@localhost:3306/mydb"))
        .toThrow("URI must start with 'mssql://'");
    });

    test("should throw error for missing scheme", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      expect(() => parser.parse("user:pass@localhost:1433/mydb"))
        .toThrow("No scheme found in URI");
    });
  });

  describe("Special Characters", () => {
    test("should handle special characters in username and password", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user%40domain:p%40ssw%3Ard@localhost:1433/mydb");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user@domain",
        password: "p@ssw:rd",
        hosts: [{ host: "localhost", port: 1433 }],
        endpoint: "mydb",
        options: undefined
      });
    });

    test("should handle special characters in database name", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user:pass@localhost:1433/my%20db");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user",
        password: "pass",
        hosts: [{ host: "localhost", port: 1433 }],
        endpoint: "my db",
        options: undefined
      });
    });
  });

  describe("Multiple Hosts", () => {
    test("should parse multiple hosts", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user:pass@host1:1433,host2:1434/mydb");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user",
        password: "pass",
        hosts: [
          { host: "host1", port: 1433 },
          { host: "host2", port: 1434 }
        ],
        endpoint: "mydb",
        options: undefined
      });
    });

    test("should handle hosts without ports", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user:pass@host1,host2/mydb");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user",
        password: "pass",
        hosts: [
          { host: "host1" },
          { host: "host2" }
        ],
        endpoint: "mydb",
        options: undefined
      });
    });
  });

  describe("Provider-Specific Tests", () => {
    test("should parse MySQL connection string", () => {
      const parser = new ConnectionStringParser({ scheme: "mysql" });
      const result = parser.parse("mysql://user:pass@localhost:3306/mydb?ssl=true");

      expect(result).toEqual({
        scheme: "mysql",
        username: "user",
        password: "pass",
        hosts: [{ host: "localhost", port: 3306 }],
        endpoint: "mydb",
        options: { ssl: "true" }
      });
    });

    test("should parse PostgreSQL connection string", () => {
      const parser = new ConnectionStringParser({ scheme: "postgresql" });
      const result = parser.parse("postgresql://user:pass@localhost:5432/mydb?sslmode=require");

      expect(result).toEqual({
        scheme: "postgresql",
        username: "user",
        password: "pass",
        hosts: [{ host: "localhost", port: 5432 }],
        endpoint: "mydb",
        options: { sslmode: "require" }
      });
    });

    test("should parse MSSQL connection string with encryption options", () => {
      const parser = new ConnectionStringParser({ scheme: "mssql" });
      const result = parser.parse("mssql://user:pass@localhost:1433/mydb?encrypt=true&trustServerCertificate=true");

      expect(result).toEqual({
        scheme: "mssql",
        username: "user",
        password: "pass",
        hosts: [{ host: "localhost", port: 1433 }],
        endpoint: "mydb",
        options: {
          encrypt: "true",
          trustServerCertificate: "true"
        }
      });
    });
  });
});