Skip to content

Commit

Permalink
fix: Finally got Groovy DSL to handle binary data
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 19, 2020
1 parent cc8df03 commit 9ec845a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ class PactBuilder extends GroovyBuilder {
Map headers = setupHeaders(requestData.headers ?: [:], requestMatchers, requestGenerators)
Map query = setupQueryParameters(requestData.query ?: [:], requestMatchers, requestGenerators)
String path = setupPath(requestData.path ?: '/', requestMatchers, requestGenerators)
def requestBody = setupBody(requestData, currentInteraction.request)
this.currentInteraction.request.method = requestData.method ?: 'GET'
this.currentInteraction.request.headers = headers
this.currentInteraction.request.query = query
this.currentInteraction.request.path = path
def requestBody = setupBody(requestData, currentInteraction.request)
this.currentInteraction.request.body = requestBody
this
}
Expand All @@ -131,9 +131,9 @@ class PactBuilder extends GroovyBuilder {
MatchingRules responseMatchers = currentInteraction.response.matchingRules
Generators responseGenerators = currentInteraction.response.generators
Map responseHeaders = setupHeaders(responseData.headers ?: [:], responseMatchers, responseGenerators)
def responseBody = setupBody(responseData, currentInteraction.response)
this.currentInteraction.response.status = responseData.status ?: 200
this.currentInteraction.response.headers = responseHeaders
def responseBody = setupBody(responseData, currentInteraction.response)
this.currentInteraction.response.body = responseBody
requestState = false
this
Expand Down Expand Up @@ -190,29 +190,29 @@ class PactBuilder extends GroovyBuilder {

private setupRequestOrResponse(PactBodyBuilder body, Map options) {
if (requestState) {
currentInteraction.request.body = body.body instanceof OptionalBody ? body.body :
OptionalBody.body(body.body.bytes)
currentInteraction.request.matchingRules.addCategory(body.matchers)
currentInteraction.request.generators.addGenerators(body.generators)
if (!currentInteraction.request.contentTypeHeader()) {
if (options.mimeType) {
currentInteraction.request.headers[CONTENT_TYPE] = [ options.mimeType ]
} else {
currentInteraction.request.headers[CONTENT_TYPE] = [ JSON ]
}
}
} else {
currentInteraction.response.body = body.body instanceof OptionalBody ? body.body :
currentInteraction.request.body = body.body instanceof OptionalBody ? body.body :
OptionalBody.body(body.body.bytes)
currentInteraction.response.matchingRules.addCategory(body.matchers)
currentInteraction.response.generators.addGenerators(body.generators)
currentInteraction.request.matchingRules.addCategory(body.matchers)
currentInteraction.request.generators.addGenerators(body.generators)
} else {
if (!currentInteraction.response.contentTypeHeader()) {
if (options.mimeType) {
currentInteraction.response.headers[CONTENT_TYPE] = [ options.mimeType ]
} else {
currentInteraction.response.headers[CONTENT_TYPE] = [ JSON ]
}
}
currentInteraction.response.body = body.body instanceof OptionalBody ? body.body :
OptionalBody.body(body.body.bytes)
currentInteraction.response.matchingRules.addCategory(body.matchers)
currentInteraction.response.generators.addGenerators(body.generators)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@ import mu.KLogging
import java.util.regex.Pattern

open class BaseBuilder : Matchers() {
protected fun setupBody(requestData: Map<String, Any>, httpPart: HttpPart): OptionalBody {
return if (requestData.containsKey(BODY)) {
val body = requestData[BODY]
protected fun setupBody(data: Map<String, Any>, httpPart: HttpPart): OptionalBody {
return if (data.containsKey(BODY)) {
val body = data[BODY]
val contentType = httpPart.determineContentType()
if (body != null && body::class.qualifiedName == "au.com.dius.pact.consumer.groovy.PactBodyBuilder") {
httpPart.matchingRules.addCategory(body::class.property("matchers")?.get(body) as Category)
httpPart.generators.addGenerators(body::class.property("generators")?.get(body) as Generators)
OptionalBody.body(body::class.property(BODY)?.get(body).toString().toByteArray())
OptionalBody.body(body::class.property(BODY)?.get(body).toString().toByteArray(contentType.asCharset()))
} else if (body != null && body !is String) {
val prettyPrint = requestData["prettyPrint"] as Boolean?
if (prettyPrint == null && !compactMimeTypes(requestData) || prettyPrint == true) {
OptionalBody.body(JsonBuilder(body).toPrettyString().toByteArray())
if (contentType.isBinaryType()) {
when (body) {
is ByteArray -> OptionalBody.body(body)
else -> OptionalBody.body(body.toString().toByteArray(contentType.asCharset()))
}
} else {
OptionalBody.body(JsonBuilder(body).toString().toByteArray())
val prettyPrint = data["prettyPrint"] as Boolean?
if (prettyPrint == null && !compactMimeTypes(data) || prettyPrint == true) {
OptionalBody.body(JsonBuilder(body).toPrettyString().toByteArray(contentType.asCharset()))
} else {
OptionalBody.body(JsonBuilder(body).toString().toByteArray(contentType.asCharset()))
}
}
} else {
OptionalBody.body(body.toString().toByteArray())
OptionalBody.body(body.toString().toByteArray(contentType.asCharset()))
}
} else {
OptionalBody.missing()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import au.com.dius.pact.consumer.PactVerificationResult
import org.apache.http.client.methods.RequestBuilder
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClients
import spock.lang.Ignore
import spock.lang.Specification

@Ignore
class BinaryFileSpec extends Specification {

def 'handles bodies from form posts'() {
Expand Down

0 comments on commit 9ec845a

Please sign in to comment.