OptionsNormalizer.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { IInputOptions } from '../../src/interfaces/options/IInputOptions';
  2. import { IOptions } from '../../src/interfaces/options/IOptions';
  3. import { DEFAULT_PRESET } from '../../src/preset-options/DefaultPreset';
  4. import { Options } from '../../src/options/Options';
  5. import { OptionsNormalizer } from '../../src/options/OptionsNormalizer';
  6. const assert: Chai.AssertStatic = require('chai').assert;
  7. /**
  8. * @param optionsPreset
  9. * @returns {IOptions}
  10. */
  11. function getNormalizedOptions (optionsPreset: IInputOptions): IInputOptions {
  12. const options: IOptions = new Options(optionsPreset);
  13. return OptionsNormalizer.normalizeOptions(options);
  14. }
  15. describe('OptionsNormalizer', () => {
  16. describe('normalizeOptions (options: IObfuscatorOptions): IObfuscatorOptions', () => {
  17. let optionsPreset: IInputOptions,
  18. expectedOptionsPreset: IInputOptions;
  19. it('should normalize options preset: domainLockRule', () => {
  20. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  21. domainLock: ['//localhost:9000', 'https://google.ru/abc?cde=fgh']
  22. });
  23. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  24. domainLock: ['localhost', 'google.ru']
  25. });
  26. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  27. });
  28. it('should normalize options preset: selfDefendingRule', () => {
  29. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  30. compact: false
  31. });
  32. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  33. compact: true
  34. });
  35. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  36. });
  37. it('should normalize options preset: sourceMapBaseUrlRule #1', () => {
  38. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  39. sourceMapBaseUrl: 'http://localhost:9000',
  40. });
  41. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  42. sourceMapBaseUrl: '',
  43. });
  44. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  45. });
  46. it('should normalize options preset: sourceMapBaseUrlRule #2', () => {
  47. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  48. sourceMapBaseUrl: 'http://localhost:9000',
  49. sourceMapFileName: '/outputSourceMapName.map'
  50. });
  51. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  52. sourceMapBaseUrl: 'http://localhost:9000/',
  53. sourceMapFileName: 'outputSourceMapName.js.map'
  54. });
  55. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  56. });
  57. it('should normalize options preset: sourceMapFileNameRule', () => {
  58. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  59. sourceMapBaseUrl: 'http://localhost:9000',
  60. sourceMapFileName: '//outputSourceMapName',
  61. });
  62. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  63. sourceMapBaseUrl: 'http://localhost:9000/',
  64. sourceMapFileName: 'outputSourceMapName.js.map',
  65. });
  66. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  67. });
  68. it('should normalize options preset: stringArrayRule', () => {
  69. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  70. stringArray: false,
  71. stringArrayEncoding: 'rc4',
  72. stringArrayThreshold: 0.5,
  73. rotateStringArray: true
  74. });
  75. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  76. stringArray: false,
  77. stringArrayEncoding: false,
  78. stringArrayThreshold: 0,
  79. rotateStringArray: false
  80. });
  81. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  82. });
  83. it('should normalize options preset: stringArrayEncodingRule', () => {
  84. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  85. stringArrayEncoding: true
  86. });
  87. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  88. stringArrayEncoding: 'base64'
  89. });
  90. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  91. });
  92. it('should normalize options preset: stringArrayThresholdRule', () => {
  93. optionsPreset = Object.assign({}, DEFAULT_PRESET, {
  94. rotateStringArray: true,
  95. stringArray: true,
  96. stringArrayThreshold: 0
  97. });
  98. expectedOptionsPreset = Object.assign({}, DEFAULT_PRESET, {
  99. rotateStringArray: false,
  100. stringArray: false,
  101. stringArrayThreshold: 0
  102. });
  103. assert.deepEqual(getNormalizedOptions(optionsPreset), expectedOptionsPreset);
  104. });
  105. });
  106. });