优化
This commit is contained in:
@@ -162,6 +162,106 @@ test('photo upload refreshes and replays once while retaining the file payload',
|
||||
])
|
||||
})
|
||||
|
||||
test('H5 blob photo uses authenticated multipart fetch instead of uni.uploadFile', async () => {
|
||||
const value = prepareSession()
|
||||
const originalWindow = globalThis.window
|
||||
const originalLocation = globalThis.location
|
||||
const originalFetch = globalThis.fetch
|
||||
let uploadFileCalls = 0
|
||||
let uploaded
|
||||
globalThis.window = {}
|
||||
globalThis.location = { protocol: 'http:' }
|
||||
globalThis.uni.uploadFile = () => { uploadFileCalls += 1 }
|
||||
globalThis.fetch = async (url, options) => {
|
||||
if (String(url).startsWith('blob:')) return new Response(new Blob(['photo'], { type: 'image/png' }))
|
||||
uploaded = { url, options }
|
||||
return new Response(JSON.stringify({ code: '200', data: { fileId: 'h5-1' } }), { status: 200 })
|
||||
}
|
||||
try {
|
||||
session.apiBase = '/overseas-api'
|
||||
assert.deepEqual(await overseasUpload('/api/pda/v1/last-leg/photos/files', 'blob:http://localhost/photo'), { fileId: 'h5-1' })
|
||||
assert.equal(uploadFileCalls, 0)
|
||||
assert.equal(uploaded.url, '/overseas-api/pda/v1/last-leg/photos/files')
|
||||
assert.equal(uploaded.options.headers.Authorization, `Bearer ${value.overseasAccessToken}`)
|
||||
assert.ok(uploaded.options.body instanceof FormData)
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch
|
||||
if (originalWindow === undefined) delete globalThis.window
|
||||
else globalThis.window = originalWindow
|
||||
if (originalLocation === undefined) delete globalThis.location
|
||||
else globalThis.location = originalLocation
|
||||
}
|
||||
})
|
||||
|
||||
test('H5 File photo is appended directly without rereading a temporary blob URL', async () => {
|
||||
const value = prepareSession()
|
||||
const originalWindow = globalThis.window
|
||||
const originalLocation = globalThis.location
|
||||
globalThis.window = {}
|
||||
globalThis.location = { protocol: 'http:' }
|
||||
const selected = new Blob(['photo'], { type: 'image/png' })
|
||||
let calls = 0
|
||||
globalThis.uni.uploadFile = (options) => {
|
||||
calls += 1
|
||||
assert.equal(options.url, '/overseas-api/pda/photo')
|
||||
assert.equal(options.header.Authorization, `Bearer ${value.overseasAccessToken}`)
|
||||
assert.equal(options.files[0].file, selected)
|
||||
assert.equal(options.files[0].name, 'file')
|
||||
options.success({ statusCode: 200, data: JSON.stringify({ code: '200', data: { fileId: 'direct-file' } }), header: {} })
|
||||
}
|
||||
try {
|
||||
session.apiBase = '/overseas-api'
|
||||
assert.deepEqual(await overseasUpload('/pda/photo', selected), { fileId: 'direct-file' })
|
||||
assert.equal(calls, 1)
|
||||
} finally {
|
||||
if (originalWindow === undefined) delete globalThis.window
|
||||
else globalThis.window = originalWindow
|
||||
if (originalLocation === undefined) delete globalThis.location
|
||||
else globalThis.location = originalLocation
|
||||
}
|
||||
})
|
||||
|
||||
test('native file runtime rejects an H5-relative overseas root before upload with a clear configuration error', async () => {
|
||||
prepareSession()
|
||||
const originalLocation = globalThis.location
|
||||
globalThis.location = { protocol: 'file:' }
|
||||
session.apiBase = '/overseas-api'
|
||||
let uploadCalls = 0
|
||||
globalThis.uni.uploadFile = () => { uploadCalls += 1 }
|
||||
try {
|
||||
await assert.rejects(overseasUpload('/pda/photo', '_doc/photo.jpg'), (error) => {
|
||||
assert.equal(error.code, 'PDA_OVERSEAS_API_BASE_REQUIRED')
|
||||
assert.match(error.message, /VITE_OVERSEAS_API_BASE_URL/)
|
||||
assert.match(error.message, /HTTP\(S\)/)
|
||||
return true
|
||||
})
|
||||
assert.equal(uploadCalls, 0)
|
||||
} finally {
|
||||
if (originalLocation === undefined) delete globalThis.location
|
||||
else globalThis.location = originalLocation
|
||||
}
|
||||
})
|
||||
|
||||
test('native file runtime uploads to an absolute overseas root', async () => {
|
||||
prepareSession()
|
||||
const originalLocation = globalThis.location
|
||||
globalThis.location = { protocol: 'file:' }
|
||||
session.apiBase = 'https://overseas.example.com/api/'
|
||||
let call
|
||||
globalThis.uni.uploadFile = (options) => {
|
||||
call = options
|
||||
options.success({ statusCode: 200, data: JSON.stringify({ code: '200', data: { fileId: 'native-1' } }), header: {} })
|
||||
}
|
||||
try {
|
||||
assert.deepEqual(await overseasUpload('/api/pda/v1/last-leg/photos/files', '_doc/photo.jpg'), { fileId: 'native-1' })
|
||||
assert.equal(call.url, 'https://overseas.example.com/api/pda/v1/last-leg/photos/files')
|
||||
assert.equal(call.filePath, '_doc/photo.jpg')
|
||||
} finally {
|
||||
if (originalLocation === undefined) delete globalThis.location
|
||||
else globalThis.location = originalLocation
|
||||
}
|
||||
})
|
||||
|
||||
test('authenticated photo download maps the public api path through the H5 overseas proxy', async () => {
|
||||
const value = prepareSession(); const calls = []
|
||||
globalThis.uni.downloadFile = (options) => {
|
||||
|
||||
Reference in New Issue
Block a user