angular - 资源上下文中使用的不安全值 (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,
},
},
推荐阅读
- matplotlib - 如何摆脱 Seaborn mainplot 下的情节?
- docker - 带有 AspNet Core Web API 的容器在 Docker 上运行,但不在 AKS Kubernetes 上运行
- c++ - glib 中 G_OPTION_ARG_FILENAME 和 G_OPTION_ARG_STRING 的区别
- python - 我如何通过给出 y 来访问数组 x?
- c# - 如何使用安全声明代表 AAD 用户调用 Web API?
- delphi - TNetHTTPClient 和 Delphi 10.3 的连接超时
- python - 如何在 AWS Glue 中运行查询
- serverless-framework - 没有在带有无服务器和 AWS 的 NestJS 上注入依赖项
- python - 为什么导入不包含模块中的所有内容?
- c# - 有没有办法以任何不涉及鼠标的方式与世界空间中的 GUI 按钮进行交互?最好是光线投射