如何解决资源上下文 (iframe) 中使用的不安全值
我正在尝试测试我的组件,在该组件中我使用预先签名的 URL 来呈现来自 S3 的 PDF 文件,然后我使用了 bypassSecurityTrustResourceUrl,这运行良好,但就在我尝试测试此组件时,我得到以下错误。
这是我的 .spec
@Injectable()
class MockService extends RestService{
getPDFUrl(){
return of({
"timestamp": "2021-03-10T02:17:28.699Z","statusCode": 200,"url": "someURL"
})
}
getDocument() {
{someinfo:"infoo"}
}
}
describe('DashboardDetailsComponent',() => {
let component: DashboardDetailsComponent;
let fixture: ComponentFixture<DashboardDetailsComponent>;
beforeEach(async () => {
await Testbed.configureTestingModule({
imports: [HomeModule,HomeRoutingModule,HttpClientModule,RouterTestingModule,NoopAnimationsModule,MatDialogModule ],declarations: [ DashboardDetailsComponent,],providers: [
{
provide: DomSanitizer,useValue: {
sanitize: (ctx: any,val: string) => val,bypassSecurityTrustResourceUrl: (val: string) => val,},// more providers
],})
.compileComponents();
});
beforeEach(() => {
fixture = Testbed.createComponent(DashboardDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create',() => {
expect(component).toBeTruthy();
});
});
这是我使用 url 的 html 代码
<iframe [src]='url || ""' width="100%" height="350"></iframe>
这是我设置 url 的 .ts 代码
this._restService.getPDFUrl(this.document[0].templateURL).subscribe(data => {
this.url = this._domSanitizer.bypassSecurityTrustResourceUrl(data.url);
},err => {
this.failLoadDocument();
});
如果有人可以帮助我,我从昨天开始就一直被这个错误所困扰。
解决方法
尝试更改模拟以返回空字符串或未定义。
既然你在嘲笑它,我认为它可能会引起问题。
也许可以尝试提供真正的 DomSanitizer 或像下面显示的那样模拟它。
{
provide: DomSanitizer,useValue: {
sanitize: (ctx: any,val: string) => undefined,bypassSecurityTrustResourceUrl: (val: string) => undefined,},
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。