All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.specmatic.core.utilities.URIUtils.kt Maven / Gradle / Ivy

Go to download

Turn your contracts into executable specifications. Contract Driven Development - Collaboratively Design & Independently Deploy MicroServices & MicroFrontends.

There is a newer version: 2.0.37
Show newest version
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()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy