From a70632010f74029f5e6414aaaa464975ba51b481 Mon Sep 17 00:00:00 2001 From: Alen Turkovic Date: Mon, 26 Jun 2023 13:50:42 +0200 Subject: [PATCH] fix(robots-txt): Ignore user-agent case --- pom.xml | 2 +- .../github/alturkovic/robots/txt/StringUtils.kt | 2 +- .../alturkovic/robots/txt/RobotsTxtTest.kt | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 45bc1ab..7975dfa 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.github.alturkovic robots-txt - 1.0.0 + 1.0.1 1.8 diff --git a/src/main/kotlin/com/github/alturkovic/robots/txt/StringUtils.kt b/src/main/kotlin/com/github/alturkovic/robots/txt/StringUtils.kt index 55607cf..6972428 100644 --- a/src/main/kotlin/com/github/alturkovic/robots/txt/StringUtils.kt +++ b/src/main/kotlin/com/github/alturkovic/robots/txt/StringUtils.kt @@ -5,7 +5,7 @@ import kotlin.math.min internal object StringUtils { fun greatestCommonPrefix(a: String, b: String): String { val minLength = min(a.length, b.length) - for (i in 0 until minLength) if (a[i] != b[i]) return a.substring(0, i) + for (i in 0 until minLength) if (a[i].lowercaseChar() != b[i].lowercaseChar()) return a.substring(0, i) return a.substring(0, minLength) } } diff --git a/src/test/kotlin/com/github/alturkovic/robots/txt/RobotsTxtTest.kt b/src/test/kotlin/com/github/alturkovic/robots/txt/RobotsTxtTest.kt index c7bd007..8118fc6 100644 --- a/src/test/kotlin/com/github/alturkovic/robots/txt/RobotsTxtTest.kt +++ b/src/test/kotlin/com/github/alturkovic/robots/txt/RobotsTxtTest.kt @@ -118,6 +118,22 @@ private class RobotsTxtTest { assertThat(robotsTxt.query("BarBot", "/private/foo.txt").allowed).isFalse() } + @Test + fun shouldAllowSpecificUserAgentIgnoringCaseSensitivity() { + val robotsTxt = RobotsTxtReader.read( + """ + User-agent: * + Disallow: / + + User-Agent: FooBot + Allow: / + """.trimIndent().byteInputStream() + ) + + assertThat(robotsTxt.query("fooBot", "/private/foo.txt").allowed).isTrue() + assertThat(robotsTxt.query("BarBot", "/private/foo.txt").allowed).isFalse() + } + @Test fun shouldAllowWildcards() { val robotsTxt = RobotsTxtReader.read(