MimeType: add class with tests, woah!
This commit is contained in:
parent
1fbfc38d6f
commit
cd1732c599
37
app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt
Normal file
37
app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
class MimeType(
|
||||||
|
val main: String,
|
||||||
|
val sub: String,
|
||||||
|
val params: Map<String, String>
|
||||||
|
) {
|
||||||
|
val short: String get() = "${main.ifEmpty { "*" }}/${sub.ifEmpty { "*" }}"
|
||||||
|
val charset: String get() = params.getOrDefault("charset", DEFAULT_CHARSET)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val DEFAULT_CHARSET = "utf-8"
|
||||||
|
val DEFAULT = MimeType("text", "gemini", mapOf("charset" to DEFAULT_CHARSET))
|
||||||
|
|
||||||
|
fun from(string: String): MimeType? {
|
||||||
|
val typeString: String
|
||||||
|
val params: Map<String, String>
|
||||||
|
if (";" in string) {
|
||||||
|
val elements = string.split(";")
|
||||||
|
typeString = elements[0]
|
||||||
|
params = mutableMapOf<String, String>()
|
||||||
|
elements.subList(1, elements.size)
|
||||||
|
.map { it.trim().lowercase() }
|
||||||
|
.map { p -> if (p.count { it == '=' } != 1) return@from null else p }
|
||||||
|
.map { it.split('=') }
|
||||||
|
.forEach { params[it[0]] = it[1] }
|
||||||
|
} else {
|
||||||
|
typeString = string.trim()
|
||||||
|
params = mapOf()
|
||||||
|
}
|
||||||
|
if (typeString.count { it == '/' } != 1)
|
||||||
|
return null
|
||||||
|
val (main, sub) = typeString.split('/').map { it.trim() }
|
||||||
|
return MimeType(main, sub, params)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
38
app/src/test/java/dev/lowrespalmtree/comet/MimeTypeTests.kt
Normal file
38
app/src/test/java/dev/lowrespalmtree/comet/MimeTypeTests.kt
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package dev.lowrespalmtree.comet
|
||||||
|
|
||||||
|
import org.junit.Assert.*
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class MimeTypeTests {
|
||||||
|
@Test
|
||||||
|
fun from() {
|
||||||
|
assertNull(MimeType.from(""))
|
||||||
|
assertNull(MimeType.from("dumb"))
|
||||||
|
assertNull(MimeType.from("dumb;dumber"))
|
||||||
|
assertNull(MimeType.from("123456"))
|
||||||
|
|
||||||
|
MimeType.from("a/b")?.run {
|
||||||
|
assertEquals("a", main)
|
||||||
|
assertEquals("b", sub)
|
||||||
|
assertEquals(mapOf<String, String>(), params)
|
||||||
|
} ?: fail()
|
||||||
|
|
||||||
|
MimeType.from("text/gemini")?.run {
|
||||||
|
assertEquals("text", main)
|
||||||
|
assertEquals("gemini", sub)
|
||||||
|
assertEquals(mapOf<String, String>(), params)
|
||||||
|
} ?: fail()
|
||||||
|
|
||||||
|
MimeType.from("text/gemini;lang=en")?.run {
|
||||||
|
assertEquals("text", main)
|
||||||
|
assertEquals("gemini", sub)
|
||||||
|
assertEquals(mapOf("lang" to "en"), params)
|
||||||
|
} ?: fail()
|
||||||
|
|
||||||
|
MimeType.from("text/gemini ;lang=en")?.run {
|
||||||
|
assertEquals("text", main)
|
||||||
|
assertEquals("gemini", sub)
|
||||||
|
assertEquals(mapOf("lang" to "en"), params)
|
||||||
|
} ?: fail()
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue