mirror of
https://github.com/sasjs/adapter.git
synced 2025-12-11 09:24:35 +00:00
fix: file uploader error handling and tests
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { isLogInRequired, needsRetry, isUrl } from './utils'
|
||||
import { CsrfToken } from './types/CsrfToken'
|
||||
import { UploadFile } from './types/UploadFile'
|
||||
import { ErrorResponse } from './types'
|
||||
|
||||
const requestRetryLimit = 5
|
||||
|
||||
@@ -18,29 +19,29 @@ export class FileUploader {
|
||||
private retryCount = 0
|
||||
|
||||
public uploadFile(sasJob: string, files: UploadFile[], params: any) {
|
||||
if (files?.length < 1)
|
||||
throw new Error('At least one file must be provided.')
|
||||
|
||||
let paramsString = ''
|
||||
|
||||
for (let param in params) {
|
||||
if (params.hasOwnProperty(param)) {
|
||||
paramsString += `&${param}=${params[param]}`
|
||||
}
|
||||
}
|
||||
|
||||
const program = this.appLoc
|
||||
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
: sasJob
|
||||
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
||||
'_program=' + program
|
||||
}${paramsString}`
|
||||
|
||||
const headers = {
|
||||
'cache-control': 'no-cache'
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
if (files?.length < 1) reject(new ErrorResponse('At least one file must be provided.'))
|
||||
if (!sasJob || sasJob === '') reject(new ErrorResponse('sasJob must be provided.'))
|
||||
|
||||
let paramsString = ''
|
||||
|
||||
for (let param in params) {
|
||||
if (params.hasOwnProperty(param)) {
|
||||
paramsString += `&${param}=${params[param]}`
|
||||
}
|
||||
}
|
||||
|
||||
const program = this.appLoc
|
||||
? this.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
: sasJob
|
||||
const uploadUrl = `${this.serverUrl}${this.jobsPath}/?${
|
||||
'_program=' + program
|
||||
}${paramsString}`
|
||||
|
||||
const headers = {
|
||||
'cache-control': 'no-cache'
|
||||
}
|
||||
|
||||
const formData = new FormData()
|
||||
|
||||
for (let file of files) {
|
||||
@@ -48,7 +49,7 @@ export class FileUploader {
|
||||
}
|
||||
|
||||
if (this.csrfToken) formData.append('_csrf', this.csrfToken.value)
|
||||
|
||||
|
||||
fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
@@ -76,7 +77,7 @@ export class FileUploader {
|
||||
})
|
||||
.then((responseText) => {
|
||||
if (isLogInRequired(responseText))
|
||||
reject('You must be logged in to upload a file')
|
||||
reject(new ErrorResponse('You must be logged in to upload a file.'))
|
||||
|
||||
if (needsRetry(responseText)) {
|
||||
if (this.retryCount < requestRetryLimit) {
|
||||
@@ -95,12 +96,12 @@ export class FileUploader {
|
||||
try {
|
||||
resolve(JSON.parse(responseText))
|
||||
} catch (e) {
|
||||
reject(e)
|
||||
reject(new ErrorResponse('Error while parsing json from upload response.', e))
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err: any) => {
|
||||
reject(err)
|
||||
reject(new ErrorResponse('Upload request failed.', err))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
93
src/test/FileUploader.spec.ts
Normal file
93
src/test/FileUploader.spec.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { FileUploader } from '../FileUploader'
|
||||
import { UploadFile } from '../types';
|
||||
|
||||
(global as any).fetch = jest.fn().mockImplementation(() =>
|
||||
Promise.resolve({
|
||||
text: () => Promise.resolve(sampleResponse),
|
||||
})
|
||||
);
|
||||
|
||||
it('should upload successfully', async (done) => {
|
||||
const fileUploader =
|
||||
new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
const sasJob = 'test/upload'
|
||||
const files: UploadFile[] = [
|
||||
{
|
||||
file: new File([''], 'testfile'),
|
||||
fileName: 'testfile'
|
||||
}
|
||||
];
|
||||
const params = {table: 'libtable'}
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then(
|
||||
(res: any) => {
|
||||
if (JSON.stringify(res) === JSON.stringify(JSON.parse(sampleResponse))) done()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw no files error', async (done) => {
|
||||
const fileUploader =
|
||||
new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
const sasJob = 'test/upload'
|
||||
const files: UploadFile[] = [];
|
||||
const params = {table: 'libtable'}
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then(
|
||||
(res: any) => {},
|
||||
(err: any) => {
|
||||
if (err.error.message === 'At least one file must be provided.') done()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('should throw no sasJob error', async (done) => {
|
||||
const fileUploader =
|
||||
new FileUploader(
|
||||
'/sample/apploc',
|
||||
'https://sample.server.com',
|
||||
'/jobs/path',
|
||||
null,
|
||||
null
|
||||
)
|
||||
|
||||
const sasJob = ''
|
||||
const files: UploadFile[] = [
|
||||
{
|
||||
file: new File([''], 'testfile'),
|
||||
fileName: 'testfile'
|
||||
}
|
||||
];
|
||||
const params = {table: 'libtable'}
|
||||
|
||||
fileUploader.uploadFile(sasJob, files, params).then(
|
||||
(res: any) => {},
|
||||
(err: any) => {
|
||||
if (err.error.message === 'sasJob must be provided.') done()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
const sampleResponse = `{
|
||||
"SYSUSERID": "cas",
|
||||
"_DEBUG":" ",
|
||||
"SYS_JES_JOB_URI": "/jobExecution/jobs/000-000-000-000",
|
||||
"_PROGRAM" : "/Public/app/editors/loadfile",
|
||||
"SYSCC" : "0",
|
||||
"SYSJOBID" : "117382",
|
||||
"SYSWARNINGTEXT" : ""
|
||||
}`
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseGeneratedCode } from './index'
|
||||
import { parseGeneratedCode } from '../../utils/index'
|
||||
|
||||
it('should parse generated code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
@@ -1,4 +1,4 @@
|
||||
import { parseSourceCode } from './index'
|
||||
import { parseSourceCode } from '../../utils/index'
|
||||
|
||||
it('should parse SAS9 source code', async (done) => {
|
||||
expect(sampleResponse).toBeTruthy()
|
||||
Reference in New Issue
Block a user