diff --git a/src/clients/HookClient.js b/src/clients/HookClient.js index 4a80138..5a593cd 100644 --- a/src/clients/HookClient.js +++ b/src/clients/HookClient.js @@ -70,7 +70,12 @@ class HookClient { ).then(response => { resolve(response.body); }).catch(errResponse => { - reject(errResponse.message); + if (errResponse.statusCode === 404) { + reject('This is not the hook you are looking for.'); + } + else { + reject(errResponse.message); + } }); }); } @@ -108,8 +113,15 @@ class HookClient { } ).then(response => { resolve(response.body); - }).catch(err => { - reject(err.message); + }).catch(errResponse => { + if (errResponse.statusCode === 404) { + reject('The hook you want to update does not exist.'); + } + else if (errResponse.statusCode === 409){ + reject('A hook with this name already exists.'); + } else { + reject(errResponse.message); + } }); }); } diff --git a/tests/clients/HookClient.spec.js b/tests/clients/HookClient.spec.js index df117b6..abeacb9 100644 --- a/tests/clients/HookClient.spec.js +++ b/tests/clients/HookClient.spec.js @@ -129,6 +129,33 @@ describe('hookClient', () => { }); }); + it('should return an error when request fails with 404', done => { + // Given + const error = { statusCode: 404, message: 'Error !' }; + hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error)); + + // When / Then + hookClient.getHook(token, hookName).then(result => { + done('should have failed'); + }).catch(err => { + expect(err).to.be.equals('This is not the hook you are looking for.'); + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce; + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly( + { + method: 'GET', + path: '/api/dispatcher/hooks/:hook' + }, + { + headers: { 'x-auth-token': token }, + pathVariables: { + hook: hookName + } + } + ); + done(); + }); + }); + it('should return specified hook when request succeeds', done => { // Given const response = { body: hook }; @@ -218,6 +245,56 @@ describe('hookClient', () => { }); }); + it('should return an error message when request fails with 404', done => { + // Given + const error = { statusCode: 404, message: 'Error !' }; + hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error)); + + // When / Then + hookClient.updateHook(token, hookName, newHook).then(result => { + done('should have failed'); + }).catch(err => { + expect(err).to.be.equals('The hook you want to update does not exist.'); + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce; + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly( + { + method: 'PUT', + path: '/api/dispatcher/hooks/:hook' + }, { + headers: { 'x-auth-token': token }, + pathVariables: { hook : hookName}, + body: newHook + } + ); + done(); + }); + }); + + it('should return an error message when request fails with 409', done => { + // Given + const error = { statusCode: 409, message: 'Error !' }; + hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error)); + + // When / Then + hookClient.updateHook(token, hookName, newHook).then(result => { + done('should have failed'); + }).catch(err => { + expect(err).to.be.equals('A hook with this name already exists.'); + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce; + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly( + { + method: 'PUT', + path: '/api/dispatcher/hooks/:hook' + }, { + headers: { 'x-auth-token': token }, + pathVariables: { hook : hookName}, + body: newHook + } + ); + done(); + }); + }); + it('should return the updated hook', done => { // Given const response = { body: newHook };