From cd1732c599ed69d14f6ba70fbfd919d5bf3b7022 Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 15 Feb 2022 17:08:39 +0100 Subject: [PATCH] MimeType: add class with tests, woah! --- .../java/dev/lowrespalmtree/comet/MimeType.kt | 37 ++++++++++++++++++ .../dev/lowrespalmtree/comet/MimeTypeTests.kt | 38 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt create mode 100644 app/src/test/java/dev/lowrespalmtree/comet/MimeTypeTests.kt diff --git a/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt b/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt new file mode 100644 index 0000000..207528d --- /dev/null +++ b/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt @@ -0,0 +1,37 @@ +package dev.lowrespalmtree.comet + +class MimeType( + val main: String, + val sub: String, + val params: Map +) { + 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 + if (";" in string) { + val elements = string.split(";") + typeString = elements[0] + params = mutableMapOf() + 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) + } + } +} \ No newline at end of file diff --git a/app/src/test/java/dev/lowrespalmtree/comet/MimeTypeTests.kt b/app/src/test/java/dev/lowrespalmtree/comet/MimeTypeTests.kt new file mode 100644 index 0000000..b91d09b --- /dev/null +++ b/app/src/test/java/dev/lowrespalmtree/comet/MimeTypeTests.kt @@ -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(), params) + } ?: fail() + + MimeType.from("text/gemini")?.run { + assertEquals("text", main) + assertEquals("gemini", sub) + assertEquals(mapOf(), 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() + } +}