Skip to content

Commit

Permalink
Wrapped URI syntax exception in IllegalArgument exception. (#645) (#647)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0aae343)

Signed-off-by: AWSHurneyt <hurneyt@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 6a17f3b commit d6d61df
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
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()
)
}
}
}

0 comments on commit d6d61df

Please sign in to comment.