From a50aebf7da454ec1f94ce0cf29507c5922f26df0 Mon Sep 17 00:00:00 2001 From: Gouri Panda Date: Tue, 10 May 2022 12:56:31 +0530 Subject: [PATCH] added some test cases and refactored old test --- .../kiwixmobile/mimetype/MimeTypeTest.kt | 10 +++-- .../kiwixmobile/core/reader/ZimFileReader.kt | 8 +++- .../core/reader/ZimReaderContainer.kt | 6 ++- .../core/reader/ZimFileReaderTest.kt | 42 +++++++++++++++++++ 4 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 core/src/sharedTestFunctions/java/org/kiwix/kiwixmobile/core/reader/ZimFileReaderTest.kt diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt index 2804effa1..1b29b90b1 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/mimetype/MimeTypeTest.kt @@ -50,11 +50,11 @@ class MimeTypeTest : BaseActivityTest() { zimFile.createNewFile() loadFileStream.use { inputStream -> val outputStream: OutputStream = FileOutputStream(zimFile) - outputStream.use { outputStream -> + outputStream.use { it -> val buffer = ByteArray(1024) var length: Int while (inputStream.read(buffer).also { length = it } > 0) { - outputStream.write(buffer, 0, length) + it.write(buffer, 0, length) } } } @@ -64,15 +64,19 @@ class MimeTypeTest : BaseActivityTest() { NightModeConfig(SharedPreferenceUtil(context), context) ) zimFileReader.getRandomArticleUrl()?.let { - val mimeType = zimFileReader.readMimeType(it) + val mimeType = zimFileReader.readContentAndMimeType(it) if (mimeType.contains("^([^ ]+).*$") || mimeType.contains(";")) { Assert.fail( "Unable to get mime type from zim file. File = " + " $zimFile and url of article = $it" ) } + }.also { + zimFileReader.dispose() } ?: kotlin.run { Assert.fail("Unable to get article from zim file $zimFile") + }.also { + zimFileReader.dispose() } } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt index 4100a3023..d687f5da3 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt @@ -126,8 +126,8 @@ class ZimFileReader constructor( return loadContent(uri) } - fun readMimeType(uri: String): String = getContentAndMimeType(uri) - .second.replace("^([^ ]+).*$", "$1").substringBefore(";").also { + fun readContentAndMimeType(uri: String): String = getContentAndMimeType(uri) + .second.truncateMimeType.also { Log.d(TAG, "getting mimetype for $uri = $it") } @@ -268,5 +268,9 @@ private val Uri.filePath: String private val String.filePath: String get() = substringAfter(CONTENT_PREFIX).substringBefore("#").substringBefore("?") +// Truncate mime-type (everything after the first space and semi-colon(if exists) +val String.truncateMimeType: String + get() = replace("^([^ ]+).*$", "$1").substringBefore(";") + private val Pair.parcelFileDescriptor: ParcelFileDescriptor? get() = ParcelFileDescriptor.open(File(filename), ParcelFileDescriptor.MODE_READ_ONLY) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt index 97814856c..ad91cf3cc 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimReaderContainer.kt @@ -48,7 +48,11 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F fun getRedirect(url: String): String = zimFileReader?.getRedirect(url) ?: "" fun load(url: String, requestHeaders: Map): WebResourceResponse { val data = zimFileReader?.load(url) - return WebResourceResponse(zimFileReader?.readMimeType(url), Charsets.UTF_8.name(), data) + return WebResourceResponse( + zimFileReader?.readContentAndMimeType(url), + Charsets.UTF_8.name(), + data + ) .apply { val headers = mutableMapOf("Accept-Ranges" to "bytes") if ("Range" in requestHeaders.keys) { diff --git a/core/src/sharedTestFunctions/java/org/kiwix/kiwixmobile/core/reader/ZimFileReaderTest.kt b/core/src/sharedTestFunctions/java/org/kiwix/kiwixmobile/core/reader/ZimFileReaderTest.kt new file mode 100644 index 000000000..04c49467a --- /dev/null +++ b/core/src/sharedTestFunctions/java/org/kiwix/kiwixmobile/core/reader/ZimFileReaderTest.kt @@ -0,0 +1,42 @@ +/* + * Kiwix Android + * Copyright (c) 2022 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.reader + +import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class ZimFileReaderTest { + @Test + fun checkMimeTypeWithSemicolon() { + val mimeTypeHtml = "text/html;" + assertEquals("text/html", mimeTypeHtml.truncateMimeType) + } + + @Test + fun checkMimeTypeWithSpecialCharacters() { + val mimeTypeCss = "text/css^([^ ]+).*\$" + assertEquals("text/css$1", mimeTypeCss.truncateMimeType) + } + + @Test + fun checkMimeTypeWithSemicolonBeforeSpecialCharacters() { + val mimeTypeCss = "text/application;^([^ ]+).*\$;" + assertEquals("text/application", mimeTypeCss.truncateMimeType) + } +}