-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(utils): getMIMETypeFromResponse 테스트 코드 추가 (#131)
- Loading branch information
Showing
4 changed files
with
45 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export const getMIMETypeFromFile = (file: File) => { | ||
return file?.type ?? ''; | ||
return file.type ?? ''; | ||
}; |
32 changes: 32 additions & 0 deletions
32
packages/utils/src/file/getMIMETypeFromResponse/getMIMETypeFromResponse.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getMIMETypeFromResponse } from '.'; | ||
|
||
describe('getMIMETypeFromResponse', () => { | ||
it('should return the correct MIME type from the response headers', () => { | ||
const headers = new Headers(); | ||
headers.set('Content-Type', 'image/png'); | ||
|
||
const mockResponse = new Response(null, { headers }); | ||
|
||
const mimeType = getMIMETypeFromResponse(mockResponse); | ||
expect(mimeType).toBe('image/png'); | ||
}); | ||
|
||
it('should return an empty string if the Content-Type header is not present', () => { | ||
const headers = new Headers(); | ||
const mockResponse = new Response(null, { headers }); | ||
|
||
const mimeType = getMIMETypeFromResponse(mockResponse); | ||
expect(mimeType).toBe(''); | ||
}); | ||
|
||
it('should return an empty string when given an invalid argument', () => { | ||
const mockResponse1 = {} as unknown as Response; | ||
const mockResponse2 = 123 as unknown as Response; | ||
|
||
const mimeType1 = getMIMETypeFromResponse(mockResponse1); | ||
expect(mimeType1).toBe(''); | ||
|
||
const mimeType2 = getMIMETypeFromResponse(mockResponse2); | ||
expect(mimeType2).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
export const getMIMETypeFromResponse = (response: Response) => { | ||
try { | ||
return response.headers.get('Content-Type') ?? ''; | ||
} catch (err: any) { | ||
console.error( | ||
`Failed to get the MIME type from Response. message: ${err.message}` | ||
); | ||
return ''; | ||
} | ||
return response.headers?.get('Content-Type') ?? ''; | ||
}; |