Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Wrapped URI syntax exception in IllegalArgument exception. #646

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ data class ClusterMetricsInput(
return if (url.isEmpty()) {
constructUrlFromInputs()
} else {
URIBuilder(url).build()
try {
URIBuilder(url).build()
} catch (e: URISyntaxException) {
throw IllegalArgumentException("Invalid URL syntax.")
}
}
}

Expand Down Expand Up @@ -243,7 +247,11 @@ data class ClusterMetricsInput(
.setHost(SUPPORTED_HOST)
.setPort(SUPPORTED_PORT)
.setPath(path + pathParams)
uriBuilder.build()
try {
uriBuilder.build()
} catch (e: URISyntaxException) {
throw IllegalArgumentException("Invalid URL syntax.")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,40 @@ class ClusterMetricsInputTests {
)
}
}

@Test
fun `test url field contains invalid characters`() {
// GIVEN
path = ""
pathParams = ""
url = "http://localhost:9200/${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString("")}"

// WHEN + THEN
assertFailsWith<IllegalArgumentException>("Invalid URL syntax.") {
ClusterMetricsInput(
path = path,
pathParams = pathParams,
url = url,
clusters = listOf()
)
}
}

@Test
fun `test URI fields provided and url contains invalid characters`() {
// GIVEN
path = "/_cluster/health"
pathParams = "index1,index2,index3,index4,index5"
url = "http://localhost:9200/${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString("")}"

// WHEN + THEN
assertFailsWith<IllegalArgumentException>("Invalid URL syntax.") {
ClusterMetricsInput(
path = path,
pathParams = pathParams,
url = url,
clusters = listOf()
)
}
}
}
Loading