downloadFile.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import tcb from '../../src/index'
  2. import assert from 'assert'
  3. import config from '../config.local'
  4. import fs from 'fs'
  5. import { ERROR } from '../../lib/const/code'
  6. import { ICustomErrRes } from '../../types/type'
  7. import path from 'path'
  8. let fileContent = fs.createReadStream(`${__dirname}/cos.jpeg`)
  9. describe('storage.downloadFile: 下载文件', () => {
  10. const app = tcb.init(config)
  11. it('无效连接下载', async () => {
  12. try {
  13. const result = await app.downloadFile({
  14. fileID: 'unexistFileID'
  15. // tempFilePath: '/Users/jimmyzhang/repo/tcb-admin-node/test/storage/my-photo.png'
  16. })
  17. } catch (e) {
  18. assert((<ICustomErrRes>e).code === ERROR.STORAGE_FILE_NONEXIST.code)
  19. }
  20. }, 30000)
  21. it('获取文件链接', async () => {
  22. // 上传文件
  23. const result1 = await app.uploadFile({
  24. // cloudPath: "test-admin.jpeg",
  25. cloudPath: 'a|b测试.jpeg',
  26. fileContent
  27. })
  28. expect(result1.fileID).not.toBeNull()
  29. const fileID = result1.fileID
  30. // 下载文件
  31. const result2 = await app.downloadFile({
  32. fileID,
  33. tempFilePath: path.join(__dirname, 'my-photo.png')
  34. })
  35. assert(result2.message, '文件下载完成')
  36. // 下载文件 不知道tempFilePath
  37. const result3 = await app.downloadFile({
  38. fileID
  39. })
  40. assert.ok(result3.fileContent !== undefined)
  41. }, 30000)
  42. it('mock downloadFile statusCode !== 200', async () => {})
  43. })