unbind-request.test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict'
  2. const tap = require('tap')
  3. const { operations } = require('@ldapjs/protocol')
  4. const UnbindRequest = require('./unbind-request')
  5. const { unbindRequestBytes } = require('./_fixtures/message-byte-arrays')
  6. const { BerReader, BerWriter } = require('@ldapjs/asn1')
  7. tap.test('basic', t => {
  8. t.test('constructor no args', async t => {
  9. const req = new UnbindRequest()
  10. t.strictSame(req.pojo, {
  11. messageId: 1,
  12. protocolOp: operations.LDAP_REQ_UNBIND,
  13. type: 'UnbindRequest',
  14. controls: []
  15. })
  16. t.equal(req.type, 'UnbindRequest')
  17. })
  18. t.end()
  19. })
  20. tap.test('_toBer', t => {
  21. tap.test('converts instance to BER', async t => {
  22. const req = new UnbindRequest()
  23. const writer = new BerWriter()
  24. req._toBer(writer)
  25. t.equal(
  26. Buffer.from(unbindRequestBytes.slice(5)).compare(writer.buffer),
  27. 0
  28. )
  29. })
  30. t.end()
  31. })
  32. tap.test('_pojo', t => {
  33. t.test('returns a pojo representation', async t => {
  34. const req = new UnbindRequest()
  35. t.strictSame(req._pojo(), {})
  36. })
  37. t.end()
  38. })
  39. tap.test('#parseToPojo', t => {
  40. t.test('throws if operation incorrect', async t => {
  41. const reqBuffer = Buffer.from(unbindRequestBytes)
  42. reqBuffer[5] = 0x61
  43. const reader = new BerReader(reqBuffer)
  44. reader.readSequence()
  45. reader.readInt()
  46. t.throws(
  47. () => UnbindRequest.parseToPojo(reader),
  48. Error('found wrong protocol operation: 0x61')
  49. )
  50. })
  51. t.test('returns a pojo representation', async t => {
  52. const reqBuffer = Buffer.from(unbindRequestBytes)
  53. const reader = new BerReader(reqBuffer)
  54. reader.readSequence()
  55. reader.readInt()
  56. const pojo = UnbindRequest.parseToPojo(reader)
  57. t.equal(pojo.protocolOp, operations.LDAP_REQ_UNBIND)
  58. })
  59. t.end()
  60. })