mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-09 07:16:04 -04:00
added some test cases and refactored old test
This commit is contained in:
parent
22f3145b8f
commit
a50aebf7da
@ -50,11 +50,11 @@ class MimeTypeTest : BaseActivityTest() {
|
|||||||
zimFile.createNewFile()
|
zimFile.createNewFile()
|
||||||
loadFileStream.use { inputStream ->
|
loadFileStream.use { inputStream ->
|
||||||
val outputStream: OutputStream = FileOutputStream(zimFile)
|
val outputStream: OutputStream = FileOutputStream(zimFile)
|
||||||
outputStream.use { outputStream ->
|
outputStream.use { it ->
|
||||||
val buffer = ByteArray(1024)
|
val buffer = ByteArray(1024)
|
||||||
var length: Int
|
var length: Int
|
||||||
while (inputStream.read(buffer).also { length = it } > 0) {
|
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)
|
NightModeConfig(SharedPreferenceUtil(context), context)
|
||||||
)
|
)
|
||||||
zimFileReader.getRandomArticleUrl()?.let {
|
zimFileReader.getRandomArticleUrl()?.let {
|
||||||
val mimeType = zimFileReader.readMimeType(it)
|
val mimeType = zimFileReader.readContentAndMimeType(it)
|
||||||
if (mimeType.contains("^([^ ]+).*$") || mimeType.contains(";")) {
|
if (mimeType.contains("^([^ ]+).*$") || mimeType.contains(";")) {
|
||||||
Assert.fail(
|
Assert.fail(
|
||||||
"Unable to get mime type from zim file. File = " +
|
"Unable to get mime type from zim file. File = " +
|
||||||
" $zimFile and url of article = $it"
|
" $zimFile and url of article = $it"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}.also {
|
||||||
|
zimFileReader.dispose()
|
||||||
} ?: kotlin.run {
|
} ?: kotlin.run {
|
||||||
Assert.fail("Unable to get article from zim file $zimFile")
|
Assert.fail("Unable to get article from zim file $zimFile")
|
||||||
|
}.also {
|
||||||
|
zimFileReader.dispose()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -126,8 +126,8 @@ class ZimFileReader constructor(
|
|||||||
return loadContent(uri)
|
return loadContent(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readMimeType(uri: String): String = getContentAndMimeType(uri)
|
fun readContentAndMimeType(uri: String): String = getContentAndMimeType(uri)
|
||||||
.second.replace("^([^ ]+).*$", "$1").substringBefore(";").also {
|
.second.truncateMimeType.also {
|
||||||
Log.d(TAG, "getting mimetype for $uri = $it")
|
Log.d(TAG, "getting mimetype for $uri = $it")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,5 +268,9 @@ private val Uri.filePath: String
|
|||||||
private val String.filePath: String
|
private val String.filePath: String
|
||||||
get() = substringAfter(CONTENT_PREFIX).substringBefore("#").substringBefore("?")
|
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?
|
private val Pair.parcelFileDescriptor: ParcelFileDescriptor?
|
||||||
get() = ParcelFileDescriptor.open(File(filename), ParcelFileDescriptor.MODE_READ_ONLY)
|
get() = ParcelFileDescriptor.open(File(filename), ParcelFileDescriptor.MODE_READ_ONLY)
|
||||||
|
@ -48,7 +48,11 @@ class ZimReaderContainer @Inject constructor(private val zimFileReaderFactory: F
|
|||||||
fun getRedirect(url: String): String = zimFileReader?.getRedirect(url) ?: ""
|
fun getRedirect(url: String): String = zimFileReader?.getRedirect(url) ?: ""
|
||||||
fun load(url: String, requestHeaders: Map<String, String>): WebResourceResponse {
|
fun load(url: String, requestHeaders: Map<String, String>): WebResourceResponse {
|
||||||
val data = zimFileReader?.load(url)
|
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 {
|
.apply {
|
||||||
val headers = mutableMapOf("Accept-Ranges" to "bytes")
|
val headers = mutableMapOf("Accept-Ranges" to "bytes")
|
||||||
if ("Range" in requestHeaders.keys) {
|
if ("Range" in requestHeaders.keys) {
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user