run.qontract.core.utilities.URIUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qontract-core Show documentation
Show all versions of qontract-core Show documentation
A Contract Testing Tool that leverages Gherkin to describe APIs in a human readable and machine enforceable manner
package run.qontract.core.utilities
import run.qontract.core.pattern.isPatternToken
import run.qontract.core.pattern.withoutPatternDelimiters
import java.net.URLDecoder
import java.nio.charset.StandardCharsets
import java.util.stream.Collectors
object URIUtils {
fun parseQuery(query: String?): Map {
if (query == null) {
return emptyMap()
}
val pairs = query.split("&".toRegex()).toTypedArray()
val queryParams = HashMap()
for (pair in pairs) {
val idx = pair.indexOf("=")
if(idx < 0) {
throw Exception("a part of the query string does not seem to be a key-value pair: $pair")
}
queryParams[URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8.toString())] = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8.toString())
}
return queryParams
}
fun parsePathParams(rawPath: String): Map {
val pathParts = rawPath.split("/".toRegex()).toTypedArray()
val pathParams = HashMap()
for (pathPart in java.util.Arrays.stream(pathParts).filter { isPatternToken(it) }.collect(Collectors.toList())) {
val nameAndType = getNameAndType(pathPart)
pathParams[nameAndType[0]] = "(" + nameAndType[1] + ")"
}
return pathParams
}
private fun getNameAndType(placeHolder: String): Array {
return withoutPatternDelimiters(placeHolder).split(":".toRegex()).toTypedArray()
}
}