| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.net.FileDownloaderTest'); |
| 16 |
goog.setTestOnly('goog.net.FileDownloaderTest'); |
| 17 |
|
| 18 |
goog.require('goog.fs.Error'); |
| 19 |
goog.require('goog.net.ErrorCode'); |
| 20 |
goog.require('goog.net.FileDownloader'); |
| 21 |
goog.require('goog.net.XhrIo'); |
| 22 |
goog.require('goog.testing.PropertyReplacer'); |
| 23 |
goog.require('goog.testing.TestCase'); |
| 24 |
goog.require('goog.testing.fs'); |
| 25 |
goog.require('goog.testing.fs.FileSystem'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.testing.net.XhrIoPool'); |
| 28 |
|
| 29 |
var xhrIoPool, xhr, fs, dir, downloader; |
| 30 |
|
| 31 |
function setUpPage() { |
| 32 |
goog.testing.fs.install(new goog.testing.PropertyReplacer()); |
| 33 |
goog.testing.TestCase.getActiveTestCase().promiseTimeout = 10000; // 10s |
| 34 |
} |
| 35 |
|
| 36 |
function setUp() { |
| 37 |
xhrIoPool = new goog.testing.net.XhrIoPool(); |
| 38 |
xhr = xhrIoPool.getXhr(); |
| 39 |
fs = new goog.testing.fs.FileSystem(); |
| 40 |
dir = fs.getRoot(); |
| 41 |
downloader = new goog.net.FileDownloader(dir, xhrIoPool); |
| 42 |
} |
| 43 |
|
| 44 |
function tearDown() { |
| 45 |
goog.dispose(downloader); |
| 46 |
} |
| 47 |
|
| 48 |
function testDownload() { |
| 49 |
var promise = downloader.download('/foo/bar').then(function(blob) { |
| 50 |
var fileEntry = dir.getFileSync('`3fa/``2Ffoo`2Fbar/`bar'); |
| 51 |
assertEquals('data', blob.toString()); |
| 52 |
assertEquals('data', fileEntry.fileSync().toString()); |
| 53 |
}); |
| 54 |
|
| 55 |
xhr.simulateResponse(200, 'data'); |
| 56 |
assertEquals('/foo/bar', xhr.getLastUri()); |
| 57 |
assertEquals(goog.net.XhrIo.ResponseType.ARRAY_BUFFER, xhr.getResponseType()); |
| 58 |
|
| 59 |
return promise; |
| 60 |
} |
| 61 |
|
| 62 |
function testGetDownloadedBlob() { |
| 63 |
var promise = |
| 64 |
downloader.download('/foo/bar') |
| 65 |
.then(function() { return downloader.getDownloadedBlob('/foo/bar'); }) |
| 66 |
.then(function(blob) { assertEquals('data', blob.toString()); }); |
| 67 |
|
| 68 |
xhr.simulateResponse(200, 'data'); |
| 69 |
return promise; |
| 70 |
} |
| 71 |
|
| 72 |
function testGetLocalUrl() { |
| 73 |
var promise = |
| 74 |
downloader.download('/foo/bar') |
| 75 |
.then(function() { return downloader.getLocalUrl('/foo/bar'); }) |
| 76 |
.then(function(url) { assertMatches(/\/`bar$/, url); }); |
| 77 |
|
| 78 |
xhr.simulateResponse(200, 'data'); |
| 79 |
return promise; |
| 80 |
} |
| 81 |
|
| 82 |
function testLocalUrlWithContentDisposition() { |
| 83 |
var promise = |
| 84 |
downloader.download('/foo/bar') |
| 85 |
.then(function() { return downloader.getLocalUrl('/foo/bar'); }) |
| 86 |
.then(function(url) { assertMatches(/\/`qux`22bap$/, url); }); |
| 87 |
|
| 88 |
xhr.simulateResponse( |
| 89 |
200, 'data', {'Content-Disposition': 'attachment; filename="qux\\"bap"'}); |
| 90 |
return promise; |
| 91 |
} |
| 92 |
|
| 93 |
function testIsDownloaded() { |
| 94 |
var promise = |
| 95 |
downloader.download('/foo/bar') |
| 96 |
.then(function() { return downloader.isDownloaded('/foo/bar'); }) |
| 97 |
.then(assertTrue) |
| 98 |
.then(function(isDownloaded) { |
| 99 |
return downloader.isDownloaded('/foo/baz'); |
| 100 |
}) |
| 101 |
.then(assertFalse); |
| 102 |
|
| 103 |
xhr.simulateResponse(200, 'data'); |
| 104 |
return promise; |
| 105 |
} |
| 106 |
|
| 107 |
function testRemove() { |
| 108 |
var promise = |
| 109 |
downloader.download('/foo/bar') |
| 110 |
.then(function() { return downloader.remove('/foo/bar'); }) |
| 111 |
.then(function() { return downloader.isDownloaded('/foo/bar'); }) |
| 112 |
.then(assertFalse) |
| 113 |
.then(function() { return downloader.getDownloadedBlob('/foo/bar'); }) |
| 114 |
.then( |
| 115 |
function() { |
| 116 |
fail('Should not be able to download a missing blob.'); |
| 117 |
}, |
| 118 |
function(err) { |
| 119 |
assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, err.code); |
| 120 |
var download = downloader.download('/foo/bar'); |
| 121 |
xhr.simulateResponse(200, 'more data'); |
| 122 |
return download; |
| 123 |
}) |
| 124 |
.then(function() { return downloader.isDownloaded('/foo/bar'); }) |
| 125 |
.then(assertTrue) |
| 126 |
.then(function() { return downloader.getDownloadedBlob('/foo/bar'); }) |
| 127 |
.then(function(blob) { assertEquals('more data', blob.toString()); }); |
| 128 |
|
| 129 |
xhr.simulateResponse(200, 'data'); |
| 130 |
return promise; |
| 131 |
} |
| 132 |
|
| 133 |
function testSetBlob() { |
| 134 |
return downloader.setBlob('/foo/bar', goog.testing.fs.getBlob('data')) |
| 135 |
.then(function() { return downloader.isDownloaded('/foo/bar'); }) |
| 136 |
.then(assertTrue) |
| 137 |
.then(function() { return downloader.getDownloadedBlob('/foo/bar'); }) |
| 138 |
.then(function(blob) { assertEquals('data', blob.toString()); }); |
| 139 |
} |
| 140 |
|
| 141 |
function testSetBlobWithName() { |
| 142 |
return downloader.setBlob('/foo/bar', goog.testing.fs.getBlob('data'), 'qux') |
| 143 |
.then(function() { return downloader.getLocalUrl('/foo/bar'); }) |
| 144 |
.then(function(url) { assertMatches(/\/`qux$/, url); }); |
| 145 |
} |
| 146 |
function testDownloadDuringDownload() { |
| 147 |
var download1 = downloader.download('/foo/bar'); |
| 148 |
var download2 = downloader.download('/foo/bar'); |
| 149 |
|
| 150 |
var promise = |
| 151 |
download1.then(function() { return download2; }) |
| 152 |
.then(function() { return downloader.getDownloadedBlob('/foo/bar'); }) |
| 153 |
.then(function(blob) { assertEquals('data', blob.toString()); }); |
| 154 |
|
| 155 |
// There should only need to be one response for both downloads, since the |
| 156 |
// second should return the same deferred as the first. |
| 157 |
xhr.simulateResponse(200, 'data'); |
| 158 |
return promise; |
| 159 |
} |
| 160 |
|
| 161 |
function testGetDownloadedBlobDuringDownload() { |
| 162 |
var hasDownloaded = false; |
| 163 |
downloader.download('/foo/bar').then(function() { hasDownloaded = true; }); |
| 164 |
|
| 165 |
var promise = |
| 166 |
downloader.waitForDownload('/foo/bar') |
| 167 |
.then(function() { return downloader.getDownloadedBlob('/foo/bar'); }) |
| 168 |
.then(function(blob) { |
| 169 |
assertTrue(hasDownloaded); |
| 170 |
assertEquals('data', blob.toString()); |
| 171 |
}); |
| 172 |
|
| 173 |
xhr.simulateResponse(200, 'data'); |
| 174 |
return promise; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
function testIsDownloadedDuringDownload() { |
| 179 |
var hasDownloaded = false; |
| 180 |
downloader.download('/foo/bar').then(function() { hasDownloaded = true; }); |
| 181 |
|
| 182 |
var promise = downloader.waitForDownload('/foo/bar') |
| 183 |
.then(function() { |
| 184 |
return downloader.isDownloaded('/foo/bar'); |
| 185 |
}) |
| 186 |
.then(function() { |
| 187 |
assertTrue(hasDownloaded); |
| 188 |
}); |
| 189 |
|
| 190 |
xhr.simulateResponse(200, 'data'); |
| 191 |
return promise; |
| 192 |
} |
| 193 |
|
| 194 |
function testRemoveDuringDownload() { |
| 195 |
var hasDownloaded = false; |
| 196 |
downloader.download('/foo/bar').then(function() { hasDownloaded = true; }); |
| 197 |
|
| 198 |
var promise = |
| 199 |
downloader.waitForDownload('/foo/bar') |
| 200 |
.then(function() { return downloader.remove('/foo/bar'); }) |
| 201 |
.then(function() { assertTrue(hasDownloaded); }) |
| 202 |
.then(function() { return downloader.isDownloaded('/foo/bar'); }) |
| 203 |
.then(assertFalse); |
| 204 |
|
| 205 |
xhr.simulateResponse(200, 'data'); |
| 206 |
return promise; |
| 207 |
} |
| 208 |
|
| 209 |
function testSetBlobDuringDownload() { |
| 210 |
var download = downloader.download('/foo/bar'); |
| 211 |
|
| 212 |
var promise = |
| 213 |
downloader.waitForDownload('/foo/bar') |
| 214 |
.then(function() { |
| 215 |
return downloader.setBlob( |
| 216 |
'/foo/bar', goog.testing.fs.getBlob('blob data')); |
| 217 |
}) |
| 218 |
.then( |
| 219 |
function() { |
| 220 |
fail('Should not be able to set blob during a download.'); |
| 221 |
}, |
| 222 |
function(err) { |
| 223 |
assertEquals( |
| 224 |
goog.fs.Error.ErrorCode.INVALID_MODIFICATION, |
| 225 |
err.fileError.code); |
| 226 |
return download; |
| 227 |
}) |
| 228 |
.then(function() { return downloader.getDownloadedBlob('/foo/bar'); }) |
| 229 |
.then(function(b) { assertEquals('xhr data', b.toString()); }); |
| 230 |
|
| 231 |
xhr.simulateResponse(200, 'xhr data'); |
| 232 |
return promise; |
| 233 |
} |
| 234 |
|
| 235 |
function testDownloadCanceledBeforeXhr() { |
| 236 |
var download = downloader.download('/foo/bar'); |
| 237 |
|
| 238 |
var promise = |
| 239 |
download |
| 240 |
.then( |
| 241 |
function() { fail('Download should have been canceled.'); }, |
| 242 |
function() { |
| 243 |
assertEquals('/foo/bar', xhr.getLastUri()); |
| 244 |
assertEquals(goog.net.ErrorCode.ABORT, xhr.getLastErrorCode()); |
| 245 |
assertFalse(xhr.isActive()); |
| 246 |
|
| 247 |
return downloader.isDownloaded('/foo/bar'); |
| 248 |
}) |
| 249 |
.then(assertFalse); |
| 250 |
|
| 251 |
download.cancel(); |
| 252 |
return promise; |
| 253 |
} |
| 254 |
|
| 255 |
function testDownloadCanceledAfterXhr() { |
| 256 |
var download = downloader.download('/foo/bar'); |
| 257 |
xhr.simulateResponse(200, 'data'); |
| 258 |
download.cancel(); |
| 259 |
|
| 260 |
return download |
| 261 |
.then( |
| 262 |
function() { fail('Should not succeed after cancellation.'); }, |
| 263 |
function() { |
| 264 |
assertEquals('/foo/bar', xhr.getLastUri()); |
| 265 |
assertEquals(goog.net.ErrorCode.NO_ERROR, xhr.getLastErrorCode()); |
| 266 |
assertFalse(xhr.isActive()); |
| 267 |
|
| 268 |
return downloader.isDownloaded('/foo/bar'); |
| 269 |
}) |
| 270 |
.then(assertFalse); |
| 271 |
} |
| 272 |
|
| 273 |
function testFailedXhr() { |
| 274 |
var promise = |
| 275 |
downloader.download('/foo/bar') |
| 276 |
.then( |
| 277 |
function() { fail('Download should not have succeeded.'); }, |
| 278 |
function(err) { |
| 279 |
assertEquals('/foo/bar', err.url); |
| 280 |
assertEquals(404, err.xhrStatus); |
| 281 |
assertEquals(goog.net.ErrorCode.HTTP_ERROR, err.xhrErrorCode); |
| 282 |
assertUndefined(err.fileError); |
| 283 |
|
| 284 |
return downloader.isDownloaded('/foo/bar'); |
| 285 |
}) |
| 286 |
.then(assertFalse); |
| 287 |
|
| 288 |
xhr.simulateResponse(404); |
| 289 |
return promise; |
| 290 |
} |
| 291 |
|
| 292 |
function testFailedDownloadSave() { |
| 293 |
var promise = |
| 294 |
downloader.download('/foo/bar') |
| 295 |
.then(function() { |
| 296 |
var download = downloader.download('/foo/bar'); |
| 297 |
xhr.simulateResponse(200, 'data'); |
| 298 |
return download; |
| 299 |
}) |
| 300 |
.then( |
| 301 |
function() { |
| 302 |
fail('Should not be able to modify an active download.'); |
| 303 |
}, |
| 304 |
function(err) { |
| 305 |
assertEquals('/foo/bar', err.url); |
| 306 |
assertUndefined(err.xhrStatus); |
| 307 |
assertUndefined(err.xhrErrorCode); |
| 308 |
assertEquals( |
| 309 |
goog.fs.Error.ErrorCode.INVALID_MODIFICATION, |
| 310 |
err.fileError.code); |
| 311 |
}); |
| 312 |
|
| 313 |
xhr.simulateResponse(200, 'data'); |
| 314 |
return promise; |
| 315 |
} |
| 316 |
|
| 317 |
function testFailedGetDownloadedBlob() { |
| 318 |
return downloader.getDownloadedBlob('/foo/bar') |
| 319 |
.then( |
| 320 |
function() { fail('Should not be able to get a missing blob.'); }, |
| 321 |
function(err) { |
| 322 |
assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, err.code); |
| 323 |
}); |
| 324 |
} |
| 325 |
|
| 326 |
function testFailedRemove() { |
| 327 |
return downloader.remove('/foo/bar') |
| 328 |
.then( |
| 329 |
function() { fail('Should not be able to remove a missing file.'); }, |
| 330 |
function(err) { |
| 331 |
assertEquals(goog.fs.Error.ErrorCode.NOT_FOUND, err.code); |
| 332 |
}); |
| 333 |
} |
| 334 |
|
| 335 |
function testIsDownloading() { |
| 336 |
assertFalse(downloader.isDownloading('/foo/bar')); |
| 337 |
var promise = downloader.download('/foo/bar').then(function() { |
| 338 |
assertFalse(downloader.isDownloading('/foo/bar')); |
| 339 |
}); |
| 340 |
|
| 341 |
assertTrue(downloader.isDownloading('/foo/bar')); |
| 342 |
xhr.simulateResponse(200, 'data'); |
| 343 |
return promise; |
| 344 |
} |
| 345 |
|
| 346 |
function testIsDownloadingWhenCancelled() { |
| 347 |
assertFalse(downloader.isDownloading('/foo/bar')); |
| 348 |
var deferred = downloader.download('/foo/bar').addErrback(function() { |
| 349 |
assertFalse(downloader.isDownloading('/foo/bar')); |
| 350 |
}); |
| 351 |
|
| 352 |
assertTrue(downloader.isDownloading('/foo/bar')); |
| 353 |
deferred.cancel(); |
| 354 |
} |
| 355 |
|
| 356 |
function assertMatches(expected, actual) { |
| 357 |
assert( |
| 358 |
'Expected "' + actual + '" to match ' + expected, expected.test(actual)); |
| 359 |
} |