Skip to content

Commit

Permalink
Merge pull request #19 from ilves/jsumners
Browse files Browse the repository at this point in the history
Handle connection timeout event
  • Loading branch information
jsumners authored Oct 3, 2017
2 parents cbd0ea4 + 2c15949 commit 47406cb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/activedirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@ ActiveDirectory.prototype.authenticate = function authenticate (username, passwo
}

const client = utils.createClient(null, null, this)
client.on('connectTimeout', (err) => {
this.emit('error', err)
return callback(err, false)
})
client.on('error', (err) => {
// only used on socket connection failure since it doesn't invoke bind cb
this.emit('error', err)
Expand Down
4 changes: 4 additions & 0 deletions lib/components/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ function Searcher (opts) {
this.rangeProcessing = false

this.client = utils.createClient(ad.url || ad.opts.url, this.ldapOpts, ad)
this.client.on('connectTimeout', (err) => {
// to handle connection errors
this.callback(err)
})
this.client.on('error', (err) => {
// to handle connection errors
this.callback(err)
Expand Down
12 changes: 12 additions & 0 deletions test/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable no-unused-expressions */

const expect = require('chai').expect
const ldapjs = require('ldapjs')
const ActiveDirectory = require('../index')
const config = require('./config')

Expand Down Expand Up @@ -215,5 +216,16 @@ describe('find Method', function () {
done()
})
})

it('should return err (ConnectionError) when connection timeouts', function (done) {
new ActiveDirectory({
url: 'ldap://example.com',
connectTimeout: 100
}).find({}, function (err, result) {
expect(err).to.be.an.instanceOf(ldapjs.ConnectionError)
expect(result).to.be.undefined
done()
})
})
})
})
13 changes: 13 additions & 0 deletions test/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable no-unused-expressions */

const expect = require('chai').expect
const ldapjs = require('ldapjs')
const ActiveDirectory = require('../index')

describe('Network Connections', function () {
Expand Down Expand Up @@ -33,5 +34,17 @@ describe('Network Connections', function () {
done()
})
})

it('should return err (ConnectionError) when connection timeouts', function (done) {
const ad = new ActiveDirectory({
url: 'ldap://example.com',
connectTimeout: 1
})
ad.authenticate(username, password, function (err, auth) {
expect(err).to.be.an.instanceOf(ldapjs.ConnectionError)
expect(auth).to.be.false
done()
})
})
})
})

0 comments on commit 47406cb

Please sign in to comment.