mirror of
https://github.com/langgenius/dify.git
synced 2026-05-05 09:58:04 +08:00
chore: update ky to 1.14.3 (#33201)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
52
web/service/fetch.spec.ts
Normal file
52
web/service/fetch.spec.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { base } from './fetch'
|
||||
|
||||
vi.mock('@/app/components/base/toast', () => ({
|
||||
default: {
|
||||
notify: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('base', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('Error responses', () => {
|
||||
it('should keep the response body readable when a 401 response is rejected', async () => {
|
||||
// Arrange
|
||||
const unauthorizedResponse = new Response(
|
||||
JSON.stringify({
|
||||
code: 'unauthorized',
|
||||
message: 'Unauthorized',
|
||||
status: 401,
|
||||
}),
|
||||
{
|
||||
status: 401,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
vi.spyOn(globalThis, 'fetch').mockResolvedValue(unauthorizedResponse)
|
||||
|
||||
// Act
|
||||
let caughtError: unknown
|
||||
try {
|
||||
await base('/login')
|
||||
}
|
||||
catch (error) {
|
||||
caughtError = error
|
||||
}
|
||||
|
||||
// Assert
|
||||
expect(caughtError).toBeInstanceOf(Response)
|
||||
await expect((caughtError as Response).json()).resolves.toEqual({
|
||||
code: 'unauthorized',
|
||||
message: 'Unauthorized',
|
||||
status: 401,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user