From 7580b45998bd421d49bbdd5258e479124fc7cf98 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 19 Aug 2022 14:57:09 +0200 Subject: [PATCH] Adding Lambda DSL variants for request/response * and a sample that showcases them --- .../consumer/dsl/PactDslRequestWithPath.kt | 10 +++++ .../consumer/dsl/PactDslRequestWithoutPath.kt | 39 +++++++++++++++++++ .../dsl/samples/PactLambdaDslSamples.kt | 33 ++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/samples/PactLambdaDslSamples.kt diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithPath.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithPath.kt index 8cbb3f020a..ccdb3b48e5 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithPath.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithPath.kt @@ -409,6 +409,16 @@ open class PactDslRequestWithPath : PactDslRequestBase { version, additionalMetadata) } + /** + * Variant of [PactDslRequestWithPath.willRespondWith] that introduces a Lambda DSL syntax to better visually + * separate request and response in a pact. + * + * @see PactDslRequestWithPath.willRespondWith + * @sample au.com.dius.pact.consumer.dsl.samples.PactLambdaDslSamples.requestResponse + */ + fun willRespondWith(addResponseMatchers: PactDslResponse.() -> PactDslResponse): PactDslResponse = + addResponseMatchers(willRespondWith()) + /** * Match a query parameter with a regex. A random query parameter value will be generated from the regex * if the example value is not provided. diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithoutPath.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithoutPath.kt index 2244e59e4e..4ed915a696 100644 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithoutPath.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRequestWithoutPath.kt @@ -291,6 +291,18 @@ open class PactDslRequestWithoutPath @JvmOverloads constructor( defaultRequestValues, defaultResponseValues, comments, version, additionalMetadata) } + /** + * Variant of [PactDslRequestWithPath.path] that introduces a Lambda DSL syntax to better visually separate + * request and response in a pact. + * + * @see PactDslRequestWithPath.path + * @sample au.com.dius.pact.consumer.dsl.samples.PactLambdaDslSamples.requestResponse + */ + inline fun path( + path: String, + addRequestMatchers: PactDslRequestWithPath.() -> PactDslRequestWithPath + ): PactDslRequestWithPath = addRequestMatchers(path(path)) + /** * The path of the request. This will generate a random path to use when generating requests if the example * value is not provided. @@ -311,6 +323,20 @@ open class PactDslRequestWithoutPath @JvmOverloads constructor( defaultRequestValues, defaultResponseValues, comments, version, additionalMetadata) } + /** + * Variant of [PactDslRequestWithoutPath.matchPath] that introduces a Lambda DSL syntax to better visually separate + * request and response in a pact. + * + * @see PactDslRequestWithoutPath.matchPath + * @sample au.com.dius.pact.consumer.dsl.samples.PactLambdaDslSamples.requestResponse + */ + @JvmOverloads + inline fun matchPath( + pathRegex: String, + path: String = Generex(pathRegex).random(), + addRequestMatchers: PactDslRequestWithPath.() -> PactDslRequestWithPath + ): PactDslRequestWithPath = addRequestMatchers(matchPath(pathRegex, path)) + /** * Sets up a file upload request. This will add the correct content type header to the request * @param partName This is the name of the part in the multipart body. @@ -365,6 +391,19 @@ open class PactDslRequestWithoutPath @JvmOverloads constructor( defaultRequestValues, defaultResponseValues, comments, version, additionalMetadata) } + /** + * Variant of [PactDslRequestWithoutPath.pathFromProviderState] that introduces a Lambda DSL syntax to better + * visually separate request and response in a pact. + * + * @see PactDslRequestWithoutPath.pathFromProviderState + * @sample au.com.dius.pact.consumer.dsl.samples.PactLambdaDslSamples.requestResponse + */ + inline fun pathFromProviderState( + expression: String, + example: String, + addRequestMatchers: PactDslRequestWithPath.() -> PactDslRequestWithPath + ): PactDslRequestWithPath = addRequestMatchers(pathFromProviderState(expression, example)) + /** * Matches a date field using the provided date pattern * @param field field name diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/samples/PactLambdaDslSamples.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/samples/PactLambdaDslSamples.kt new file mode 100644 index 0000000000..13695323a8 --- /dev/null +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/samples/PactLambdaDslSamples.kt @@ -0,0 +1,33 @@ +package au.com.dius.pact.consumer.dsl.samples + +import au.com.dius.pact.consumer.dsl.PactDslWithProvider +import au.com.dius.pact.consumer.dsl.newJsonObject +import au.com.dius.pact.core.model.RequestResponsePact + +/** + * Samples of using Lambda DSL for creating pacts. + */ +object PactLambdaDslSamples { + + /** + * Shows how Lambda DSL can be used to visually separate the request and the response + * section from each other. + */ + fun requestResponse(builder: PactDslWithProvider): RequestResponsePact { + return builder.given("no existing users") + .uponReceiving("create a new user") + .path("users") { + // Lambda DSL on request, this: PactDslRequestWithPath + headers("X-Locale", "en-US") + method("PUT") + }.willRespondWith { + // Lambda DSL on response, this: PactDslResponse + successStatus() + body( + newJsonObject { + uuid("name") + } + ) + }.toPact() + } +}