|
| 1 | +import 'mocha'; |
| 2 | +import assert from 'assert'; |
| 3 | +import { preprocessHarEntries, preprocess, trackingAPIComparisonSchema, HarEntry} from './har' |
| 4 | +import fs from 'fs'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +describe('preprocessHarEntries', () => { |
| 8 | + // Recordings 1 & 2 were generated on the same website using the same ajs, at different time |
| 9 | + // Recording 3 was generated on the same website as the first two, using a different AJS configuration that |
| 10 | + // produces a different tracking api payload |
| 11 | + |
| 12 | + it('should return true for identical recordings', () => { |
| 13 | + const a = preprocessHarEntries(fs.readFileSync(path.join(__dirname, 'test_data', 'recording1.har'), 'utf8')); |
| 14 | + const b = preprocessHarEntries(fs.readFileSync(path.join(__dirname, 'test_data', 'recording1.har'), 'utf8')); |
| 15 | + assert.deepEqual(a, b) |
| 16 | + }); |
| 17 | + |
| 18 | + it('should return true for recordings generated by the same AJS on the same website', () => { |
| 19 | + const a = preprocessHarEntries(fs.readFileSync(path.join(__dirname, 'test_data', 'recording1.har'), 'utf8')); |
| 20 | + const b = preprocessHarEntries(fs.readFileSync(path.join(__dirname, 'test_data', 'recording2.har'), 'utf8')); |
| 21 | + assert.deepEqual(a, b) |
| 22 | + }); |
| 23 | + |
| 24 | + it('should return false for recordings generated by different AJS configurations on the same website', () => { |
| 25 | + const a = preprocessHarEntries(fs.readFileSync(path.join(__dirname, 'test_data', 'recording1.har'), 'utf8')); |
| 26 | + const b = preprocessHarEntries(fs.readFileSync(path.join(__dirname, 'test_data', 'recording3.har'), 'utf8')); |
| 27 | + assert.notDeepEqual(a, b) |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe('preprocess', () => { |
| 32 | + it('should ignore user-agent headers', () => { |
| 33 | + const a = {request: {headers: [{name: 'user-agent', value: 'foo'}]}} as HarEntry |
| 34 | + const b = {request: {headers: [{name: 'user-agent', value: 'bar'}]}} as HarEntry |
| 35 | + assert.deepEqual( |
| 36 | + preprocess(a, trackingAPIComparisonSchema), |
| 37 | + preprocess(b, trackingAPIComparisonSchema) |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should ignore user-agent header #2', () => { |
| 42 | + const a = {request: {headers: [{name: 'user-agent', value: 'foo'}]}} as HarEntry |
| 43 | + const b = {request: {headers: [{name: 'user-agent', value: 'foo'}]}} as HarEntry |
| 44 | + assert.deepEqual( |
| 45 | + preprocess(a, trackingAPIComparisonSchema), |
| 46 | + preprocess(b, trackingAPIComparisonSchema) |
| 47 | + ) |
| 48 | + }) |
| 49 | + |
| 50 | + it('compares object using a schema that contains properties we want to ignore / only care about existene', () => { |
| 51 | + const a = { b: 'foo', c: { d: 1 } }; |
| 52 | + const b = { a: 1, b: 2, c: { d: 1 } }; |
| 53 | + const schema = { ignored: ['a'], exists: ['b'] }; |
| 54 | + assert.deepEqual( |
| 55 | + preprocess(a, schema), |
| 56 | + preprocess(b, schema) |
| 57 | + ) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('ignore', () => { |
| 61 | + it('should ignore top-level properties', () => { |
| 62 | + const a = { a: 1, b: 2, c: { d: 1 } }; |
| 63 | + const b = { a: 1, b: 'foobar', c: { d: 1 } }; |
| 64 | + const schema = { ignored: ['b'] }; |
| 65 | + assert.deepEqual( |
| 66 | + preprocess(a, schema), |
| 67 | + preprocess(b, schema) |
| 68 | + ) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should ignore non top-level properties', () => { |
| 72 | + const a = { a: 1, b: 2, c: { d: 1 } }; |
| 73 | + const b = { a: 1, b: 2, c: { d: 123 } }; |
| 74 | + const schema = { ignored: ['c.d'] }; |
| 75 | + assert.deepEqual( |
| 76 | + preprocess(a, schema), |
| 77 | + preprocess(b, schema) |
| 78 | + ) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should ignored c.d but not entire c', () => { |
| 82 | + const a = { b: 2, c: { d: 2 } }; |
| 83 | + const b = { a: 1, b: 2, c: { d: 1 } }; |
| 84 | + const schema = { ignored: ['a', 'c.d'] }; |
| 85 | + assert.deepEqual( |
| 86 | + preprocess(a, schema), |
| 87 | + preprocess(b, schema) |
| 88 | + ) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should ignore c.d but not entire c #2', () => { |
| 92 | + const a = { b: 2 } |
| 93 | + const b = { a: 1, b: 2, c: { d: 1 } } |
| 94 | + const schema = { ignored: ['a', 'c.d'] } |
| 95 | + assert.notDeepEqual( |
| 96 | + preprocess(a, schema), |
| 97 | + preprocess(b, schema) |
| 98 | + ) |
| 99 | + }) |
| 100 | + |
| 101 | + it('should ignore entire c and its own properties', () => { |
| 102 | + const a = { a: 1, b: 2, c: { g: 1 } } |
| 103 | + const b = { a: 1, b: 2, c: { d: 1, e: 2, f: {} } } |
| 104 | + const schema = { ignored: ['c'] } |
| 105 | + assert.deepEqual( |
| 106 | + preprocess(a, schema), |
| 107 | + preprocess(b, schema) |
| 108 | + ) |
| 109 | + }) |
| 110 | + }) // describe ignore |
| 111 | + |
| 112 | + describe('exists', () => { |
| 113 | + it('should only check for existence of properties and ignore their values', () => { |
| 114 | + const a = { a: 123, b: 2, c: { d: 1 } } |
| 115 | + const b = { a: 1, b: 2, c: { d: 1 } } |
| 116 | + const schema = { exists: ['a'] } |
| 117 | + assert.deepEqual( |
| 118 | + preprocess(a, schema), |
| 119 | + preprocess(b, schema) |
| 120 | + ) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should only check for existence of properties and ignore their values #2', () => { |
| 124 | + const a = { a: null, b: 2, c: { d: 1 } } |
| 125 | + const b = { a: 1, b: 2, c: { d: 1 } } |
| 126 | + const schema = { exists: ['a'] } |
| 127 | + assert.deepEqual( |
| 128 | + preprocess(a, schema), |
| 129 | + preprocess(b, schema) |
| 130 | + ) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should return false when only one object is missing the property', () => { |
| 134 | + const a = { b: 2, c: { d: 1 } } |
| 135 | + const b = { a: 1, b: 2, c: { d: 1 } } |
| 136 | + const schema = { exists: ['a'] } |
| 137 | + assert.notDeepEqual( |
| 138 | + preprocess(a, schema), |
| 139 | + preprocess(b, schema) |
| 140 | + ) |
| 141 | + }) |
| 142 | + |
| 143 | + it('should work for multiple properties and nested props', () => { |
| 144 | + const a ={ a: 1, b: 2, c: { d: 1 } } |
| 145 | + const b ={ a: 1, b: 234, c: { d: 123 } } |
| 146 | + const schema ={ exists: ['b', 'c.d'] } |
| 147 | + assert.deepEqual( |
| 148 | + preprocess(a, schema), |
| 149 | + preprocess(b, schema) |
| 150 | + ) |
| 151 | + }) |
| 152 | + |
| 153 | + it('should work deeply nested objects', () => { |
| 154 | + const a = { a: 1, b: 2, c: { d: 2 } } |
| 155 | + const b = { a: 1, b: 2, c: { d: { e: { f: {} } } } } |
| 156 | + const schema = { exists: ['c'] } |
| 157 | + assert.deepEqual( |
| 158 | + preprocess(a, schema), |
| 159 | + preprocess(b, schema) |
| 160 | + ) |
| 161 | + }) |
| 162 | + |
| 163 | + }) // describe exists |
| 164 | +}) // describe preprocess |
0 commit comments