Skip to content

Commit

Permalink
Remove REGEX for uri
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinegomez authored and Antoine Gomez committed May 31, 2018
1 parent 9fad64e commit f04389e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 42 deletions.
9 changes: 3 additions & 6 deletions src/module/managers/connection-manager.ts
Expand Up @@ -7,8 +7,6 @@ import { events } from '../events';
import { ChannelStore } from './channel-store';
import { ChannelManager } from './channel-manager';

export const REGEX_URI = /^amqp:\/\/([^@\n]+:[^@\n]+@)?(\w+)(:?)(\d{0,6})(\/[\w%]+)?(\?(?:&?[^=&\s]*=[^=&\s]*)+)?$/;

const debug = require('debug')('hapiness:rabbitmq');

export class ConnectionManager extends EventEmitter {
Expand Down Expand Up @@ -38,10 +36,6 @@ export class ConnectionManager extends EventEmitter {
}

if (this._options.uri) {
if (!this._options.uri.match(REGEX_URI)) {
throw new Error('Invalid uri');
}

this._uri = this._options.uri;
} else {
const port = this._options.port || 5672;
Expand All @@ -53,7 +47,9 @@ export class ConnectionManager extends EventEmitter {
}

// Will block new connection if SIGTERM is received
/* istanbul ignore next */
process.once('SIGTERM', () => this._isSIGTERMReceived = true);
/* istanbul ignore next */
process.once('SIGINT', () => this._isSIGTERMReceived = true);

this.setDefaultPrefetch(this._options.default_prefetch);
Expand Down Expand Up @@ -109,6 +105,7 @@ export class ConnectionManager extends EventEmitter {
return Observable.of(null);
}

/* istanbul ignore next */
if (this._isSIGTERMReceived) {
return Observable.of(null);
}
Expand Down
19 changes: 12 additions & 7 deletions test/unit/extension/init-extension.test.ts
Expand Up @@ -23,13 +23,18 @@ export class InitExtensionUnitTest {
private userQueue;

before() {
this.ch = new ChannelManager(<any>{connection: {}});
this.ch['ch'] = <any>new ChannelMock();
this.userQueue = new UserQueue();
this.queueWrapper = new QueueWrapper(this.userQueue, extractMetadataByDecorator(UserQueue, 'Queue'));
this.messageRouter = new MessageRouter();
this.queue = new QueueManager(this.ch, this.queueWrapper);
unit.spy(this.userQueue, 'onMessage');
try {
const connection = new ConnectionManagerMock();
this.ch = new ChannelManager(connection);
this.ch['ch'] = <any>new ChannelMock();
this.userQueue = new UserQueue();
this.queueWrapper = new QueueWrapper(this.userQueue, extractMetadataByDecorator(UserQueue, 'Queue'));
this.messageRouter = new MessageRouter();
this.queue = new QueueManager(this.ch, this.queueWrapper);
unit.spy(this.userQueue, 'onMessage');
} catch (err) {
console.log(err.stack);
}
}

after() {
Expand Down
29 changes: 1 addition & 28 deletions test/unit/managers/connection.test.ts
Expand Up @@ -49,41 +49,14 @@ export class ConnectionUnitTest {
});
}

@test(' - Test options.uri')
testOptionsUri() {
const urisOk = [
'amqp://localhost',
'amqp://hello:world@localhost',
'amqp://hello:world@localhost:98798',
'amqp://hello:world@localhost:98798/vhost',
'amqp://hello:world@localhost:98798/%2Fvhost',
'amqp://hello:world@localhost:98798'
];

const urisNOk = ['not_good', ' amqp://localhost', 'amqp://xxx:zzzzz@', 'amqp://xxx:zzzzz#/322d'];

urisOk.forEach(uri => {
const instance = new ConnectionManager({ uri });
unit.object(instance).isInstanceOf(ConnectionManager);
});

urisNOk.forEach(uri => {
unit
.exception(_ => {
unit.when('Invalid uri', new ConnectionManager({ uri }));
})
.isInstanceOf(Error)
.hasProperty('message', 'Invalid uri');
});
}

@test(' - Test options')
testOptions() {
const options = [
[{ login: 'keyboard', password: 'cat' }, 'amqp://keyboard:cat@localhost:5672'],
[{ retry: { maximum_attempts: 0 } }, 'amqp://localhost:5672'],
[{ params: { heartBeat: 30 } }, 'amqp://localhost:5672?heartBeat=30'],
[{ params: { heartBeat: 30 }, vhost: '/my_vhost' }, 'amqp://localhost:5672/%2Fmy_vhost?heartBeat=30'],
[{ uri: 'amqp://localhost:5672/%2Fmy_vhost?heartBeat=30' }, 'amqp://localhost:5672/%2Fmy_vhost?heartBeat=30'],
[undefined, 'amqp://localhost:5672']
];

Expand Down
3 changes: 2 additions & 1 deletion test/unit/managers/queue.test.ts
Expand Up @@ -11,6 +11,7 @@ import { UserExchange } from '../../fixtures/Exchanges';
import { generateMessage } from '../../mocks/Message';
import { extractMetadataByDecorator } from '@hapiness/core';
import { MessageStore } from '../../../src';
import { ConnectionManagerMock } from '../../mocks/ConnectionManager';

@suite('- Unit Queue')
export class QueueServiceUnitTest {
Expand All @@ -31,7 +32,7 @@ export class QueueServiceUnitTest {
}

before() {
this.ch = new ChannelManager(<any>{ connection: {}});
this.ch = new ChannelManager(new ConnectionManagerMock());
this.ch.setChannel(new ChannelMock());
unit.spy(this.ch.getChannel(), 'assertQueue');
unit.spy(this.ch.getChannel(), 'bindQueue');
Expand Down

0 comments on commit f04389e

Please sign in to comment.