|
| 1 | +import { DirectiveMap } from '../src/index.js'; |
| 2 | + |
| 3 | +function isObject (obj: unknown): obj is Record<PropertyKey,unknown> { |
| 4 | + return Object.prototype.toString.call(obj) === '[object Object]'; |
| 5 | +} |
| 6 | +function hasOwnProperty<T> (obj: T, prop: PropertyKey): prop is keyof T { |
| 7 | + return Object.prototype.hasOwnProperty.call(obj, prop); |
| 8 | +} |
| 9 | + |
| 10 | +describe('DirectiveMap.get()',() => { |
| 11 | + describe('Dynamic Options',() => { |
| 12 | + it('Handles Hostname/URL Source',() => { |
| 13 | + const src = DirectiveMap.get('child-src'); |
| 14 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 15 | + const start: any = undefined; |
| 16 | + const result = src?.values.reduce((_,v) => { |
| 17 | + if ( |
| 18 | + isObject(v) && |
| 19 | + hasOwnProperty(v,'displayName') && |
| 20 | + v.displayName === 'Hostname/URL Source' |
| 21 | + ) { |
| 22 | + return v?.compose?.({ |
| 23 | + 'Hostname': 'example.com', |
| 24 | + 'Port': 443, |
| 25 | + 'Protocol':'https://', |
| 26 | + }); |
| 27 | + } |
| 28 | + return _; |
| 29 | + },start); |
| 30 | + expect(result).toBe('https://example.com:443'); |
| 31 | + }); |
| 32 | + it('Crypto Nonce/Hash Source',() => { |
| 33 | + const src = DirectiveMap.get('child-src'); |
| 34 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 35 | + const start: any = undefined; |
| 36 | + const result = src?.values.reduce((_,v) => { |
| 37 | + if ( |
| 38 | + isObject(v) && |
| 39 | + hasOwnProperty(v,'displayName') && |
| 40 | + v.displayName === 'Crypto Nonce/Hash Source' |
| 41 | + ) { |
| 42 | + return v?.compose?.({ |
| 43 | + 'Algorithm':'sha256', |
| 44 | + 'Hash':'SomeBase64String', |
| 45 | + }); |
| 46 | + } |
| 47 | + return _; |
| 48 | + },start); |
| 49 | + expect(result).toBe('sha256-SomeBase64String'); |
| 50 | + }); |
| 51 | + it('Handles URI Source',() => { |
| 52 | + const src = DirectiveMap.get('child-src'); |
| 53 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 54 | + const start: any = undefined; |
| 55 | + const result = src?.values.reduce((_,v) => { |
| 56 | + if ( |
| 57 | + isObject(v) && |
| 58 | + hasOwnProperty(v,'displayName') && |
| 59 | + v.displayName === 'URI Source' |
| 60 | + ) { |
| 61 | + return v?.compose?.({ |
| 62 | + 'Beginning Delineator':'/', |
| 63 | + 'Remaining Path':'send/reports/to', |
| 64 | + }); |
| 65 | + } |
| 66 | + return _; |
| 67 | + },start); |
| 68 | + expect(result).toBe('/send/reports/to'); |
| 69 | + }); |
| 70 | + it('Handles Plugin MIME Type Source',() => { |
| 71 | + const src = DirectiveMap.get('child-src'); |
| 72 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 73 | + const start: any = undefined; |
| 74 | + const result = src?.values.reduce((_,v) => { |
| 75 | + if ( |
| 76 | + isObject(v) && |
| 77 | + hasOwnProperty(v,'displayName') && |
| 78 | + v.displayName === 'Plugin MIME Type Source' |
| 79 | + ) { |
| 80 | + return v?.compose?.({ |
| 81 | + 'MIME Category':'application', |
| 82 | + 'MIME Implementation':'xml', |
| 83 | + }); |
| 84 | + } |
| 85 | + return _; |
| 86 | + },start); |
| 87 | + expect(result).toBe('application/xml'); |
| 88 | + }); |
| 89 | + it('Handles Any String',() => { |
| 90 | + const src = DirectiveMap.get('child-src'); |
| 91 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 92 | + const start: any = undefined; |
| 93 | + const result = src?.values.reduce((_,v) => { |
| 94 | + if ( |
| 95 | + isObject(v) && |
| 96 | + hasOwnProperty(v,'displayName') && |
| 97 | + v.displayName === 'Any String' |
| 98 | + ) { |
| 99 | + return v?.compose?.({ |
| 100 | + 'String':'hello world', |
| 101 | + }); |
| 102 | + } |
| 103 | + return _; |
| 104 | + },start); |
| 105 | + expect(result).toBe('hello world'); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments