@kesha-antonov/react-native-background-downloader 4.6.4 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -5
- package/android/src/main/java/com/eko/DownloadConstants.kt +13 -2
- package/android/src/main/java/com/eko/DownloadResult.kt +3 -1
- package/android/src/main/java/com/eko/Downloader.kt +25 -4
- package/android/src/main/java/com/eko/ProgressReporter.kt +14 -35
- package/android/src/main/java/com/eko/RNBackgroundDownloaderModuleImpl.kt +162 -76
- package/android/src/main/java/com/eko/ResumableDownloadService.kt +2 -1
- package/android/src/main/java/com/eko/ResumableDownloader.kt +75 -3
- package/android/src/main/java/com/eko/UIDTDownloadJobService.kt +34 -8
- package/android/src/main/java/com/eko/Uploader.kt +15 -11
- package/android/src/main/java/com/eko/handlers/OnBegin.kt +6 -1
- package/android/src/main/java/com/eko/uidt/CancelDownloadReceiver.kt +3 -1
- package/android/src/main/java/com/eko/uidt/UIDTJobManager.kt +18 -2
- package/android/src/main/java/com/eko/utils/RedirectResolver.kt +5 -13
- package/android/src/main/java/com/eko/utils/StorageManager.kt +5 -0
- package/android/src/test/java/com/eko/DownloaderRecoveryTest.kt +78 -0
- package/android/src/test/java/com/eko/DownloaderResumeTest.kt +97 -0
- package/android/src/test/java/com/eko/ResumableDownloaderStallTest.kt +145 -0
- package/android/src/test/java/com/eko/UploaderMultipartTest.kt +84 -0
- package/android/src/test/java/com/eko/handlers/OnBeginTest.kt +51 -0
- package/android/src/test/java/com/eko/uidt/CancelDownloadReceiverTest.kt +84 -0
- package/android/src/test/java/com/eko/utils/RedirectResolverTest.kt +67 -0
- package/ios/RNBGDTaskConfig.h +2 -0
- package/ios/RNBGDTaskConfig.mm +6 -0
- package/ios/RNBackgroundDownloader.mm +640 -90
- package/lib/DownloadTask.d.ts +2 -0
- package/lib/DownloadTask.js +17 -1
- package/lib/UploadTask.d.ts +2 -0
- package/lib/UploadTask.js +17 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +50 -36
- package/package.json +14 -11
- package/plugin/build/index.d.ts +1 -0
- package/plugin/build/index.js +18 -10
- package/react-native-background-downloader.podspec +8 -0
- package/src/DownloadTask.ts +18 -1
- package/src/UploadTask.ts +18 -1
- package/src/index.ts +73 -41
|
@@ -312,20 +312,20 @@ class Uploader(private val context: Context) {
|
|
|
312
312
|
|
|
313
313
|
// Parameters
|
|
314
314
|
config.parameters?.forEach { (key, value) ->
|
|
315
|
-
size += "$twoHyphens$boundary$crlf".
|
|
316
|
-
size += "Content-Disposition: form-data; name=\"$key\"$crlf$crlf".
|
|
317
|
-
size += "$value$crlf".
|
|
315
|
+
size += "$twoHyphens$boundary$crlf".utf8Size()
|
|
316
|
+
size += "Content-Disposition: form-data; name=\"$key\"$crlf$crlf".utf8Size()
|
|
317
|
+
size += "$value$crlf".utf8Size()
|
|
318
318
|
}
|
|
319
319
|
|
|
320
320
|
// File
|
|
321
|
-
size += "$twoHyphens$boundary$crlf".
|
|
322
|
-
size += "Content-Disposition: form-data; name=\"$fieldName\"; filename=\"$fileName\"$crlf".
|
|
323
|
-
size += "Content-Type: $mimeType$crlf$crlf".
|
|
321
|
+
size += "$twoHyphens$boundary$crlf".utf8Size()
|
|
322
|
+
size += "Content-Disposition: form-data; name=\"$fieldName\"; filename=\"$fileName\"$crlf".utf8Size()
|
|
323
|
+
size += "Content-Type: $mimeType$crlf$crlf".utf8Size()
|
|
324
324
|
size += file.length()
|
|
325
325
|
size += crlf.length
|
|
326
326
|
|
|
327
327
|
// End boundary
|
|
328
|
-
size += "$twoHyphens$boundary$twoHyphens$crlf".
|
|
328
|
+
size += "$twoHyphens$boundary$twoHyphens$crlf".utf8Size()
|
|
329
329
|
|
|
330
330
|
return size
|
|
331
331
|
}
|
|
@@ -351,8 +351,8 @@ class Uploader(private val context: Context) {
|
|
|
351
351
|
val paramPart = "$twoHyphens$boundary$crlf" +
|
|
352
352
|
"Content-Disposition: form-data; name=\"$key\"$crlf$crlf" +
|
|
353
353
|
"$value$crlf"
|
|
354
|
-
outputStream.
|
|
355
|
-
val bytesWritten = state.bytesUploaded.addAndGet(paramPart.
|
|
354
|
+
outputStream.write(paramPart.toByteArray(Charsets.UTF_8))
|
|
355
|
+
val bytesWritten = state.bytesUploaded.addAndGet(paramPart.utf8Size().toLong())
|
|
356
356
|
listener.onProgress(config.id, bytesWritten, state.bytesTotal)
|
|
357
357
|
}
|
|
358
358
|
|
|
@@ -360,8 +360,8 @@ class Uploader(private val context: Context) {
|
|
|
360
360
|
val fileHeader = "$twoHyphens$boundary$crlf" +
|
|
361
361
|
"Content-Disposition: form-data; name=\"$fieldName\"; filename=\"$fileName\"$crlf" +
|
|
362
362
|
"Content-Type: $mimeType$crlf$crlf"
|
|
363
|
-
outputStream.
|
|
364
|
-
state.bytesUploaded.addAndGet(fileHeader.
|
|
363
|
+
outputStream.write(fileHeader.toByteArray(Charsets.UTF_8))
|
|
364
|
+
state.bytesUploaded.addAndGet(fileHeader.utf8Size().toLong())
|
|
365
365
|
|
|
366
366
|
// Write file content
|
|
367
367
|
FileInputStream(file).use { fileInputStream ->
|
|
@@ -407,3 +407,7 @@ class Uploader(private val context: Context) {
|
|
|
407
407
|
}
|
|
408
408
|
}
|
|
409
409
|
}
|
|
410
|
+
|
|
411
|
+
// Multipart text is sent as UTF-8 (DataOutputStream.writeBytes would keep only each
|
|
412
|
+
// char's low byte, garbling non-ASCII names and values), so sizes count UTF-8 bytes
|
|
413
|
+
private fun String.utf8Size(): Int = toByteArray(Charsets.UTF_8).size
|
|
@@ -16,7 +16,9 @@ import java.util.concurrent.Callable
|
|
|
16
16
|
*/
|
|
17
17
|
class OnBegin(
|
|
18
18
|
private val config: RNBGDTaskConfig,
|
|
19
|
-
private val callback: BeginCallback
|
|
19
|
+
private val callback: BeginCallback,
|
|
20
|
+
// The download's own headers, so auth-protected URLs answer the HEAD request too
|
|
21
|
+
private val requestHeaders: Map<String, String> = emptyMap()
|
|
20
22
|
) : Callable<OnBeginState> {
|
|
21
23
|
|
|
22
24
|
override fun call(): OnBeginState {
|
|
@@ -44,6 +46,9 @@ class OnBegin(
|
|
|
44
46
|
// Requests only headers from the server.
|
|
45
47
|
// Prevents memory leaks for invalid connections.
|
|
46
48
|
urlConnection.requestMethod = "HEAD"
|
|
49
|
+
for ((key, value) in requestHeaders) {
|
|
50
|
+
urlConnection.setRequestProperty(key, value)
|
|
51
|
+
}
|
|
47
52
|
|
|
48
53
|
// Set timeout values to prevent downloads from staying in PENDING state
|
|
49
54
|
// when URLs are slow to respond (e.g., taking 2-6 minutes)
|
|
@@ -34,7 +34,9 @@ class CancelDownloadReceiver : BroadcastReceiver() {
|
|
|
34
34
|
|
|
35
35
|
// Snapshot liveness first: stopTask tears the job down, so afterwards we
|
|
36
36
|
// can no longer tell an active download from an already-finished one.
|
|
37
|
-
|
|
37
|
+
// A job the system stopped (e.g. waiting for Wi-Fi) is no longer active but still
|
|
38
|
+
// scheduled, and its notification with this action stays up - that counts too.
|
|
39
|
+
val wasActive = UIDTJobRegistry.isActiveJob(configId) || UIDTJobManager.isScheduledJob(context, configId)
|
|
38
40
|
|
|
39
41
|
if (!RNBackgroundDownloaderModuleImpl.stopTaskFromNative(configId)) {
|
|
40
42
|
RNBackgroundDownloaderModuleImpl.logD(
|
|
@@ -375,14 +375,29 @@ object UIDTJobManager {
|
|
|
375
375
|
UIDTJobRegistry.clearResumeState(context, configId)
|
|
376
376
|
|
|
377
377
|
// Update summary notification if grouping was enabled
|
|
378
|
-
if (jobState != null && config.groupingEnabled
|
|
379
|
-
|
|
378
|
+
if (jobState != null && config.groupingEnabled) {
|
|
379
|
+
refreshGroupSummaryAfterJobLeft(context, jobState.groupId, jobState.groupName)
|
|
380
380
|
}
|
|
381
381
|
|
|
382
382
|
RNBackgroundDownloaderModuleImpl.logD(UIDTConstants.TAG, "Cancelled UIDT job for $configId")
|
|
383
383
|
return jobState != null
|
|
384
384
|
}
|
|
385
385
|
|
|
386
|
+
/**
|
|
387
|
+
* Update a group's summary notification after a job left it (cancelled or paused),
|
|
388
|
+
* mode-aware like the completion and error paths: remove it with the last job,
|
|
389
|
+
* otherwise refresh its count and progress. Nothing else would remove it.
|
|
390
|
+
*/
|
|
391
|
+
private fun refreshGroupSummaryAfterJobLeft(context: Context, groupId: String, groupName: String) {
|
|
392
|
+
if (groupId.isEmpty()) return
|
|
393
|
+
if (UIDTJobRegistry.activeJobs.values.none { it.groupId == groupId }) {
|
|
394
|
+
UIDTJobRegistry.markGroupFinalized(groupId)
|
|
395
|
+
UIDTNotificationManager.cancelSummaryNotification(context, groupId)
|
|
396
|
+
} else {
|
|
397
|
+
UIDTNotificationManager.updateSummaryNotificationForGroup(context, groupId, groupName)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
386
401
|
/**
|
|
387
402
|
* Pause an active UIDT download.
|
|
388
403
|
* This will pause the download, cancel the UIDT job, but keep a detached notification showing paused state.
|
|
@@ -416,6 +431,7 @@ object UIDTJobManager {
|
|
|
416
431
|
|
|
417
432
|
// Remove from activeJobs - will create new job on resume
|
|
418
433
|
UIDTJobRegistry.activeJobs.remove(configId)
|
|
434
|
+
refreshGroupSummaryAfterJobLeft(context, jobState.groupId, jobState.groupName)
|
|
419
435
|
|
|
420
436
|
// Show detached paused notification (not tied to UIDT job)
|
|
421
437
|
if (config.showNotificationsEnabled) {
|
|
@@ -55,20 +55,12 @@ object RedirectResolver {
|
|
|
55
55
|
break
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
"${baseUrl.protocol}://${baseUrl.host}$location"
|
|
63
|
-
}
|
|
64
|
-
!location.startsWith("http") -> {
|
|
65
|
-
val baseUrl = URL(currentUrl)
|
|
66
|
-
"${baseUrl.protocol}://${baseUrl.host}/$location"
|
|
67
|
-
}
|
|
68
|
-
else -> location
|
|
69
|
-
}
|
|
58
|
+
// Resolve relative locations against the current URL, keeping its port,
|
|
59
|
+
// path and scheme (for "//host/path")
|
|
60
|
+
val previousUrl = currentUrl
|
|
61
|
+
currentUrl = URL(URL(currentUrl), location).toString()
|
|
70
62
|
|
|
71
|
-
RNBackgroundDownloaderModuleImpl.logD(TAG, "Redirect ${redirectCount + 1}/$maxRedirects: $
|
|
63
|
+
RNBackgroundDownloaderModuleImpl.logD(TAG, "Redirect ${redirectCount + 1}/$maxRedirects: $previousUrl -> $currentUrl")
|
|
72
64
|
redirectCount++
|
|
73
65
|
} else {
|
|
74
66
|
// Not a redirect, we've found the final URL
|
|
@@ -352,6 +352,9 @@ class StorageManager(context: Context, private val name: String) {
|
|
|
352
352
|
/**
|
|
353
353
|
* Upsert a single in-progress resumable download recovery snapshot.
|
|
354
354
|
*/
|
|
355
|
+
// Synchronized: download threads save while others remove, and an unguarded
|
|
356
|
+
// load-modify-save can write back a snapshot another thread just removed
|
|
357
|
+
@Synchronized
|
|
355
358
|
fun saveActiveDownload(info: Downloader.PausedDownloadInfo) {
|
|
356
359
|
val map = loadActiveDownloads()
|
|
357
360
|
map[info.configId] = info
|
|
@@ -361,6 +364,7 @@ class StorageManager(context: Context, private val name: String) {
|
|
|
361
364
|
/**
|
|
362
365
|
* Remove a single recovery snapshot by config ID.
|
|
363
366
|
*/
|
|
367
|
+
@Synchronized
|
|
364
368
|
fun removeActiveDownload(configId: String) {
|
|
365
369
|
val map = loadActiveDownloads()
|
|
366
370
|
if (map.remove(configId) != null) {
|
|
@@ -371,6 +375,7 @@ class StorageManager(context: Context, private val name: String) {
|
|
|
371
375
|
/**
|
|
372
376
|
* Clear all recovery snapshots.
|
|
373
377
|
*/
|
|
378
|
+
@Synchronized
|
|
374
379
|
fun clearActiveDownloads() {
|
|
375
380
|
saveActiveDownloads(emptyMap())
|
|
376
381
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
package com.eko
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import androidx.test.core.app.ApplicationProvider
|
|
5
|
+
import com.eko.utils.StorageManager
|
|
6
|
+
import org.junit.Assert.assertEquals
|
|
7
|
+
import org.junit.Assert.assertTrue
|
|
8
|
+
import org.junit.Rule
|
|
9
|
+
import org.junit.Test
|
|
10
|
+
import org.junit.rules.TemporaryFolder
|
|
11
|
+
import org.junit.runner.RunWith
|
|
12
|
+
import org.robolectric.RobolectricTestRunner
|
|
13
|
+
import org.robolectric.annotation.Config
|
|
14
|
+
import java.util.UUID
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* On launch, downloads that were running when the app died are recovered from their
|
|
18
|
+
* snapshots. One whose file is complete finished without the app (an Android 14+ job
|
|
19
|
+
* records its final size then): it's done, not paused - resuming it would fail with 416.
|
|
20
|
+
*/
|
|
21
|
+
@RunWith(RobolectricTestRunner::class)
|
|
22
|
+
@Config(sdk = [34])
|
|
23
|
+
class DownloaderRecoveryTest {
|
|
24
|
+
|
|
25
|
+
@get:Rule
|
|
26
|
+
val tempFolder = TemporaryFolder()
|
|
27
|
+
|
|
28
|
+
private val context: Context get() = ApplicationProvider.getApplicationContext()
|
|
29
|
+
|
|
30
|
+
// A store of its own, so tests don't see each other's snapshots
|
|
31
|
+
private val storage = StorageManager(context, "recovery-${UUID.randomUUID()}")
|
|
32
|
+
|
|
33
|
+
private fun snapshot(configId: String, fileBytes: Int, bytesTotal: Long) {
|
|
34
|
+
val file = tempFolder.newFile(configId).apply { writeBytes(ByteArray(fileBytes)) }
|
|
35
|
+
storage.saveActiveDownload(
|
|
36
|
+
Downloader.PausedDownloadInfo(
|
|
37
|
+
configId = configId,
|
|
38
|
+
url = "https://example.com/$configId",
|
|
39
|
+
destination = file.absolutePath,
|
|
40
|
+
headers = emptyMap(),
|
|
41
|
+
bytesDownloaded = 0,
|
|
42
|
+
bytesTotal = bytesTotal
|
|
43
|
+
)
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Test
|
|
48
|
+
fun `a download whose file is complete is recovered as finished, once`() {
|
|
49
|
+
snapshot("complete", fileBytes = 100, bytesTotal = 100)
|
|
50
|
+
|
|
51
|
+
val downloader = Downloader(context, storage)
|
|
52
|
+
|
|
53
|
+
assertEquals(setOf("complete"), downloader.takeFinishedDownloads().keys)
|
|
54
|
+
assertTrue(downloader.getAllPausedDownloads().isEmpty())
|
|
55
|
+
assertTrue(downloader.takeFinishedDownloads().isEmpty())
|
|
56
|
+
assertTrue(storage.loadActiveDownloads().isEmpty())
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test
|
|
60
|
+
fun `a download with part of its file is recovered as paused`() {
|
|
61
|
+
snapshot("partial", fileBytes = 40, bytesTotal = 100)
|
|
62
|
+
|
|
63
|
+
val downloader = Downloader(context, storage)
|
|
64
|
+
|
|
65
|
+
assertEquals(40L, downloader.getAllPausedDownloads()["partial"]?.bytesDownloaded)
|
|
66
|
+
assertTrue(downloader.takeFinishedDownloads().isEmpty())
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Test
|
|
70
|
+
fun `a download of unknown size is recovered as paused`() {
|
|
71
|
+
snapshot("unknown-size", fileBytes = 40, bytesTotal = -1)
|
|
72
|
+
|
|
73
|
+
val downloader = Downloader(context, storage)
|
|
74
|
+
|
|
75
|
+
assertTrue(downloader.getAllPausedDownloads().containsKey("unknown-size"))
|
|
76
|
+
assertTrue(downloader.takeFinishedDownloads().isEmpty())
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
package com.eko
|
|
2
|
+
|
|
3
|
+
import android.app.job.JobScheduler
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import androidx.test.core.app.ApplicationProvider
|
|
6
|
+
import com.eko.uidt.UIDTConstants
|
|
7
|
+
import com.eko.uidt.UIDTJobRegistry
|
|
8
|
+
import org.junit.Assert.assertEquals
|
|
9
|
+
import org.junit.Assert.assertTrue
|
|
10
|
+
import org.junit.Before
|
|
11
|
+
import org.junit.Rule
|
|
12
|
+
import org.junit.Test
|
|
13
|
+
import org.junit.rules.TemporaryFolder
|
|
14
|
+
import org.junit.runner.RunWith
|
|
15
|
+
import org.robolectric.RobolectricTestRunner
|
|
16
|
+
import org.robolectric.annotation.Config
|
|
17
|
+
import java.io.File
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A resumed download appends to the partial file, so it has to continue from the
|
|
21
|
+
* file's length. Continuing from a saved count that doesn't match it either keeps
|
|
22
|
+
* only the tail (no file) or duplicates bytes (the count lags the file).
|
|
23
|
+
*/
|
|
24
|
+
@RunWith(RobolectricTestRunner::class)
|
|
25
|
+
@Config(sdk = [34])
|
|
26
|
+
class DownloaderResumeTest {
|
|
27
|
+
|
|
28
|
+
@get:Rule
|
|
29
|
+
val tempFolder = TemporaryFolder()
|
|
30
|
+
|
|
31
|
+
private val context: Context get() = ApplicationProvider.getApplicationContext()
|
|
32
|
+
private val jobScheduler: JobScheduler
|
|
33
|
+
get() = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
|
34
|
+
|
|
35
|
+
private val listener = object : ResumableDownloader.DownloadListener {
|
|
36
|
+
override fun onBegin(id: String, expectedBytes: Long, headers: Map<String, String>) = Unit
|
|
37
|
+
override fun onProgress(id: String, bytesDownloaded: Long, bytesTotal: Long) = Unit
|
|
38
|
+
override fun onComplete(id: String, location: String, bytesDownloaded: Long, bytesTotal: Long) = Unit
|
|
39
|
+
override fun onError(id: String, error: String, errorCode: Int) = Unit
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@Before
|
|
43
|
+
fun clearScheduler() {
|
|
44
|
+
jobScheduler.cancelAll()
|
|
45
|
+
UIDTJobRegistry.activeJobs.clear()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private fun pauseAndResume(destination: File, bytesDownloaded: Long): Long {
|
|
49
|
+
val downloader = Downloader(context)
|
|
50
|
+
downloader.savePausedDownloadState(
|
|
51
|
+
Downloader.PausedDownloadInfo(
|
|
52
|
+
configId = "paused",
|
|
53
|
+
url = "https://example.com/file",
|
|
54
|
+
destination = destination.absolutePath,
|
|
55
|
+
headers = emptyMap(),
|
|
56
|
+
bytesDownloaded = bytesDownloaded,
|
|
57
|
+
bytesTotal = 1000,
|
|
58
|
+
metadata = "{}",
|
|
59
|
+
isAllowedOverMetered = true
|
|
60
|
+
)
|
|
61
|
+
)
|
|
62
|
+
assertTrue(downloader.resume("paused", listener))
|
|
63
|
+
return jobScheduler.allPendingJobs.single().extras.getLong(UIDTConstants.KEY_START_BYTE)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@Test
|
|
67
|
+
fun `a paused DownloadManager download whose file is gone starts over`() {
|
|
68
|
+
// Pausing a DownloadManager download removes it, and its partial file with it
|
|
69
|
+
assertEquals(0L, pauseAndResume(File(tempFolder.root, "removed.bin"), bytesDownloaded = 400))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@Test
|
|
73
|
+
fun `a resume continues from the partial file, not a lagging saved count`() {
|
|
74
|
+
val partial = tempFolder.newFile("partial.bin").apply { writeBytes(ByteArray(600)) }
|
|
75
|
+
|
|
76
|
+
assertEquals(600L, pauseAndResume(partial, bytesDownloaded = 400))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@Test
|
|
80
|
+
fun `a UIDT job continues from the partial file's length`() {
|
|
81
|
+
val partial = tempFolder.newFile("job.bin").apply { writeBytes(ByteArray(700)) }
|
|
82
|
+
|
|
83
|
+
assertEquals(700L, UIDTDownloadJobService.resumeStartByte(500, partial.absolutePath))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Test
|
|
87
|
+
fun `a UIDT job whose partial file is gone starts over`() {
|
|
88
|
+
assertEquals(0L, UIDTDownloadJobService.resumeStartByte(500, File(tempFolder.root, "gone.bin").absolutePath))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@Test
|
|
92
|
+
fun `a UIDT job with nothing saved starts from zero even if a file is there`() {
|
|
93
|
+
val stale = tempFolder.newFile("stale.bin").apply { writeBytes(ByteArray(300)) }
|
|
94
|
+
|
|
95
|
+
assertEquals(0L, UIDTDownloadJobService.resumeStartByte(0, stale.absolutePath))
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
package com.eko
|
|
2
|
+
|
|
3
|
+
import okhttp3.mockwebserver.Dispatcher
|
|
4
|
+
import okhttp3.mockwebserver.MockResponse
|
|
5
|
+
import okhttp3.mockwebserver.MockWebServer
|
|
6
|
+
import okhttp3.mockwebserver.RecordedRequest
|
|
7
|
+
import okhttp3.mockwebserver.SocketPolicy
|
|
8
|
+
import org.junit.After
|
|
9
|
+
import org.junit.Assert.assertEquals
|
|
10
|
+
import org.junit.Assert.assertNull
|
|
11
|
+
import org.junit.Assert.assertTrue
|
|
12
|
+
import org.junit.Before
|
|
13
|
+
import org.junit.Rule
|
|
14
|
+
import org.junit.Test
|
|
15
|
+
import org.junit.rules.TemporaryFolder
|
|
16
|
+
import org.junit.runner.RunWith
|
|
17
|
+
import org.robolectric.RobolectricTestRunner
|
|
18
|
+
import org.robolectric.annotation.Config
|
|
19
|
+
import java.io.File
|
|
20
|
+
import java.util.concurrent.CopyOnWriteArrayList
|
|
21
|
+
import java.util.concurrent.CountDownLatch
|
|
22
|
+
import java.util.concurrent.TimeUnit
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A transfer the network cuts off (a stall that times out, a dropped connection) continues
|
|
26
|
+
* from the partial file with a Range request, as DownloadManager and iOS do. Only when the
|
|
27
|
+
* retries get nothing through does the app get an error.
|
|
28
|
+
*/
|
|
29
|
+
@RunWith(RobolectricTestRunner::class)
|
|
30
|
+
@Config(sdk = [34])
|
|
31
|
+
class ResumableDownloaderStallTest {
|
|
32
|
+
|
|
33
|
+
@get:Rule
|
|
34
|
+
val tempFolder = TemporaryFolder()
|
|
35
|
+
|
|
36
|
+
private lateinit var server: MockWebServer
|
|
37
|
+
private val ranges = CopyOnWriteArrayList<String?>()
|
|
38
|
+
|
|
39
|
+
private val body = ByteArray(200_000) { (it % 251).toByte() }
|
|
40
|
+
|
|
41
|
+
private val finished = CountDownLatch(1)
|
|
42
|
+
@Volatile private var errorCode: Int? = null
|
|
43
|
+
@Volatile private var completed = false
|
|
44
|
+
|
|
45
|
+
private val listener = object : ResumableDownloader.DownloadListener {
|
|
46
|
+
override fun onBegin(id: String, expectedBytes: Long, headers: Map<String, String>) = Unit
|
|
47
|
+
override fun onProgress(id: String, bytesDownloaded: Long, bytesTotal: Long) = Unit
|
|
48
|
+
override fun onComplete(id: String, location: String, bytesDownloaded: Long, bytesTotal: Long) {
|
|
49
|
+
completed = true
|
|
50
|
+
finished.countDown()
|
|
51
|
+
}
|
|
52
|
+
override fun onError(id: String, error: String, errorCode: Int) {
|
|
53
|
+
this@ResumableDownloaderStallTest.errorCode = errorCode
|
|
54
|
+
finished.countDown()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Before
|
|
59
|
+
fun startServer() {
|
|
60
|
+
server = MockWebServer()
|
|
61
|
+
server.start()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@After
|
|
65
|
+
fun stopServer() {
|
|
66
|
+
server.shutdown()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// The first request gets part of the body and then fails as `firstResponse` says;
|
|
70
|
+
// later ones get the rest, from their Range
|
|
71
|
+
private fun serve(firstResponse: MockResponse.() -> MockResponse) {
|
|
72
|
+
server.dispatcher = object : Dispatcher() {
|
|
73
|
+
override fun dispatch(request: RecordedRequest): MockResponse {
|
|
74
|
+
val range = request.getHeader("Range")
|
|
75
|
+
ranges.add(range)
|
|
76
|
+
if (range == null) {
|
|
77
|
+
return MockResponse()
|
|
78
|
+
.setHeader("Content-Length", body.size.toString())
|
|
79
|
+
.setHeader("Accept-Ranges", "bytes")
|
|
80
|
+
.setBody(okio.Buffer().write(body))
|
|
81
|
+
.firstResponse()
|
|
82
|
+
}
|
|
83
|
+
val start = range.removePrefix("bytes=").removeSuffix("-").toInt()
|
|
84
|
+
return MockResponse()
|
|
85
|
+
.setResponseCode(206)
|
|
86
|
+
.setHeader("Content-Length", (body.size - start).toString())
|
|
87
|
+
.setHeader("Content-Range", "bytes $start-${body.size - 1}/${body.size}")
|
|
88
|
+
.setBody(okio.Buffer().write(body, start, body.size - start))
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private fun download(id: String): File {
|
|
94
|
+
val destination = File(tempFolder.root, id)
|
|
95
|
+
ResumableDownloader().apply {
|
|
96
|
+
readTimeoutMs = 300
|
|
97
|
+
networkRetryDelaysMs = longArrayOf(10, 10, 10)
|
|
98
|
+
}.startDownload(
|
|
99
|
+
id = id,
|
|
100
|
+
url = server.url("/file").toString(),
|
|
101
|
+
destination = destination.absolutePath,
|
|
102
|
+
headers = emptyMap(),
|
|
103
|
+
listener = listener,
|
|
104
|
+
isAllowedOverMetered = true
|
|
105
|
+
)
|
|
106
|
+
assertTrue("the download didn't finish", finished.await(10, TimeUnit.SECONDS))
|
|
107
|
+
return destination
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@Test
|
|
111
|
+
fun `a dropped connection continues from the partial file`() {
|
|
112
|
+
serve { setSocketPolicy(SocketPolicy.DISCONNECT_DURING_RESPONSE_BODY) }
|
|
113
|
+
|
|
114
|
+
val file = download("dropped")
|
|
115
|
+
|
|
116
|
+
assertNull(errorCode)
|
|
117
|
+
assertTrue(completed)
|
|
118
|
+
assertTrue(ranges.size >= 2)
|
|
119
|
+
assertTrue("the retry didn't continue from the partial file", ranges.last()!!.removePrefix("bytes=").removeSuffix("-").toInt() > 0)
|
|
120
|
+
assertTrue(file.readBytes().contentEquals(body))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
@Test
|
|
124
|
+
fun `a stalled transfer continues from the partial file`() {
|
|
125
|
+
// Some of the body, then nothing for longer than the read timeout
|
|
126
|
+
serve { throttleBody(50_000, 1, TimeUnit.SECONDS) }
|
|
127
|
+
|
|
128
|
+
val file = download("stalled")
|
|
129
|
+
|
|
130
|
+
assertNull(errorCode)
|
|
131
|
+
assertTrue(file.readBytes().contentEquals(body))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@Test
|
|
135
|
+
fun `a transfer that keeps failing reports the timeout`() {
|
|
136
|
+
server.dispatcher = object : Dispatcher() {
|
|
137
|
+
override fun dispatch(request: RecordedRequest): MockResponse =
|
|
138
|
+
MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
download("unreachable")
|
|
142
|
+
|
|
143
|
+
assertEquals(DownloadConstants.ERR_TIMED_OUT, errorCode)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
package com.eko
|
|
2
|
+
|
|
3
|
+
import androidx.test.core.app.ApplicationProvider
|
|
4
|
+
import okhttp3.mockwebserver.MockResponse
|
|
5
|
+
import okhttp3.mockwebserver.MockWebServer
|
|
6
|
+
import org.junit.After
|
|
7
|
+
import org.junit.Assert.assertEquals
|
|
8
|
+
import org.junit.Assert.assertNull
|
|
9
|
+
import org.junit.Assert.assertTrue
|
|
10
|
+
import org.junit.Before
|
|
11
|
+
import org.junit.Rule
|
|
12
|
+
import org.junit.Test
|
|
13
|
+
import org.junit.rules.TemporaryFolder
|
|
14
|
+
import org.junit.runner.RunWith
|
|
15
|
+
import org.robolectric.RobolectricTestRunner
|
|
16
|
+
import org.robolectric.annotation.Config
|
|
17
|
+
import java.util.concurrent.CountDownLatch
|
|
18
|
+
import java.util.concurrent.TimeUnit
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The body is sent with a fixed Content-Length, so the length counted up front has to
|
|
22
|
+
* be the number of bytes written. Non-ASCII text is more than one byte per character.
|
|
23
|
+
*/
|
|
24
|
+
@RunWith(RobolectricTestRunner::class)
|
|
25
|
+
@Config(sdk = [34])
|
|
26
|
+
class UploaderMultipartTest {
|
|
27
|
+
|
|
28
|
+
@get:Rule
|
|
29
|
+
val tempFolder = TemporaryFolder()
|
|
30
|
+
|
|
31
|
+
private lateinit var server: MockWebServer
|
|
32
|
+
|
|
33
|
+
@Before
|
|
34
|
+
fun startServer() {
|
|
35
|
+
server = MockWebServer()
|
|
36
|
+
server.enqueue(MockResponse().setBody("ok"))
|
|
37
|
+
server.start()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@After
|
|
41
|
+
fun stopServer() {
|
|
42
|
+
server.shutdown()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@Test
|
|
46
|
+
fun `non-ASCII field values and file names are sent as UTF-8`() {
|
|
47
|
+
val file = tempFolder.newFile("отчёт.txt").apply { writeText("payload") }
|
|
48
|
+
val done = CountDownLatch(1)
|
|
49
|
+
var failedWith: String? = null
|
|
50
|
+
var completedWith = 0
|
|
51
|
+
|
|
52
|
+
Uploader(ApplicationProvider.getApplicationContext()).startUpload(
|
|
53
|
+
RNBGDUploadTaskConfig(
|
|
54
|
+
id = "upload",
|
|
55
|
+
url = server.url("/upload").toString(),
|
|
56
|
+
source = file.absolutePath,
|
|
57
|
+
fieldName = "file",
|
|
58
|
+
parameters = mapOf("title" to "Привет, 世界"),
|
|
59
|
+
),
|
|
60
|
+
object : Uploader.UploadListener {
|
|
61
|
+
override fun onBegin(id: String, expectedBytes: Long) = Unit
|
|
62
|
+
override fun onProgress(id: String, bytesUploaded: Long, bytesTotal: Long) = Unit
|
|
63
|
+
override fun onComplete(id: String, responseCode: Int, responseBody: String, bytesUploaded: Long, bytesTotal: Long) {
|
|
64
|
+
completedWith = responseCode
|
|
65
|
+
done.countDown()
|
|
66
|
+
}
|
|
67
|
+
override fun onError(id: String, error: String, errorCode: Int) {
|
|
68
|
+
failedWith = error
|
|
69
|
+
done.countDown()
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
assertTrue(done.await(10, TimeUnit.SECONDS))
|
|
75
|
+
assertNull(failedWith)
|
|
76
|
+
assertEquals(200, completedWith)
|
|
77
|
+
|
|
78
|
+
val request = server.takeRequest(5, TimeUnit.SECONDS)!!
|
|
79
|
+
val body = request.body.readUtf8()
|
|
80
|
+
assertEquals(request.getHeader("Content-Length")!!.toLong(), request.bodySize)
|
|
81
|
+
assertTrue(body.contains("Привет, 世界"))
|
|
82
|
+
assertTrue(body.contains("filename=\"отчёт.txt\""))
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
package com.eko.handlers
|
|
2
|
+
|
|
3
|
+
import com.eko.RNBGDTaskConfig
|
|
4
|
+
import okhttp3.mockwebserver.MockResponse
|
|
5
|
+
import okhttp3.mockwebserver.MockWebServer
|
|
6
|
+
import org.junit.After
|
|
7
|
+
import org.junit.Assert.assertEquals
|
|
8
|
+
import org.junit.Before
|
|
9
|
+
import org.junit.Test
|
|
10
|
+
import org.junit.runner.RunWith
|
|
11
|
+
import org.robolectric.RobolectricTestRunner
|
|
12
|
+
import org.robolectric.annotation.Config
|
|
13
|
+
import java.util.concurrent.TimeUnit
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The HEAD request that reads the size goes to the same URL as the download, so a
|
|
17
|
+
* URL behind auth only answers it when it carries the download's headers.
|
|
18
|
+
*/
|
|
19
|
+
@RunWith(RobolectricTestRunner::class)
|
|
20
|
+
@Config(sdk = [34])
|
|
21
|
+
class OnBeginTest {
|
|
22
|
+
|
|
23
|
+
private lateinit var server: MockWebServer
|
|
24
|
+
|
|
25
|
+
@Before
|
|
26
|
+
fun startServer() {
|
|
27
|
+
server = MockWebServer()
|
|
28
|
+
server.start()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@After
|
|
32
|
+
fun stopServer() {
|
|
33
|
+
server.shutdown()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@Test
|
|
37
|
+
fun `the HEAD request sends the download's headers`() {
|
|
38
|
+
server.enqueue(MockResponse().setHeader("Content-Length", "10"))
|
|
39
|
+
val config = RNBGDTaskConfig(id = "a", url = server.url("/file").toString(), destination = "/tmp/a")
|
|
40
|
+
|
|
41
|
+
// The response is turned into a React map, which needs the native React library;
|
|
42
|
+
// only the request matters here
|
|
43
|
+
runCatching {
|
|
44
|
+
OnBegin(config, { _, _, _ -> }, mapOf("Authorization" to "Bearer token")).call()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
val request = server.takeRequest(5, TimeUnit.SECONDS)!!
|
|
48
|
+
assertEquals("HEAD", request.method)
|
|
49
|
+
assertEquals("Bearer token", request.getHeader("Authorization"))
|
|
50
|
+
}
|
|
51
|
+
}
|