Showing
2 changed files
with
30 additions
and
8 deletions
| ... | @@ -4,7 +4,7 @@ describe('Repository API', () => { | ... | @@ -4,7 +4,7 @@ describe('Repository API', () => { |
| 4 | 4 | ||
| 5 | let repo = new Repository(null, null); | 5 | let repo = new Repository(null, null); |
| 6 | 6 | ||
| 7 | - it('parse url', () => { | 7 | + it('parse url with user:pass', () => { |
| 8 | let result = repo.parseUrl('https://admin1:admin2@someplace.com:6454/somedb_name'); | 8 | let result = repo.parseUrl('https://admin1:admin2@someplace.com:6454/somedb_name'); |
| 9 | 9 | ||
| 10 | expect(result.db_name).toEqual('local_somedb_name'); | 10 | expect(result.db_name).toEqual('local_somedb_name'); |
| ... | @@ -15,4 +15,13 @@ describe('Repository API', () => { | ... | @@ -15,4 +15,13 @@ describe('Repository API', () => { |
| 15 | expect(result.domain).toEqual('someplace.com:6454'); | 15 | expect(result.domain).toEqual('someplace.com:6454'); |
| 16 | }); | 16 | }); |
| 17 | 17 | ||
| 18 | + it('parse url without user:pass', () => { | ||
| 19 | + let result = repo.parseUrl('https://someplace.com:6454/somedb_name'); | ||
| 20 | + | ||
| 21 | + expect(result.db_name).toEqual('local_somedb_name'); | ||
| 22 | + expect(result.orig_db_name).toEqual('somedb_name'); | ||
| 23 | + expect(result.protocol).toEqual('https'); | ||
| 24 | + expect(result.domain).toEqual('someplace.com:6454'); | ||
| 25 | + }); | ||
| 26 | + | ||
| 18 | }); | 27 | }); |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -99,7 +99,9 @@ export class Repository { | ... | @@ -99,7 +99,9 @@ export class Repository { |
| 99 | this.rest.scanEnvironment(response.local_ips) | 99 | this.rest.scanEnvironment(response.local_ips) |
| 100 | .then(node => { | 100 | .then(node => { |
| 101 | let res = this.parseUrl(response.db_url); | 101 | let res = this.parseUrl(response.db_url); |
| 102 | - resolve(this.prepare('http://' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name)); | 102 | + let localCouchUrl = 'http://' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name; |
| 103 | + console.log("prepare local couch url: ", localCouchUrl); | ||
| 104 | + resolve(this.prepare(localCouchUrl)); | ||
| 103 | }).catch(() => { | 105 | }).catch(() => { |
| 104 | resolve(this.prepare(response.db_url)); | 106 | resolve(this.prepare(response.db_url)); |
| 105 | }); | 107 | }); |
| ... | @@ -109,9 +111,8 @@ export class Repository { | ... | @@ -109,9 +111,8 @@ export class Repository { |
| 109 | prepare(url:string) : Promise<any> { | 111 | prepare(url:string) : Promise<any> { |
| 110 | return new Promise<any>((resolve,reject) => { | 112 | return new Promise<any>((resolve,reject) => { |
| 111 | this.params = this.parseUrl(url); | 113 | this.params = this.parseUrl(url); |
| 112 | - | ||
| 113 | if (!this.params) { | 114 | if (!this.params) { |
| 114 | - reject(); | 115 | + reject('params are incorrect'); |
| 115 | } | 116 | } |
| 116 | 117 | ||
| 117 | if (this.db && this.db.name == this.params.db_name) { | 118 | if (this.db && this.db.name == this.params.db_name) { |
| ... | @@ -147,10 +148,11 @@ export class Repository { | ... | @@ -147,10 +148,11 @@ export class Repository { |
| 147 | }); | 148 | }); |
| 148 | } | 149 | } |
| 149 | 150 | ||
| 150 | - parseUrl(url:string) { | 151 | + parseUrl(url:string) : any { |
| 151 | - // matches: 0:user,1:password,2:domain,3:db_name | 152 | + // matches: protocol, user, password, domain, db_name |
| 152 | - let exp = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/; | 153 | + let expWithUsers = /^(https?):\/\/(\w+?):(\w+?)@([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/; |
| 153 | - let match = url.match(exp); | 154 | + let expWithoutUsers = /^(https?):\/\/([a-zA-Z0-9.\-_:]+?)\/([a-zA-Z0-9\-_]+?)$/; |
| 155 | + let match = url.match(expWithUsers); | ||
| 154 | 156 | ||
| 155 | if (match) { | 157 | if (match) { |
| 156 | return { | 158 | return { |
| ... | @@ -164,6 +166,17 @@ export class Repository { | ... | @@ -164,6 +166,17 @@ export class Repository { |
| 164 | } | 166 | } |
| 165 | } | 167 | } |
| 166 | 168 | ||
| 169 | + match = url.match(expWithoutUsers); | ||
| 170 | + if (match) { | ||
| 171 | + return { | ||
| 172 | + url : match[0] , | ||
| 173 | + protocol : match[1] , | ||
| 174 | + domain : match[2] , | ||
| 175 | + db_name : 'local_' + match[3] , | ||
| 176 | + orig_db_name : match[3] | ||
| 177 | + } | ||
| 178 | + } | ||
| 179 | + | ||
| 167 | return null; | 180 | return null; |
| 168 | } | 181 | } |
| 169 | 182 | ... | ... |
-
Please register or login to post a comment