Skip to content

Commit

Permalink
fix(robots-txt): Ignore user-agent case
Browse files Browse the repository at this point in the history
  • Loading branch information
alturkovic committed Jun 26, 2023
1 parent a1608b8 commit a706320
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.alturkovic</groupId>
<artifactId>robots-txt</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
16 changes: 16 additions & 0 deletions src/test/kotlin/com/github/alturkovic/robots/txt/RobotsTxtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a706320

Please sign in to comment.