modify-response.test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict'
  2. const tap = require('tap')
  3. const { BerReader } = require('@ldapjs/asn1')
  4. const { operations, resultCodes } = require('@ldapjs/protocol')
  5. const {
  6. modifyResponseBytes
  7. } = require('./_fixtures/message-byte-arrays')
  8. const ModifyResponse = require('./modify-response')
  9. tap.test('basic', async t => {
  10. const res = new ModifyResponse()
  11. t.equal(res.protocolOp, operations.LDAP_RES_MODIFY)
  12. t.equal(res.type, 'ModifyResponse')
  13. })
  14. tap.test('toBer', t => {
  15. tap.test('returns basic bytes', async t => {
  16. const res = new ModifyResponse({
  17. messageId: 2,
  18. status: resultCodes.SUCCESS
  19. })
  20. const ber = res.toBer()
  21. const expected = Buffer.from(modifyResponseBytes)
  22. t.equal(expected.compare(ber.buffer), 0)
  23. })
  24. t.end()
  25. })
  26. tap.test('#parseToPojo', t => {
  27. t.test('parses a basic object', async t => {
  28. const bytes = modifyResponseBytes.slice(5)
  29. const reader = new BerReader(Buffer.from(bytes))
  30. const pojo = ModifyResponse.parseToPojo(reader)
  31. t.strictSame(pojo, {
  32. status: resultCodes.SUCCESS,
  33. matchedDN: '',
  34. diagnosticMessage: '',
  35. referrals: []
  36. })
  37. })
  38. t.test('throws if protocol op is wrong', async t => {
  39. const bytes = modifyResponseBytes.slice(5)
  40. bytes[0] = 0x68
  41. const reader = new BerReader(Buffer.from(bytes))
  42. t.throws(
  43. () => ModifyResponse.parseToPojo(reader),
  44. Error('found wrong protocol operation: 0x68')
  45. )
  46. })
  47. t.end()
  48. })