mirror of https://github.com/abpframework/abp.git
3 changed files with 56 additions and 0 deletions
@ -0,0 +1,17 @@ |
|||||
|
export class CrossOriginStrategy { |
||||
|
constructor(public crossorigin: 'anonymous' | 'use-credentials', public integrity?: string) {} |
||||
|
|
||||
|
setCrossOrigin<T extends HTMLElement>(element: T) { |
||||
|
if (this.integrity) element.setAttribute('integrity', this.integrity); |
||||
|
element.setAttribute('crossorigin', this.crossorigin); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
export const CROSS_ORIGIN_STRATEGY = { |
||||
|
Anonymous(integrity?: string) { |
||||
|
return new CrossOriginStrategy('anonymous', integrity); |
||||
|
}, |
||||
|
UseCredentials(integrity?: string) { |
||||
|
return new CrossOriginStrategy('use-credentials', integrity); |
||||
|
}, |
||||
|
}; |
||||
@ -1 +1,2 @@ |
|||||
|
export * from './cross-origin.strategy'; |
||||
export * from './dom.strategy'; |
export * from './dom.strategy'; |
||||
|
|||||
@ -0,0 +1,38 @@ |
|||||
|
import { CrossOriginStrategy, CROSS_ORIGIN_STRATEGY } from '../strategies'; |
||||
|
import { uuid } from '../utils'; |
||||
|
|
||||
|
describe('CrossOriginStrategy', () => { |
||||
|
describe('#setCrossOrigin', () => { |
||||
|
it('should set crossorigin attribute', () => { |
||||
|
const strategy = new CrossOriginStrategy('use-credentials'); |
||||
|
const element = document.createElement('link'); |
||||
|
strategy.setCrossOrigin(element); |
||||
|
|
||||
|
expect(element.crossOrigin).toBe('use-credentials'); |
||||
|
}); |
||||
|
|
||||
|
it('should set integrity attribute when given', () => { |
||||
|
const integrity = uuid(); |
||||
|
const strategy = new CrossOriginStrategy('anonymous', integrity); |
||||
|
const element = document.createElement('link'); |
||||
|
strategy.setCrossOrigin(element); |
||||
|
|
||||
|
expect(element.crossOrigin).toBe('anonymous'); |
||||
|
expect(element.getAttribute('integrity')).toBe(integrity); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
describe('CROSS_ORIGIN_STRATEGY', () => { |
||||
|
test.each` |
||||
|
name | integrity | crossOrigin |
||||
|
${'Anonymous'} | ${undefined} | ${'anonymous'} |
||||
|
${'Anonymous'} | ${uuid()} | ${'anonymous'} |
||||
|
${'UseCredentials'} | ${undefined} | ${'use-credentials'} |
||||
|
${'UseCredentials'} | ${uuid()} | ${'use-credentials'} |
||||
|
`('should successfully map $name to CrossOriginStrategy', ({ name, integrity, crossOrigin }) => {
|
||||
|
expect(CROSS_ORIGIN_STRATEGY[name](integrity)).toEqual( |
||||
|
new CrossOriginStrategy(crossOrigin, integrity), |
||||
|
); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue