@@ -3,6 +3,7 @@ import { ResourceService } from "./resource";
33import { vi , describe , it , expect , beforeEach } from "vitest" ;
44import type { Group } from "@Packages/message/server" ;
55import type { IMessageQueue } from "@Packages/message/message_queue" ;
6+ import { parseUrlSRI } from "./utils" ;
67
78initTestEnv ( ) ;
89
@@ -27,7 +28,7 @@ function mockResponse(blob: Blob, status = 200, contentType?: string) {
2728 } as unknown as Response ;
2829}
2930
30- describe ( "ResourceService - loadByUrl " , ( ) => {
31+ describe ( "ResourceService - createResourceByUrlFetch " , ( ) => {
3132 let service : ResourceService ;
3233
3334 beforeEach ( ( ) => {
@@ -49,7 +50,7 @@ describe("ResourceService - loadByUrl", () => {
4950 const jsCode = "console.log('hello');" ;
5051 mockFetch . mockResolvedValue ( mockResponse ( textBlob ( jsCode ) , 200 , "application/javascript; charset=utf-8" ) ) ;
5152
52- const res = await service . loadByUrl ( "https://example.com/lib.js" , "require" ) ;
53+ const res = await service . createResourceByUrlFetch ( parseUrlSRI ( "https://example.com/lib.js" ) , "require" ) ;
5354
5455 expect ( res . url ) . toBe ( "https://example.com/lib.js" ) ;
5556 expect ( res . content ) . toBeTruthy ( ) ;
@@ -62,7 +63,7 @@ describe("ResourceService - loadByUrl", () => {
6263 const text = "plain text content" ;
6364 mockFetch . mockResolvedValue ( mockResponse ( textBlob ( text ) , 200 , "text/plain" ) ) ;
6465
65- const res = await service . loadByUrl ( "https://example.com/data.txt" , "resource" ) ;
66+ const res = await service . createResourceByUrlFetch ( parseUrlSRI ( "https://example.com/data.txt" ) , "resource" ) ;
6667
6768 expect ( res . content ) . toBe ( text ) ;
6869 expect ( res . type ) . toBe ( "resource" ) ;
@@ -73,7 +74,7 @@ describe("ResourceService - loadByUrl", () => {
7374 const bytes = [ 0x89 , 0x50 , 0x4e , 0x47 , 0x00 , 0x00 , 0x00 , 0x00 ] ;
7475 mockFetch . mockResolvedValue ( mockResponse ( binaryBlob ( bytes ) , 200 , "image/png" ) ) ;
7576
76- const res = await service . loadByUrl ( "https://example.com/img.png" , "resource" ) ;
77+ const res = await service . createResourceByUrlFetch ( parseUrlSRI ( "https://example.com/img.png" ) , "resource" ) ;
7778
7879 expect ( res . content ) . toBe ( "" ) ;
7980 expect ( res . base64 ) . toBeTruthy ( ) ;
@@ -83,15 +84,15 @@ describe("ResourceService - loadByUrl", () => {
8384 it ( "响应非200时应抛出异常" , async ( ) => {
8485 mockFetch . mockResolvedValue ( mockResponse ( textBlob ( "" ) , 404 ) ) ;
8586
86- await expect ( service . loadByUrl ( "https://example.com/404" , "require" ) ) . rejects . toThrow (
87+ await expect ( service . createResourceByUrlFetch ( parseUrlSRI ( "https://example.com/404" ) , "require" ) ) . rejects . toThrow (
8788 "resource response status not 200: 404"
8889 ) ;
8990 } ) ;
9091
9192 it ( "没有 content-type 时应默认为 application/octet-stream" , async ( ) => {
9293 mockFetch . mockResolvedValue ( mockResponse ( textBlob ( "data" ) , 200 ) ) ;
9394
94- const res = await service . loadByUrl ( "https://example.com/noct" , "resource" ) ;
95+ const res = await service . createResourceByUrlFetch ( parseUrlSRI ( "https://example.com/noct" ) , "resource" ) ;
9596
9697 expect ( res . contentType ) . toBe ( "application/octet-stream" ) ;
9798 } ) ;
0 commit comments