request.test.mjs 799 B

12345678910111213141516171819202122
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { unwrapResponse } from './request.js'
  4. test('unwrapResponse returns data when code is 200', () => {
  5. const data = unwrapResponse({ code: 200, message: 'success', data: { token: 't' } })
  6. assert.deepEqual(data, { token: 't' })
  7. })
  8. test('unwrapResponse throws with backend message when code is not 200', () => {
  9. assert.throws(
  10. () => unwrapResponse({ code: 401, message: '用户名或密码错误', data: null }),
  11. (e) => e instanceof Error && e.message === '用户名或密码错误' && e.code === 401
  12. )
  13. })
  14. test('unwrapResponse throws network message on null body', () => {
  15. assert.throws(
  16. () => unwrapResponse(null),
  17. (e) => e instanceof Error && e.message === '网络异常,请稍后重试'
  18. )
  19. })