diff --git a/src/discovery/HazelcastCloudDiscovery.ts b/src/discovery/HazelcastCloudDiscovery.ts index 362c21a0d..ecdd549f6 100644 --- a/src/discovery/HazelcastCloudDiscovery.ts +++ b/src/discovery/HazelcastCloudDiscovery.ts @@ -73,6 +73,10 @@ export class HazelcastCloudDiscovery { let dataAsAString = ''; const req = get(endpointUrlOptions, (res: IncomingMessage) => { + if (res.statusCode != 200) { + deferred.reject(new HazelcastError('Your cluster discovery token is invalid.')); + return; + } res.setEncoding('utf8'); res.on('data', (chunk) => { dataAsAString += chunk; diff --git a/test/unit/discovery/CloudDiscoveryInvalidTokenTest.js b/test/unit/discovery/CloudDiscoveryInvalidTokenTest.js new file mode 100644 index 000000000..8a1e252c3 --- /dev/null +++ b/test/unit/discovery/CloudDiscoveryInvalidTokenTest.js @@ -0,0 +1,24 @@ +'use strict'; + +const { expect } = require('chai'); +const {HazelcastCloudDiscovery} = require('../../../lib/discovery/HazelcastCloudDiscovery'); +const { HazelcastError } = require('../../../lib'); +const { ClientConfigImpl } = require('../../../lib/config/Config'); +const {getRejectionReasonOrThrow} = require('../../TestUtil'); + +describe('Cloud Discovery Invalid Token Test', function () { + it('Cloud Discovery should throw proper error with an invalid token', async function () { + const clientConfig = new ClientConfigImpl(); + const cloudDiscovery = new HazelcastCloudDiscovery( + clientConfig.properties['hazelcast.client.cloud.url'], 2 << 31 + ); + + try { + const error = await getRejectionReasonOrThrow(cloudDiscovery.callService.bind(cloudDiscovery)); + expect(error).to.be.instanceof(HazelcastError); + expect(error.message).to.include('discovery token is invalid'); + } catch (err) { + console.log(err); + } + }); +});