Skip to content

Commit

Permalink
Fix key to avoid to much /
Browse files Browse the repository at this point in the history
  • Loading branch information
srz09 committed Dec 11, 2017
1 parent 479030c commit 3ff96da
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 29 deletions.
35 changes: 20 additions & 15 deletions src/module/services/etcd3.service.ts
Expand Up @@ -25,18 +25,17 @@ export class Etcd3Service {


constructor(@Inject(Etcd3Ext) private _manager) {
let basePath = '/';
this._basePath = (this._manager.config.basePath || '').trim().length ?
this._manager.config.basePath : '/';
this._client = this._manager.client.namespace(this._basePath);
}

if (this._manager.config.basePath) {
basePath = this._manager.config.basePath;
if (!this._manager.config.basePath.endsWith('/')) {
basePath = `${basePath}/`;
}
protected _fixKey(key: string) {
if (!key || !key.trim().length || key === '/') {
return '/';
}

this._basePath = basePath;

this._client = this._manager.client.namespace(this._basePath);
return `${this._basePath.endsWith('/') ? '' : '/'}${key.split('/').filter(_ => _.trim().length).join('/')}`;
}

/**
Expand Down Expand Up @@ -87,8 +86,9 @@ export class Etcd3Service {
* @returns {string | object | number | Buffer | null | Error} The value of the object stored
*
*/
public get(key: string, format: ResponseFormat = ResponseFormat.String):
public get(_key: string, format: ResponseFormat = ResponseFormat.String):
Observable<string | object | Buffer | number | null | Error> {
const key = this._fixKey(_key);
const promise = this.client.get(key);
switch (format) {
case ResponseFormat.String:
Expand All @@ -113,7 +113,8 @@ export class Etcd3Service {
* @returns {IDeleteRangeResponse} The result of the operation
*
*/
public delete(key: string): Observable<IDeleteRangeResponse> {
public delete(_key: string): Observable<IDeleteRangeResponse> {
const key = this._fixKey(_key);
return Observable.fromPromise(this.client.delete().key(key));
}

Expand All @@ -139,9 +140,10 @@ export class Etcd3Service {
* @returns {IPutResponse} The result of the operation
*
*/
public put(key: string, value: string | number | Object | Buffer, returnResult: boolean = true):
public put(_key: string, value: string | number | Object | Buffer, returnResult: boolean = true):
Observable<IPutResponse | string | number | Object | Buffer> {

const key = this._fixKey(_key);
if (!value) {
return Observable.throw(new Error('"value" should not be null nor undefined'));
}
Expand Down Expand Up @@ -204,7 +206,8 @@ export class Etcd3Service {
* @returns {Watcher} The watcher instance created
*
*/
public createWatcher(key: string, prefix: boolean = false): Observable<Watcher> {
public createWatcher(_key: string, prefix: boolean = false): Observable<Watcher> {
const key = this._fixKey(_key);
const prefix$ = Observable.of(prefix).share();
return Observable.merge(
prefix$.filter(_ => !!_)
Expand Down Expand Up @@ -234,7 +237,8 @@ export class Etcd3Service {
* @returns {Lock} The lock instance created
*
*/
public acquireLock(key: string, ttl: number = 1): Observable<Lock> {
public acquireLock(_key: string, ttl: number = 1): Observable<Lock> {
const key = this._fixKey(_key);
return Observable.fromPromise(
this.client.lock(key).ttl(ttl || 1).acquire()
);
Expand Down Expand Up @@ -274,7 +278,8 @@ export class Etcd3Service {
* @returns {Lease} The lease instance created
*
*/
public createLeaseWithValue(key: string, value: string | Buffer, ttl: number = 1): Observable<Lease> {
public createLeaseWithValue(_key: string, value: string | Buffer, ttl: number = 1): Observable<Lease> {
const key = this._fixKey(_key);
const lease = this.client.lease(ttl || 1);
return Observable.fromPromise(
lease.put(key).value(value).exec().then(_ => lease)
Expand Down
65 changes: 51 additions & 14 deletions test/unit/etcd3.service.test.ts
Expand Up @@ -54,7 +54,7 @@ export class Etcd3ServiceTest {
const instanceBasePathEndsWithSlash = new Etcd3Service(<any>{ client, config: { basePath: '/basepath/' } });
const instanceNoBasePath = new Etcd3Service(<any>{ client, config: {} });

unit.string(instanceBasePath.basePath).is('/basepath/');
unit.string(instanceBasePath.basePath).is('/basepath');
unit.string(instanceBasePathEndsWithSlash.basePath).is('/basepath/');
unit.string(instanceNoBasePath.basePath).is('/');

Expand All @@ -69,6 +69,43 @@ export class Etcd3ServiceTest {
unit.number(nsStub.callCount).is(3);
}

/**
* Test `Etcd3Service` function _fixKey
*/
@test('- Test `Etcd3Service` function _fixKey')
testEtcd3ServiceFixKey() {
class Service extends Etcd3Service {
constructor(conf) {
super(conf);
}

fix(key) {
return this._fixKey(key);
}
}

const service = new Service({ config: {}, client: { namespace: () => {} } });
unit.string(service.fix('')).is('/');
unit.string(service.fix('/')).is('/');
unit.string(service.fix('/test')).is('test');
unit.string(service.fix('/test/')).is('test');
unit.string(service.fix('test')).is('test');

const service2 = new Service({ config: { basePath: '/bp/' }, client: { namespace: () => {} } });
unit.string(service2.fix('')).is('/');
unit.string(service2.fix('/')).is('/');
unit.string(service2.fix('/test')).is('test');
unit.string(service2.fix('/test/')).is('test');
unit.string(service2.fix('test')).is('test');

const service3 = new Service({ config: { basePath: '/bp' }, client: { namespace: () => {} } });
unit.string(service3.fix('')).is('/');
unit.string(service3.fix('/')).is('/');
unit.string(service3.fix('/test')).is('/test');
unit.string(service3.fix('/test/')).is('/test');
unit.string(service3.fix('test')).is('/test');
}

/**
* Test of `Etcd3Service` method get should return an error if ask for content type other than existing ones
*/
Expand Down Expand Up @@ -198,7 +235,7 @@ export class Etcd3ServiceTest {
.subscribe(
_ => {
unit.value(putStub.callCount).is(1);
unit.value(putStub.getCall(0).args[0]).is('key');
unit.value(putStub.getCall(0).args[0]).is('/key');

unit.value(valueStub.callCount).is(1);
unit.value(valueStub.getCall(0).args[0]).is(JSON.stringify({ test: 'micro' }));
Expand Down Expand Up @@ -231,7 +268,7 @@ export class Etcd3ServiceTest {
.subscribe(
_ => {
unit.value(putStub.callCount).is(1);
unit.value(putStub.getCall(0).args[0]).is('key');
unit.value(putStub.getCall(0).args[0]).is('/key');

unit.value(valueStub.callCount).is(1);
unit.value(valueStub.getCall(0).args[0]).is('value');
Expand Down Expand Up @@ -326,7 +363,7 @@ export class Etcd3ServiceTest {
.subscribe(
_ => {
unit.value(putStub.callCount).is(1);
unit.value(putStub.getCall(0).args[0]).is('key');
unit.value(putStub.getCall(0).args[0]).is('/key');

unit.value(valueStub.callCount).is(1);
unit.value(valueStub.getCall(0).args[0].toString('utf8')).is(buff.toString('utf8'));
Expand Down Expand Up @@ -359,7 +396,7 @@ export class Etcd3ServiceTest {
.subscribe(
_ => {
unit.value(putStub.callCount).is(1);
unit.value(putStub.getCall(0).args[0]).is('key');
unit.value(putStub.getCall(0).args[0]).is('/key');

unit.value(valueStub.callCount).is(1);
unit.value(valueStub.getCall(0).args[0]).is(10);
Expand Down Expand Up @@ -394,7 +431,7 @@ export class Etcd3ServiceTest {
unit.value(delStub.callCount).is(1);
unit.value(keyStub.callCount).is(1);

unit.value(keyStub.getCall(0).args[0]).is('key');
unit.value(keyStub.getCall(0).args[0]).is('/key');

done();
},
Expand Down Expand Up @@ -459,7 +496,7 @@ export class Etcd3ServiceTest {
unit.value(prefixStub.callCount).is(0);
unit.value(keyStub.callCount).is(1);

unit.value(keyStub.getCall(0).args[0]).is('key');
unit.value(keyStub.getCall(0).args[0]).is('/key');

unit.value(nsStub.callCount).is(1);

Expand All @@ -472,7 +509,7 @@ export class Etcd3ServiceTest {

unit.value(keyStub.callCount).is(1);
unit.value(prefixStub.callCount).is(1);
unit.value(prefixStub.getCall(0).args[0]).is('key');
unit.value(prefixStub.getCall(0).args[0]).is('/key');

unit.value(nsStub.callCount).is(1);

Expand Down Expand Up @@ -506,9 +543,9 @@ export class Etcd3ServiceTest {
.subscribe(
_ => {
unit.value(lockStub.callCount).is(3);
unit.value(lockStub.getCall(0).args[0]).is('key');
unit.value(lockStub.getCall(1).args[0]).is('key');
unit.value(lockStub.getCall(2).args[0]).is('key');
unit.value(lockStub.getCall(0).args[0]).is('/key');
unit.value(lockStub.getCall(1).args[0]).is('/key');
unit.value(lockStub.getCall(2).args[0]).is('/key');

unit.value(ttlStub.callCount).is(3);
unit.value(ttlStub.getCall(0).args[0]).is(1);
Expand Down Expand Up @@ -582,9 +619,9 @@ export class Etcd3ServiceTest {
unit.value(leaseStub.getCall(2).args[0]).is(1);

unit.value(putStub.callCount).is(3);
unit.value(putStub.getCall(0).args[0]).is('key');
unit.value(putStub.getCall(1).args[0]).is('key');
unit.value(putStub.getCall(2).args[0]).is('key');
unit.value(putStub.getCall(0).args[0]).is('/key');
unit.value(putStub.getCall(1).args[0]).is('/key');
unit.value(putStub.getCall(2).args[0]).is('/key');

unit.value(valueStub.callCount).is(3);
unit.value(valueStub.getCall(0).args[0]).is('value');
Expand Down

0 comments on commit 3ff96da

Please sign in to comment.