1
0
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:
Mihajlo Medjedovic
2020-10-30 16:11:50 +01:00
parent 7279c23fe2
commit 3f6e89d716
4 changed files with 122 additions and 28 deletions

View File

@@ -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,8 +19,9 @@ 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.')
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 = ''
@@ -40,7 +42,6 @@ export class FileUploader {
'cache-control': 'no-cache'
}
return new Promise((resolve, reject) => {
const formData = new FormData()
for (let file of files) {
@@ -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))
})
})
}

View 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" : ""
}`

View File

@@ -1,4 +1,4 @@
import { parseGeneratedCode } from './index'
import { parseGeneratedCode } from '../../utils/index'
it('should parse generated code', async (done) => {
expect(sampleResponse).toBeTruthy()

View File

@@ -1,4 +1,4 @@
import { parseSourceCode } from './index'
import { parseSourceCode } from '../../utils/index'
it('should parse SAS9 source code', async (done) => {
expect(sampleResponse).toBeTruthy()