Browse Source

Add error management for invalid/malformed cookies files

Closes #1
master
Gwendal 7 years ago
parent
commit
c9daf707e1
5 changed files with 62 additions and 1 deletions
  1. +13
    -1
      src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt
  2. +42
    -0
      src/test/kotlin/bandcampcollectiodownloader/test/BandcampCollectionDownloaderTests.kt
  3. +3
    -0
      test-data/invalidcookies_noarray.json
  4. +3
    -0
      test-data/invalidcookies_wrongkeys.json
  5. +1
    -0
      test-data/notjsoncookies.json

+ 13
- 1
src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt View File

@ -1,6 +1,7 @@
package bandcampcollectiondownloader
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.google.gson.annotations.SerializedName
import org.jsoup.Jsoup
import org.zeroturnaround.zip.ZipUtil
@ -42,6 +43,12 @@ data class ParsedStatDownload(
fun parsedCookiesToMap(parsedCookies: Array<ParsedCookie>): Map<String, String> {
val result = HashMap<String, String>()
for (parsedCookie in parsedCookies) {
if (parsedCookie.contentRaw == null) {
throw BandCampDownloaderError("Missing 'Content raw' field in cookie number ${parsedCookies.indexOf(parsedCookie) + 1}.")
}
if (parsedCookie.nameRaw == null) {
throw BandCampDownloaderError("Missing 'Name raw' field in cookie number ${parsedCookies.indexOf(parsedCookie) + 1}.")
}
result.put(parsedCookie.nameRaw, parsedCookie.contentRaw)
}
return result
@ -109,7 +116,12 @@ fun downloadAll(cookiesFile: Path, bandcampUser: String, downloadFormat: String,
throw BandCampDownloaderError("Cookies file '$cookiesFile' cannot be found.")
}
val jsonData = String(Files.readAllBytes(cookiesFile))
val parsedCookies = gson.fromJson(jsonData, Array<ParsedCookie>::class.java)
val parsedCookies =
try {
gson.fromJson(jsonData, Array<ParsedCookie>::class.java)
} catch (e: JsonSyntaxException) {
throw BandCampDownloaderError("Cookies file '$cookiesFile' is not well formed: ${e.message}")
}
val cookies = parsedCookiesToMap(parsedCookies)
// Get collection page with cookies, hence with download links


+ 42
- 0
src/test/kotlin/bandcampcollectiodownloader/test/BandcampCollectionDownloaderTests.kt View File

@ -0,0 +1,42 @@
package bandcampcollectiodownloader.test
import bandcampcollectiondownloader.BandCampDownloaderError
import bandcampcollectiondownloader.downloadAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.nio.file.Paths
/**
* Note: bli is a valid bandcamp user (completely randomly chosen),
* but for which we have no credentials (ie. valid cookies).
*/
class BandcampCollectionDownloaderTests {
@Test
fun testErrorCookiesFileNotFound() {
assertThrows<BandCampDownloaderError> {
downloadAll(Paths.get("bli"),"bli","bli", Paths.get("bli"))
}
}
@Test
fun testErrorCookiesFileInvalidJson() {
assertThrows<BandCampDownloaderError> {
downloadAll(Paths.get("./test-data/notjsoncookies.json"),"bli","bli", Paths.get("bli"))
}
}
@Test
fun testErrorCookiesFileInvalidContent_wrongkey() {
//assertThrows<BandCampDownloaderError> {
downloadAll(Paths.get("./test-data/invalidcookies_wrongkeys.json"),"bli","bli", Paths.get("bli"))
//}
}
@Test
fun testErrorCookiesFileInvalidContent_noarray() {
//assertThrows<BandCampDownloaderError> {
downloadAll(Paths.get("./test-data/invalidcookies_noarray.json"),"bli","bli", Paths.get("bli"))
//}
}
}

+ 3
- 0
test-data/invalidcookies_noarray.json View File

@ -0,0 +1,3 @@
{
"hello" : 123
}

+ 3
- 0
test-data/invalidcookies_wrongkeys.json View File

@ -0,0 +1,3 @@
[{
"hello" : 123
}]

+ 1
- 0
test-data/notjsoncookies.json View File

@ -0,0 +1 @@
Nope, not json.

Loading…
Cancel
Save