io.specmatic.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 specmatic-core Show documentation
Show all versions of specmatic-core Show documentation
Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.
package io.specmatic.core.utilities
import io.specmatic.core.pattern.isPatternToken
import io.specmatic.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()
}
}