added some test cases and refactored old test

This commit is contained in:
Gouri Panda 2022-05-10 12:56:31 +05:30 committed by Emmanuel Engelhart
parent 22f3145b8f
commit a50aebf7da
No known key found for this signature in database
GPG Key ID: 120B30D020B553D3
4 changed files with 60 additions and 6 deletions

View File

@ -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()
}
}
}

View File

@ -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)

View File

@ -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<String, String>): 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) {

View File

@ -0,0 +1,42 @@
/*
* Kiwix Android
* Copyright (c) 2022 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
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)
}
}