Skip to content

Commit

Permalink
Merge pull request #1598 from Zabuzard/lambda_dsl_pact_request_response
Browse files Browse the repository at this point in the history
Adding Lambda DSL variants for request/response
  • Loading branch information
rholshausen authored Aug 22, 2022
2 parents d094716 + 7580b45 commit a9e8168
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit a9e8168

Please sign in to comment.