|
1 | 1 |
|
2 | 2 | // Define globals for eslint.
|
3 |
| -/* global describe it require */ |
| 3 | +/* global describe it require jest */ |
4 | 4 |
|
5 | 5 | // Load dependencies
|
6 | 6 | import should from 'should'; // eslint-disable-line no-unused-vars
|
@@ -84,42 +84,111 @@ describe('lib/FileSystem', function() {
|
84 | 84 |
|
85 | 85 | });
|
86 | 86 |
|
87 |
| - it('#getFileNameFromUrl should create a sha1 filename from a PNG/JPG/GIF/BMP url.', () => { |
| 87 | + it('#getFileNameFromUrl should create a sha1 filename from a PNG/JPG/GIF/BMP url.', async () => { |
88 | 88 |
|
89 | 89 | const fileSystem = FileSystemFactory();
|
90 | 90 |
|
91 |
| - let pngFilename = fileSystem.getFileNameFromUrl('https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png'); |
| 91 | + let pngFilename = await fileSystem.getFileNameFromUrl('https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png'); |
92 | 92 |
|
93 | 93 | pngFilename.should.equal('cd7d2199cd8e088cdfd9c99fc6359666adc36289.png');
|
94 | 94 |
|
95 |
| - let gifFilename = fileSystem.getFileNameFromUrl('https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif'); |
| 95 | + let gifFilename = await fileSystem.getFileNameFromUrl('https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif'); |
96 | 96 |
|
97 | 97 | gifFilename.should.equal('c048132247cd28c7879ab36d78a8f45194640006.gif');
|
98 | 98 |
|
99 |
| - let jpgFilename = fileSystem.getFileNameFromUrl('https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red.jpg'); |
| 99 | + let jpgFilename = await fileSystem.getFileNameFromUrl('https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red.jpg'); |
100 | 100 |
|
101 | 101 | jpgFilename.should.equal('6adf4569ecc3bf8c378bb4d47b1995cd85c5a13c.jpg');
|
102 | 102 |
|
103 |
| - let bmpFilename = fileSystem.getFileNameFromUrl('https://cdn-learn.adafruit.com/assets/assets/000/010/147/original/tiger.bmp'); |
| 103 | + let bmpFilename = await fileSystem.getFileNameFromUrl('https://cdn-learn.adafruit.com/assets/assets/000/010/147/original/tiger.bmp'); |
104 | 104 |
|
105 | 105 | bmpFilename.should.equal('282fb62d2caff367aff828ce21e79575733605c8.bmp');
|
106 | 106 |
|
107 | 107 | });
|
108 | 108 |
|
109 |
| - it('#getFileNameFromUrl should handle urls with same pathname but different query strings or fragments as individual files.', () => { |
| 109 | + it('#getFileNameFromUrl should handle urls with same pathname but different query strings or fragments as individual files.', async () => { |
110 | 110 |
|
111 | 111 | const fileSystem = FileSystemFactory();
|
112 | 112 |
|
113 |
| - const pngFilename = fileSystem.getFileNameFromUrl('https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png?exampleparam=one&anotherparam=2#this-is-a-fragment'); |
| 113 | + const pngFilename = await fileSystem.getFileNameFromUrl('https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png?exampleparam=one&anotherparam=2#this-is-a-fragment'); |
114 | 114 |
|
115 | 115 | pngFilename.should.equal('9eea25bf871c2333648080180f6b616a91ce1b09.png');
|
116 | 116 |
|
117 |
| - const pngFilenameTwo = fileSystem.getFileNameFromUrl('https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png?exampleparam=DIFFERENT&anotherparam=2#this-is-a-fragment-two'); |
| 117 | + const pngFilenameTwo = await fileSystem.getFileNameFromUrl('https://img.wennermedia.com/5333a62d-07db-432a-92e2-198cafa38a14-326adb1a-d8ed-4a5d-b37e-5c88883e1989.png?exampleparam=DIFFERENT&anotherparam=2#this-is-a-fragment-two'); |
118 | 118 |
|
119 | 119 | pngFilenameTwo.should.equal('09091b8880ddb982968a0fe28abed5034f9a43b8.png');
|
120 | 120 |
|
121 | 121 | });
|
122 | 122 |
|
| 123 | + it('#getFileNameFromUrl should handle PNG/JPG/GIF/BMP urls without file extensions by using content-type header.', async () => { |
| 124 | + |
| 125 | + const fileSystem = FileSystemFactory(); |
| 126 | + |
| 127 | + // Mock fetch |
| 128 | + fetch = jest.fn(); // eslint-disable-line no-global-assign |
| 129 | + |
| 130 | + // Test PNG |
| 131 | + fetch.mockReturnValueOnce(Promise.resolve({ |
| 132 | + headers: { |
| 133 | + get: (headerName) => { |
| 134 | + |
| 135 | + headerName.should.equals('content-type'); |
| 136 | + |
| 137 | + return 'image/png'; |
| 138 | + } |
| 139 | + } |
| 140 | + })); |
| 141 | + |
| 142 | + const pngFilename = await fileSystem.getFileNameFromUrl('https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'); |
| 143 | + pngFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.png'); |
| 144 | + |
| 145 | + // Test JPG |
| 146 | + fetch.mockReturnValueOnce(Promise.resolve({ |
| 147 | + headers: { |
| 148 | + get: (headerName) => { |
| 149 | + |
| 150 | + headerName.should.equals('content-type'); |
| 151 | + |
| 152 | + return 'image/jpeg'; |
| 153 | + } |
| 154 | + } |
| 155 | + })); |
| 156 | + |
| 157 | + const jpgFilename = await fileSystem.getFileNameFromUrl('https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'); |
| 158 | + jpgFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.jpg'); |
| 159 | + |
| 160 | + // Test GIF |
| 161 | + fetch.mockReturnValueOnce(Promise.resolve({ |
| 162 | + headers: { |
| 163 | + get: (headerName) => { |
| 164 | + |
| 165 | + headerName.should.equals('content-type'); |
| 166 | + |
| 167 | + return 'image/gif'; |
| 168 | + } |
| 169 | + } |
| 170 | + })); |
| 171 | + |
| 172 | + const gifFilename = await fileSystem.getFileNameFromUrl('https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'); |
| 173 | + gifFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.gif'); |
| 174 | + |
| 175 | + // Test BMP |
| 176 | + fetch.mockReturnValueOnce(Promise.resolve({ |
| 177 | + headers: { |
| 178 | + get: (headerName) => { |
| 179 | + |
| 180 | + headerName.should.equals('content-type'); |
| 181 | + |
| 182 | + return 'image/bmp'; |
| 183 | + } |
| 184 | + } |
| 185 | + })); |
| 186 | + |
| 187 | + const bmpFilename = await fileSystem.getFileNameFromUrl('https://cdn2.hubspot.net/hub/42284/file-14233687-jpg/images/test_in_red'); |
| 188 | + bmpFilename.should.equal('831eb245a3d9032cdce450f8760d2b8ddb442a3d.bmp'); |
| 189 | + |
| 190 | + }); |
| 191 | + |
123 | 192 | it('#getLocalFilePathFromUrl should return local filepath if it exists on local fs in permanent dir.', () => {
|
124 | 193 |
|
125 | 194 | const RNFetchBlob = require('react-native-fetch-blob');
|
|
0 commit comments