Skip to content

Commit

Permalink
unit test for getRabbitMQUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Scott committed Jan 3, 2018
1 parent 83ad210 commit b606b57
Show file tree
Hide file tree
Showing 7 changed files with 4,405 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ node_modules
.DS_Store
.queues
npm-debug.log
coverage
28 changes: 28 additions & 0 deletions README.md
Expand Up @@ -43,6 +43,34 @@ bus.listen('my.event', { ack: true }, function (event) {

Message acknowledgement is suited for use in load distribution scenarios.

## Authentication (RabbitMQ Bus)

### Fully qualified url

You may authenticate by providing `url` as an option when initializin the bus, or setting RABBITMQ_URL as an environment variable. RabbitMQ uses basic auth url format for authentication.

```
var bus = servicebus.bus({
url: "amqp://user:pass@localhost:5672,
})
```

### config options

Alternatively, you may provide a `user`, `password`, `host` (optional, default = 'localhost'), and `port` (optional, default = 5672), and servicebus will construct the url before passing it to RabbitMQ.

```
var bus = servicebus.bus({
user: 'rabbitUser',
password: 'test1234',
host: '1.1.1.1'
port: '5555'
})
```

NOTE:
If `url` and `user/password` are provided, the `url` will be used.

# Publish / Subscribe

Servicebus can also send messages from 1:N processes in a fan-out architecture. In this pattern, one sender publishes a message and any number of subscribers can receive. The pattern for usage looks very similar to send/listen:
Expand Down
14 changes: 14 additions & 0 deletions __tests__/rabbitmq/bus.js
@@ -0,0 +1,14 @@
const getRabbitMQUrl = require('rabbitmq/bus').getRabbitMQUrl

describe('getRabbitMQUrl', () => {
it('determines url based on options available', () => {
let url = getRabbitMQUrl({ url: 'amqp://rabbitmq:5672' })
expect(url).toBe('amqp://rabbitmq:5672')

url = getRabbitMQUrl({ user: 'pat', password: 'test1234' })
expect(url).toBe('amqp://pat:test1234@localhost:5672')

url = getRabbitMQUrl({ user: 'pat', password: 'test1234', host: 'myhost', port: 5555 })
expect(url).toBe('amqp://pat:test1234@myhost:5555')
})
})
2 changes: 1 addition & 1 deletion bus/rabbitmq/bus.js
Expand Up @@ -10,7 +10,7 @@ var amqp = require('amqplib'),
Queue = require('./queue'),
util = require('util');

function getRabbitMQUrl (options) {
module.exports.getRabbitMQUrl = function getRabbitMQUrl (options) {
var rabbitUrl = options.url || process.env.RABBITMQ_URL;
if (!rabbitUrl) {
// see if url can be built with user and password
Expand Down
11 changes: 11 additions & 0 deletions jest.json
@@ -0,0 +1,11 @@
{
"roots": ["__tests__"],
"testEnvironment": "node",
"modulePaths": [
"bus",
"/node_modules/"
],
"collectCoverageFrom" : [
"bus/**/*.js"
]
}

0 comments on commit b606b57

Please sign in to comment.