|
| 1 | +import { expect, use } from 'chai'; |
| 2 | +import { spy } from 'sinon'; |
| 3 | +import sinonChai from 'sinon-chai'; |
| 4 | +import chaiStream from 'chai-stream'; |
| 5 | + |
| 6 | +import Uploader from '../src/lib/Uploader'; |
| 7 | +use(sinonChai); |
| 8 | +use(chaiStream); |
| 9 | + |
| 10 | +let uploader:Uploader; |
| 11 | + |
| 12 | +describe('Uploader', () => { |
| 13 | + describe('uploadFile', () => { |
| 14 | + it('should upload', async function() { |
| 15 | + this.timeout(5000); |
| 16 | + |
| 17 | + const s3 = { |
| 18 | + upload(_, cb) { |
| 19 | + cb(null); |
| 20 | + } |
| 21 | + }; |
| 22 | + spy(s3, "upload"); |
| 23 | + |
| 24 | + uploader = new Uploader({ |
| 25 | + localPath: 'test/files', |
| 26 | + remotePath: 'fake', |
| 27 | + bucket: 'fake', |
| 28 | + glob: '**/pano_001_1k_0_0_0.png', |
| 29 | + s3Client: <any>s3, |
| 30 | + }); |
| 31 | + |
| 32 | + await uploader.upload(); |
| 33 | + |
| 34 | + const { Body, ...args} = (<any>s3.upload).lastCall.args[0]; |
| 35 | + |
| 36 | + |
| 37 | + expect(args).to.deep.equal({ |
| 38 | + Bucket: 'fake', |
| 39 | + Key: 'fake/panoramas/family-guy/pano_001_1k_0_0_0.png', |
| 40 | + ContentType: 'image/png', |
| 41 | + CacheControl: '', |
| 42 | + }); |
| 43 | + |
| 44 | + (<any>expect(Body).to.be.a).ReadableStream; |
| 45 | + |
| 46 | + (<any>s3.upload).restore(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should fix windows paths', async function() { |
| 50 | + this.timeout(5000); |
| 51 | + |
| 52 | + const s3 = { |
| 53 | + upload(_, cb) { |
| 54 | + cb(null); |
| 55 | + } |
| 56 | + }; |
| 57 | + spy(s3, "upload"); |
| 58 | + |
| 59 | + uploader = new Uploader({ |
| 60 | + localPath: 'test/files', |
| 61 | + remotePath: 'fake', |
| 62 | + bucket: 'fake', |
| 63 | + glob: '**/pano_001_1k_0_0_0.png', |
| 64 | + s3Client: <any>s3, |
| 65 | + }); |
| 66 | + |
| 67 | + await uploader.uploadFile('files/panoramas/family-guy/pano_001_1k_0_0_0.png', 'foo\\bar.png'); |
| 68 | + |
| 69 | + const { Body, ...args} = (<any>s3.upload).lastCall.args[0]; |
| 70 | + |
| 71 | + |
| 72 | + expect(args).to.deep.equal({ |
| 73 | + Bucket: 'fake', |
| 74 | + Key: 'foo/bar.png', |
| 75 | + ContentType: 'image/png', |
| 76 | + CacheControl: '', |
| 77 | + }); |
| 78 | + |
| 79 | + (<any>expect(Body).to.be.a).ReadableStream; |
| 80 | + |
| 81 | + (<any>s3.upload).restore(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('getCacheControlValue', () => { |
| 86 | + describe('with no config value', () => { |
| 87 | + it('should return default value', () => { |
| 88 | + uploader = new Uploader({ |
| 89 | + localPath: '', |
| 90 | + remotePath: '', |
| 91 | + bucket: 'a', |
| 92 | + }); |
| 93 | + |
| 94 | + expect(uploader.getCacheControlValue('foo.bar')).to.equal(''); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('with static config value', () => { |
| 99 | + it('should return config value', () => { |
| 100 | + uploader = new Uploader({ |
| 101 | + localPath: '', |
| 102 | + remotePath: '', |
| 103 | + bucket: 'a', |
| 104 | + cacheControl: '1' |
| 105 | + }); |
| 106 | + |
| 107 | + expect(uploader.getCacheControlValue('foo.bar')).to.equal('1'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('with glob config value', () => { |
| 112 | + it('should return config value', () => { |
| 113 | + uploader = new Uploader({ |
| 114 | + localPath: '', |
| 115 | + remotePath: '', |
| 116 | + bucket: 'a', |
| 117 | + cacheControl: { |
| 118 | + '**/*.json': '10', |
| 119 | + '**/*.*': '100', |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + expect(uploader.getCacheControlValue('files/foo.json')).to.equal('10'); |
| 124 | + expect(uploader.getCacheControlValue('files/foo.jpg')).to.equal('100'); |
| 125 | + expect(uploader.getCacheControlValue('foo.jpg')).to.equal('100'); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + }); |
| 130 | +}); |
0 commit comments