Skip to content

Commit

Permalink
Merge pull request #218 from AML14/postman-seeding
Browse files Browse the repository at this point in the history
Postman seeding
  • Loading branch information
arcuri82 authored Nov 23, 2020
2 parents 7942e0d + b13c592 commit c64b5bf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.slf4j.LoggerFactory
import java.io.File
import java.lang.IllegalStateException
import java.net.URI
import java.net.URLDecoder
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

class PostmanParser(
Expand Down Expand Up @@ -54,7 +56,7 @@ class PostmanParser(

private fun getRestAction(defaultRestActions: List<RestCallAction>, postmanRequest: Request): RestCallAction? {
val verb = postmanRequest.method
val path = URI(getEncodedPath(postmanRequest.url.raw)).path.trim()
val path = URI(postmanRequest.url.raw).path.trim()
val originalRestAction = defaultRestActions.firstOrNull { it.verb.toString() == verb && it.path.matches(path) }

if (originalRestAction == null)
Expand Down Expand Up @@ -85,10 +87,14 @@ class PostmanParser(
var value: String? = null
when (parameter) {
is HeaderParam -> value = postmanRequest.header?.find { it.key == parameter.name }?.value
is QueryParam -> value = postmanRequest.url.query?.find { it.key == parameter.name }?.value
is QueryParam -> {
value = postmanRequest.url.query?.find { it.key == parameter.name }?.value
if (value != null)
value = URLDecoder.decode(value, StandardCharsets.UTF_8.toString()) // Query params must be encoded
}
is BodyParam -> value = postmanRequest.body?.raw // Will return null for form bodies
is PathParam -> {
val path = URI(getEncodedPath(postmanRequest.url.raw)).path.trim()
val path = URI(postmanRequest.url.raw).path.trim()
value = restAction.path.getKeyValues(path)?.get(parameter.name)
}
}
Expand Down Expand Up @@ -133,18 +139,6 @@ class PostmanParser(
}
}

private fun getEncodedPath(path: String): String {
/*
WARNING: Postman doesn't encode parameter values properly. The best we can do is to manually encode
some safe-to-encode characters (i.e., they must be encoded regardless of where they occur)
*/
return path
.replace(" ", "%20")
.replace("\"", "%22")
.replace("<", "%3C")
.replace(">", "%3E")
}

private fun isFormBody(parameter: Param): Boolean {
return parameter is BodyParam && parameter.isForm()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"url": {
"raw": "http://localhost:8080/api/v1/queryHeaderPath/pathParamValue?optStringQueryParam=string1&reqStringQueryParam=string2&optStringEnumQueryParam=val2&optIntQueryParam=10&optIntEnumQueryParam=3&optBase64QueryParam=ZXhhbXBsZQ==&optBoolQueryParam=true&optDateQueryParam=2020-12-14&optTimeQueryParam=13:45:08&optDateTimeQueryParam=2020-12-14 13:45:08&optDoubleQueryParam=12.143425253&optFloatQueryParam=1.9&optLongQueryParam=3147483647&optArrayQueryParam=1,2,3,4,5,6",
"raw": "http://localhost:8080/api/v1/queryHeaderPath/pathParamValue?optStringQueryParam=string1&reqStringQueryParam=string2&optStringEnumQueryParam=val2&optIntQueryParam=10&optIntEnumQueryParam=3&optBase64QueryParam=ZXhhbXBsZQ%3D%3D&optBoolQueryParam=true&optDateQueryParam=2020-12-14&optTimeQueryParam=13%3A45%3A08&optDateTimeQueryParam=2020-12-14+13%3A45%3A08&optDoubleQueryParam=12.143425253&optFloatQueryParam=1.9&optLongQueryParam=3147483647&optArrayQueryParam=1%2C2%2C3%2C4%2C5%2C6",
"protocol": "http",
"host": [
"localhost"
Expand Down Expand Up @@ -52,7 +52,7 @@
},
{
"key": "optBase64QueryParam",
"value": "ZXhhbXBsZQ=="
"value": "ZXhhbXBsZQ%3D%3D"
},
{
"key": "optBoolQueryParam",
Expand All @@ -64,11 +64,11 @@
},
{
"key": "optTimeQueryParam",
"value": "13:45:08"
"value": "13%3A45%3A08"
},
{
"key": "optDateTimeQueryParam",
"value": "2020-12-14 13:45:08"
"value": "2020-12-14+13%3A45%3A08"
},
{
"key": "optDoubleQueryParam",
Expand All @@ -84,7 +84,7 @@
},
{
"key": "optArrayQueryParam",
"value": "1,2,3,4,5,6"
"value": "1%2C2%2C3%2C4%2C5%2C6"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"url": {
"raw": "http://localhost:8080/api/v1/queryHeaderPath/pathParamValue?optStringQueryParam=string1&optStringEnumQueryParam=out_of_range&optIntQueryParam=17.47&optIntEnumQueryParam=100&optBase64QueryParam=][¨Ç*_¿´‚≠&optBoolQueryParam=non_bool&optDateQueryParam=non_date&optTimeQueryParam=13:45:08&optDateTimeQueryParam=1800-12-14 29:45:08&optDoubleQueryParam=non_double&optFloatQueryParam=non_float&optLongQueryParam=19.43&optArrayQueryParam=1,2,1000,string,5,10,true,1.8,-1",
"raw": "http://localhost:8080/api/v1/queryHeaderPath/pathParamValue?optStringQueryParam=string1&optStringEnumQueryParam=out_of_range&optIntQueryParam=17.47&optIntEnumQueryParam=100&optBase64QueryParam=%5D%5B%C2%A8%C3%87%2A_%C2%BF%C2%B4%E2%80%9A%E2%89%A0&optBoolQueryParam=non_bool&optDateQueryParam=non_date&optTimeQueryParam=13%3A45%3A08&optDateTimeQueryParam=1800-12-14+29%3A45%3A08&optDoubleQueryParam=non_double&optFloatQueryParam=non_float&optLongQueryParam=19.43&optArrayQueryParam=1%2C2%2C1000%2Cstring%2C5%2C10%2Ctrue%2C1.8%2C-1",
"protocol": "http",
"host": [
"localhost"
Expand Down Expand Up @@ -47,7 +47,7 @@
},
{
"key": "optBase64QueryParam",
"value": "][¨Ç*_¿´‚≠"
"value": "%5D%5B%C2%A8%C3%87%2A_%C2%BF%C2%B4%E2%80%9A%E2%89%A0"
},
{
"key": "optBoolQueryParam",
Expand All @@ -59,11 +59,11 @@
},
{
"key": "optTimeQueryParam",
"value": "13:45:08"
"value": "13%3A45%3A08"
},
{
"key": "optDateTimeQueryParam",
"value": "1800-12-14 29:45:08"
"value": "1800-12-14+29%3A45%3A08"
},
{
"key": "optDoubleQueryParam",
Expand All @@ -79,7 +79,7 @@
},
{
"key": "optArrayQueryParam",
"value": "1,2,1000,string,5,10,true,1.8,-1"
"value": "1%2C2%2C1000%2Cstring%2C5%2C10%2Ctrue%2C1.8%2C-1"
}
]
},
Expand Down

0 comments on commit c64b5bf

Please sign in to comment.