Skip to content

Commit

Permalink
Wrapped URI syntax exception in IllegalArgument exception. (opensearc…
Browse files Browse the repository at this point in the history
…h-project#645)

Signed-off-by: AWSHurneyt <hurneyt@amazon.com>
  • Loading branch information
AWSHurneyt authored Apr 25, 2024
1 parent c19a10a commit 0aae343
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 0aae343

Please sign in to comment.