com.greenmoonsoftware.tea.JsonRecorder.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of green-tea-test Show documentation
Show all versions of green-tea-test Show documentation
A Groovy framework for functional testing rest services. Intended to help you relax while you REST.
package com.greenmoonsoftware.tea
import groovy.json.JsonBuilder
import groovy.xml.XmlUtil
class JsonRecorder {
final static queryEncoders = [
'application/x-www-form-urlencoded': JsonRecorder.&urlEncode
, 'application/xml': JsonRecorder.&xmlEncode
, 'application/json' : JsonRecorder.&jsonEncode
, 'application/hal+json' : JsonRecorder.&jsonEncode
].withDefault {
{ query ->
query.toString()
}
}
static record(HttpMetaData data ) {
def json = [
'host': data.host
, 'uri': data.uri
, 'method': data.method
, request: [
headers: data.requestHeaders
, 'query': urlEncode(data.queryParameters)
, 'body': encodeBody(extractContentType(data.requestHeaders), data.requestBody)
]
, response: [
headers: data.responseHeaders
, status: data.responseStatus
, 'body': data.responseBody.toString()
]
]
new groovy.json.JsonBuilder(json).toPrettyString()
}
static String extractContentType(requestHeaders) {
requestHeaders.find { k, v -> k.toLowerCase() == 'content-type'}?.value
}
static encodeBody(contentType, body) {
def encoder = queryEncoders[contentType]
encoder(body)
}
private static urlEncode(params){
def encode = { URLEncoder.encode( it, 'UTF-8') }
params.collect { "${encode(it.key)}=${encode(it.value)}" }.join('&')
}
private static xmlEncode(node) {
XmlUtil.serialize(node).toString()
}
private static jsonEncode(json) {
new JsonBuilder(json).toString()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy