2015-04-24 14:57:35 -04:00
|
|
|
'use strict';
|
|
|
|
|
2020-12-28 16:55:08 +02:00
|
|
|
const parseConnection = require('../../lib/knex-builder/internal/parse-connection');
|
2018-10-15 22:29:53 -04:00
|
|
|
const test = require('tape');
|
2015-04-24 14:57:35 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('parses standard connections', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2020-04-26 19:35:06 +02:00
|
|
|
t.deepLooseEqual(
|
2018-07-09 08:10:34 -04:00
|
|
|
parseConnection('postgres://username:pass@path.to.some-url:6000/testdb'),
|
|
|
|
{
|
|
|
|
client: 'postgres',
|
|
|
|
connection: {
|
|
|
|
user: 'username',
|
|
|
|
password: 'pass',
|
|
|
|
port: '6000',
|
2020-04-26 19:35:06 +02:00
|
|
|
host: 'path.to.some-url',
|
2018-07-09 08:10:34 -04:00
|
|
|
database: 'testdb',
|
|
|
|
},
|
2015-04-24 14:57:35 -04:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
});
|
2015-04-24 14:57:35 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('parses standard connections without password', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(
|
|
|
|
parseConnection('mysql://username@path.to.some-url:3306/testdb'),
|
|
|
|
{
|
|
|
|
client: 'mysql',
|
|
|
|
connection: {
|
|
|
|
user: 'username',
|
|
|
|
host: 'path.to.some-url',
|
|
|
|
port: '3306',
|
|
|
|
database: 'testdb',
|
|
|
|
},
|
2016-06-06 22:19:24 -04:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
);
|
|
|
|
});
|
2015-04-24 14:57:35 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('mysql connection protocol with query string params', function (t) {
|
2019-11-21 15:21:51 +01:00
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(
|
2020-12-28 01:09:58 +03:00
|
|
|
parseConnection(
|
|
|
|
'mysql://user:pass@path.to.some-url:3306/testdb?foo=bar&anotherParam=%2Fa%2Fb%2Fc'
|
|
|
|
),
|
2019-11-21 15:21:51 +01:00
|
|
|
{
|
|
|
|
client: 'mysql',
|
|
|
|
connection: {
|
|
|
|
user: 'user',
|
|
|
|
password: 'pass',
|
|
|
|
host: 'path.to.some-url',
|
|
|
|
port: '3306',
|
|
|
|
database: 'testdb',
|
|
|
|
foo: 'bar',
|
2020-12-28 01:09:58 +03:00
|
|
|
anotherParam: '/a/b/c',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('mysql connection protocol allows skip username', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(
|
|
|
|
parseConnection(
|
|
|
|
'mysql://:pass@path.to.some-url:3306/testdb?foo=bar&anotherParam=%2Fa%2Fb%2Fc'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
client: 'mysql',
|
|
|
|
connection: {
|
|
|
|
user: '',
|
|
|
|
password: 'pass',
|
|
|
|
host: 'path.to.some-url',
|
|
|
|
port: '3306',
|
|
|
|
database: 'testdb',
|
|
|
|
foo: 'bar',
|
|
|
|
anotherParam: '/a/b/c',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('mysql connection protocol allows skip password', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(
|
|
|
|
parseConnection('mysql://username@path.to.some-url:3306/testdb'),
|
|
|
|
{
|
|
|
|
client: 'mysql',
|
|
|
|
connection: {
|
|
|
|
user: 'username',
|
|
|
|
host: 'path.to.some-url',
|
|
|
|
port: '3306',
|
|
|
|
database: 'testdb',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('mysql connection protocol allows empty password', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(
|
|
|
|
parseConnection('mysql://username:@path.to.some-url:3306/testdb'),
|
|
|
|
{
|
|
|
|
client: 'mysql',
|
|
|
|
connection: {
|
|
|
|
user: 'username',
|
|
|
|
host: 'path.to.some-url',
|
|
|
|
port: '3306',
|
|
|
|
database: 'testdb',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('decodes username and password', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(parseConnection('mysql://user%3A@path.to.some-url:3306/testdb'), {
|
|
|
|
client: 'mysql',
|
|
|
|
connection: {
|
|
|
|
user: 'user:',
|
|
|
|
host: 'path.to.some-url',
|
|
|
|
port: '3306',
|
|
|
|
database: 'testdb',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('do not encode password', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(
|
|
|
|
parseConnection(
|
|
|
|
'mysql://user:password-start:rest@path.to.some-url:3306/testdb?'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
client: 'mysql',
|
|
|
|
connection: {
|
|
|
|
user: 'user',
|
|
|
|
password: 'password-start:rest',
|
|
|
|
host: 'path.to.some-url',
|
|
|
|
port: '3306',
|
|
|
|
database: 'testdb',
|
2019-11-21 15:21:51 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('parses mssql connections, aliasing host to server', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2018-10-15 22:29:53 -04:00
|
|
|
const mssql = {
|
2016-04-12 03:33:28 +04:00
|
|
|
client: 'mssql',
|
|
|
|
connection: {
|
|
|
|
user: 'username',
|
|
|
|
password: 'pass',
|
|
|
|
server: 'path.to.some-url',
|
|
|
|
port: '6000',
|
2018-07-09 08:10:34 -04:00
|
|
|
database: 'testdb',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
t.deepEqual(
|
|
|
|
parseConnection('mssql://username:pass@path.to.some-url:6000/testdb'),
|
|
|
|
mssql
|
|
|
|
);
|
|
|
|
});
|
2016-04-12 03:33:28 +04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('parses mssql connections, aliasing host to server and adding extra params', function (t) {
|
2019-11-21 15:21:51 +01:00
|
|
|
t.plan(1);
|
|
|
|
const mssql = {
|
|
|
|
client: 'mssql',
|
|
|
|
connection: {
|
|
|
|
user: 'user',
|
|
|
|
password: 'pass',
|
|
|
|
server: 'path.to.some-url',
|
|
|
|
port: '6000',
|
|
|
|
database: 'testdb',
|
|
|
|
param: 'value',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
t.deepEqual(
|
|
|
|
parseConnection(
|
|
|
|
'mssql://user:pass@path.to.some-url:6000/testdb?param=value'
|
|
|
|
),
|
|
|
|
mssql
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-12-28 01:09:58 +03:00
|
|
|
test('assume a path is sqlite', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2015-04-24 14:57:35 -04:00
|
|
|
t.deepEqual(parseConnection('/path/to/file.db'), {
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
2018-07-09 08:10:34 -04:00
|
|
|
filename: '/path/to/file.db',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2015-06-01 07:39:57 -04:00
|
|
|
|
2020-12-28 01:09:58 +03:00
|
|
|
test('assume a relative path is sqlite', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
t.deepEqual(parseConnection('./path/to/file.db'), {
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: './path/to/file.db',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parse windows path as sqlite config', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
// reset process.platform to windows, reset require cache, require fresh
|
|
|
|
// after test restore back
|
|
|
|
const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform');
|
|
|
|
|
|
|
|
Object.defineProperty(process, 'platform', {
|
|
|
|
value: 'win32',
|
|
|
|
});
|
|
|
|
|
2020-12-28 16:55:08 +02:00
|
|
|
const modulePath = require.resolve(
|
|
|
|
'../../lib/knex-builder/internal/parse-connection'
|
|
|
|
);
|
2020-12-28 01:09:58 +03:00
|
|
|
const oldCache = require.cache[modulePath];
|
|
|
|
delete require.cache[modulePath];
|
|
|
|
|
2020-12-28 16:55:08 +02:00
|
|
|
const parseConnection = require('../../lib/knex-builder/internal/parse-connection');
|
2020-12-28 01:09:58 +03:00
|
|
|
try {
|
|
|
|
t.deepLooseEqual(
|
|
|
|
parseConnection('C:\\Documents\\Newsletters\\Summer2018.pdf'),
|
|
|
|
{
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: 'C:\\Documents\\Newsletters\\Summer2018.pdf',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
Object.defineProperty(process, 'platform', originalPlatform);
|
|
|
|
require.cache[modulePath] = oldCache;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('#852, ssl param with PG query string', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2020-04-26 19:35:06 +02:00
|
|
|
t.deepLooseEqual(
|
2020-12-28 01:09:58 +03:00
|
|
|
parseConnection('postgres://user:password@host:0000/database?ssl=true'),
|
2018-07-09 08:10:34 -04:00
|
|
|
{
|
2020-12-28 01:09:58 +03:00
|
|
|
client: 'postgres',
|
|
|
|
connection: {
|
|
|
|
host: 'host',
|
|
|
|
port: '0000',
|
|
|
|
user: 'user',
|
|
|
|
password: 'password',
|
|
|
|
database: 'database',
|
|
|
|
ssl: true,
|
|
|
|
},
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2018-05-18 05:12:15 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('support postgresql connection protocol', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2020-04-26 19:35:06 +02:00
|
|
|
t.deepLooseEqual(
|
2020-12-28 01:09:58 +03:00
|
|
|
parseConnection('postgresql://user:password@host:0000/database?ssl=true'),
|
2018-07-09 08:10:34 -04:00
|
|
|
{
|
2020-12-28 01:09:58 +03:00
|
|
|
client: 'postgresql',
|
|
|
|
connection: {
|
|
|
|
host: 'host',
|
|
|
|
port: '0000',
|
|
|
|
user: 'user',
|
|
|
|
password: 'password',
|
|
|
|
database: 'database',
|
|
|
|
ssl: true,
|
|
|
|
},
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|