Stefan Huber

commit

......@@ -4,7 +4,7 @@ describe('Repository API', () => {
let repo = new Repository(null, null);
it('parse url', () => {
it('parse url with user:pass', () => {
let result = repo.parseUrl('https://admin1:admin2@someplace.com:6454/somedb_name');
expect(result.db_name).toEqual('local_somedb_name');
......@@ -15,4 +15,13 @@ describe('Repository API', () => {
expect(result.domain).toEqual('someplace.com:6454');
});
it('parse url without user:pass', () => {
let result = repo.parseUrl('https://someplace.com:6454/somedb_name');
expect(result.db_name).toEqual('local_somedb_name');
expect(result.orig_db_name).toEqual('somedb_name');
expect(result.protocol).toEqual('https');
expect(result.domain).toEqual('someplace.com:6454');
});
});
\ No newline at end of file
......
......@@ -99,7 +99,9 @@ export class Repository {
this.rest.scanEnvironment(response.local_ips)
.then(node => {
let res = this.parseUrl(response.db_url);
resolve(this.prepare('http://' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name));
let localCouchUrl = 'http://' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name;
console.log("prepare local couch url: ", localCouchUrl);
resolve(this.prepare(localCouchUrl));
}).catch(() => {
resolve(this.prepare(response.db_url));
});
......@@ -109,9 +111,8 @@ export class Repository {
prepare(url:string) : Promise<any> {
return new Promise<any>((resolve,reject) => {
this.params = this.parseUrl(url);
if (!this.params) {
reject();
reject('params are incorrect');
}
if (this.db && this.db.name == this.params.db_name) {
......@@ -147,10 +148,11 @@ export class Repository {
});
}
parseUrl(url:string) {
// matches: 0:user,1:password,2:domain,3:db_name
let exp = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/;
let match = url.match(exp);
parseUrl(url:string) : any {
// matches: protocol, user, password, domain, db_name
let expWithUsers = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/;
let expWithoutUsers = /^(https?):\/\/([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/;
let match = url.match(expWithUsers);
if (match) {
return {
......@@ -164,6 +166,17 @@ export class Repository {
}
}
match = url.match(expWithoutUsers);
if (match) {
return {
url : match[0] ,
protocol : match[1] ,
domain : match[2] ,
db_name : 'local_' + match[3] ,
orig_db_name : match[3]
}
}
return null;
}
......