Skip to content

Commit

Permalink
fix: resume upload from a stored url (#328)
Browse files Browse the repository at this point in the history
* fix: resume upload from a stored url

closes #327

* chore: update dev packages
  • Loading branch information
kukhariev authored Oct 23, 2021
1 parent 6b18d36 commit ad8d6df
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@types/cpx": "^1.5.2",
"@types/jasmine": "^3.9.1",
"@types/jasmine": "~3.10.1",
"@types/jasminewd2": "~2.0.10",
"@types/node": "12.12.41",
"@uploadx/core": "^4.4.2",
Expand All @@ -65,9 +65,9 @@
"core-js": "2.6.11",
"cpx": "^1.5.0",
"husky": "^7.0.2",
"jasmine-core": "^3.9.0",
"jasmine-core": "~3.10.1",
"jasmine-spec-reporter": "^7.0.0",
"karma": "^5.2.3",
"karma": "~6.3.5",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.3",
"karma-jasmine": "~4.0.1",
Expand Down
27 changes: 15 additions & 12 deletions src/uploadx/lib/tus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,38 @@ import { Tus } from './tus';
// tslint:disable: no-any
const fileWithType = new File(['123456'], 'filename.txt', { type: 'text/plain' });
describe('getFileUrl', () => {
let upx: Tus;
let uploader: Tus;
let req: jasmine.Spy;
let getValueFromResponse: jasmine.Spy;
it('should set headers', async () => {
upx = new Tus(fileWithType, {}, () => {}, {} as Ajax);
req = spyOn<any>(upx, 'request').and.callFake(({ headers }: any) => {
uploader = new Tus(fileWithType, {}, () => {}, {} as Ajax);
req = spyOn<any>(uploader, 'request').and.callFake(({ headers }: any) => {
expect(headers['Upload-Metadata']).toContain('name');
expect(headers['Upload-Length']).toEqual('6');
});
getValueFromResponse = spyOn<any>(upx, 'getValueFromResponse').and.returnValue('/12345678');
expect(upx.name).toEqual('filename.txt');
expect(upx.size).toEqual(6);
expect(await upx.getFileUrl()).toEqual('/12345678');
getValueFromResponse = spyOn<any>(uploader, 'getValueFromResponse').and.returnValue(
'/12345678'
);
expect(uploader.name).toEqual('filename.txt');
expect(uploader.size).toEqual(6);
expect(await uploader.getFileUrl()).toEqual('/12345678');
expect(req).toHaveBeenCalled();
expect(getValueFromResponse).toHaveBeenCalled();
});
});
describe('sendFileContent', () => {
let upx: Tus;
let uploader: Tus;
let req: jasmine.Spy;
let getOffsetFromResponse: jasmine.Spy;
it('should set Upload-Offset header', async () => {
upx = new Tus(fileWithType, {}, () => {}, {} as Ajax);
req = spyOn<any>(upx, 'request').and.callFake(({ headers }: any) => {
uploader = new Tus(fileWithType, {}, () => {}, {} as Ajax);
uploader.offset = 0;
req = spyOn<any>(uploader, 'request').and.callFake(({ headers }: any) => {
expect(headers['Content-Type']).toEqual('application/offset+octet-stream');
expect(headers['Upload-Offset']).toEqual('0');
});
getOffsetFromResponse = spyOn<any>(upx, 'getOffsetFromResponse').and.returnValue(6);
expect(await upx.sendFileContent()).toEqual(6);
getOffsetFromResponse = spyOn<any>(uploader, 'getOffsetFromResponse').and.returnValue(6);
expect(await uploader.sendFileContent()).toEqual(6);
expect(req).toHaveBeenCalled();
expect(getOffsetFromResponse).toHaveBeenCalled();
});
Expand Down
4 changes: 3 additions & 1 deletion src/uploadx/lib/uploader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ describe('Uploader', () => {
const updateToken = spyOn<any>(uploader, 'updateToken').and.callThrough();
const getFileUrl = spyOn(uploader, 'getFileUrl').and.callThrough();
const getOffset = spyOn(uploader, 'getOffset').and.callThrough();
const sendFileContent = spyOn(uploader, 'sendFileContent').and.callThrough();
const cleanup = spyOn<any>(uploader, 'cleanup').and.callThrough();
await uploader.upload();
expect(updateToken).toHaveBeenCalledTimes(1);
expect(getFileUrl).toHaveBeenCalledTimes(1);
expect(getOffset).toHaveBeenCalledTimes(0);
expect(getOffset).toHaveBeenCalledTimes(1);
expect(sendFileContent).toHaveBeenCalledTimes(1);
expect(cleanup).toHaveBeenCalled();
expect(uploader.status).toEqual('complete');
});
Expand Down
2 changes: 1 addition & 1 deletion src/uploadx/lib/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export abstract class Uploader implements UploadState {
/** Auth token/tokenGetter */
token: UploadxControlEvent['token'];
/** Byte offset within the whole file */
offset? = 0;
offset?: number;
/** Retries handler */
retry: RetryHandler;
canceler = new Canceler();
Expand Down
43 changes: 24 additions & 19 deletions src/uploadx/lib/uploaderx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,57 @@ const fileWithType = new File(['123456'], 'filename.txt', { type: 'text/plain' }
const fileWithoutType = new File([''], 'filename');

describe('getFileUrl', () => {
let upx: UploaderX;
let uploader: UploaderX;
let req: jasmine.Spy;
let getValueFromResponse: jasmine.Spy;
it('should set headers', async () => {
upx = new UploaderX(fileWithType, {}, () => {}, {} as Ajax);
req = spyOn<any>(upx, 'request').and.callFake(({ headers }: any) => {
uploader = new UploaderX(fileWithType, {}, () => {}, {} as Ajax);
req = spyOn<any>(uploader, 'request').and.callFake(({ headers }: any) => {
expect(headers['X-Upload-Content-Type']).toEqual('text/plain');
expect(headers['X-Upload-Content-Length']).toEqual('6');
});
getValueFromResponse = spyOn<any>(upx, 'getValueFromResponse').and.returnValue('/12345678');
expect(upx.name).toEqual('filename.txt');
expect(upx.size).toEqual(6);
expect(await upx.getFileUrl()).toEqual('/12345678');
getValueFromResponse = spyOn<any>(uploader, 'getValueFromResponse').and.returnValue(
'/12345678'
);
expect(uploader.name).toEqual('filename.txt');
expect(uploader.size).toEqual(6);
expect(await uploader.getFileUrl()).toEqual('/12345678');
expect(req).toHaveBeenCalled();
expect(getValueFromResponse).toHaveBeenCalled();
});
it('should set default type header', async () => {
upx = new UploaderX(fileWithoutType, {}, () => {}, {} as Ajax);
req = spyOn<any>(upx, 'request').and.callFake(({ headers }: any) => {
uploader = new UploaderX(fileWithoutType, {}, () => {}, {} as Ajax);
req = spyOn<any>(uploader, 'request').and.callFake(({ headers }: any) => {
expect(headers['X-Upload-Content-Type']).toEqual('application/octet-stream');
expect(headers['X-Upload-Content-Length']).toEqual('0');
});
getValueFromResponse = spyOn<any>(upx, 'getValueFromResponse').and.returnValue('/12345678');
expect(upx.name).toEqual('filename');
expect(upx.size).toEqual(0);
await upx.getFileUrl();
getValueFromResponse = spyOn<any>(uploader, 'getValueFromResponse').and.returnValue(
'/12345678'
);
expect(uploader.name).toEqual('filename');
expect(uploader.size).toEqual(0);
await uploader.getFileUrl();
expect(req).toHaveBeenCalled();
expect(getValueFromResponse).toHaveBeenCalled();
});
});

describe('sendFileContent', () => {
let upx: UploaderX;
let uploaderX: UploaderX;
let req: jasmine.Spy;
let getValueFromResponse: jasmine.Spy;
it('should set Content-Range header', async () => {
upx = new UploaderX(fileWithType, {}, () => {}, {} as Ajax);
req = spyOn<any>(upx, 'request').and.callFake(({ headers }: any) => {
uploaderX = new UploaderX(fileWithType, {}, () => {}, {} as Ajax);
uploaderX.offset = 0;
req = spyOn<any>(uploaderX, 'request').and.callFake(({ headers }: any) => {
expect(headers['Content-Type']).toEqual('application/octet-stream');
expect(headers['Content-Range']).toEqual('bytes 0-5/6');
});
getValueFromResponse = spyOn<any>(upx, 'getValueFromResponse').and.returnValue(
getValueFromResponse = spyOn<any>(uploaderX, 'getValueFromResponse').and.returnValue(
'Range: bytes=0-5'
);
upx.responseStatus = 308;
expect(await upx.sendFileContent()).toEqual(6);
uploaderX.responseStatus = 308;
expect(await uploaderX.sendFileContent()).toEqual(6);
expect(req).toHaveBeenCalled();
expect(getValueFromResponse).toHaveBeenCalled();
});
Expand Down

0 comments on commit ad8d6df

Please sign in to comment.