-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: more some more Groovy code to Kotlin
- Loading branch information
Ronald Holshausen
committed
Jun 17, 2020
1 parent
68b0d6c
commit d7a9b93
Showing
8 changed files
with
103 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
consumer/groovy/src/main/kotlin/au/com/dius/pact/consumer/groovy/BaseBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package au.com.dius.pact.consumer.groovy | ||
|
||
import au.com.dius.pact.core.model.generators.Generators | ||
import au.com.dius.pact.core.model.matchingrules.Category | ||
import au.com.dius.pact.core.model.matchingrules.MatchingRules | ||
import au.com.dius.pact.core.support.property | ||
import groovy.json.JsonBuilder | ||
import mu.KLogging | ||
|
||
open class BaseBuilder : Matchers() { | ||
protected fun setupBody(requestData: Map<String, Any>, request: MutableMap<String, Any>) { | ||
if (requestData.containsKey(BODY)) { | ||
val body = requestData[BODY] | ||
if (body != null && body::class.qualifiedName == "au.com.dius.pact.consumer.groovy.PactBodyBuilder") { | ||
request[BODY] = body::class.property(BODY)?.get(body) as Any | ||
val matchers = request["matchers"] as MatchingRules | ||
matchers.addCategory(body::class.property("matchers")?.get(body) as Category) | ||
val generators = request["generators"] as Generators | ||
generators.addGenerators(body::class.property("generators")?.get(body) as Generators) | ||
} else if (body != null && body !is String) { | ||
val prettyPrint = requestData["prettyPrint"] as Boolean? | ||
if (prettyPrint == null && !compactMimeTypes(requestData) || prettyPrint == true) { | ||
request[BODY] = JsonBuilder(body).toPrettyString() | ||
} else { | ||
request[BODY] = JsonBuilder(body).toString() | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object : KLogging() { | ||
const val CONTENT_TYPE = "Content-Type" | ||
const val JSON = "application/json" | ||
const val BODY = "body" | ||
const val LOCALHOST = "localhost" | ||
const val HEADER = "header" | ||
|
||
val COMPACT_MIME_TYPES = listOf("application/x-thrift+json") | ||
|
||
@JvmStatic | ||
fun compactMimeTypes(reqResData: Map<String, Any>): Boolean { | ||
return if (reqResData.containsKey("headers")) { | ||
val headers = reqResData["headers"] as Map<String, String> | ||
headers.entries.find { it.key == CONTENT_TYPE }?.value in COMPACT_MIME_TYPES | ||
} else false | ||
} | ||
|
||
@JvmStatic | ||
fun isCompactMimeType(mimetype: String) = mimetype in COMPACT_MIME_TYPES | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
consumer/groovy/src/test/groovy/au/com/dius/pact/consumer/groovy/BinaryFileSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package au.com.dius.pact.consumer.groovy | ||
|
||
import au.com.dius.pact.consumer.MockServer | ||
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'() { | ||
given: | ||
def pdf = BinaryFileSpec.getResourceAsStream('/sample.pdf').bytes | ||
def service = new PactBuilder() | ||
service { | ||
serviceConsumer 'Consumer' | ||
hasPactWith 'File Service' | ||
uponReceiving('a multipart file POST') | ||
withAttributes(path: '/get-doco') | ||
willRespondWith(status: 200, body: pdf, headers: ['Content-Type': 'application/pdf']) | ||
} | ||
|
||
when: | ||
def result = service.runTest { MockServer mockServer, context -> | ||
CloseableHttpClient httpclient = HttpClients.createDefault() | ||
def response = httpclient.withCloseable { | ||
def request = RequestBuilder.get(mockServer.url + '/get-doco').build() | ||
httpclient.execute(request) | ||
} | ||
assert response.statusLine.statusCode == 200 | ||
assert response.entity.contentLength == pdf.size() | ||
} | ||
|
||
then: | ||
result instanceof PactVerificationResult.Ok | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters