From 59df942054c2dc933bbbc9d194bc850ba1c8f391 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Thu, 2 Oct 2014 16:11:57 +1000 Subject: [PATCH] Allow the gradle plugin to accept a closure to modify the request before sending it --- pact-jvm-provider-gradle/README.md | 30 +++++++++++++++++++ .../gradle/PactVerificationTask.groovy | 1 - .../provider/gradle/ProviderClient.groovy | 10 +++++-- .../pact/provider/gradle/ProviderInfo.groovy | 1 + 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/pact-jvm-provider-gradle/README.md b/pact-jvm-provider-gradle/README.md index 66a550b8e2..d2f1873c90 100644 --- a/pact-jvm-provider-gradle/README.md +++ b/pact-jvm-provider-gradle/README.md @@ -99,6 +99,36 @@ pact { Following typical Gradle behaviour, you can set the provider task properties to the actual tasks, or to the task names as a string (for the case when they haven't been defined yet). +## Modifying the requests before they are sent + +Sometimes you may need to add things to the requests that can't be persisted in a pact file. Examples of these would +be authentication tokens, which have a small life span. The Pact Gradle plugin provides a request filter that can be +set to a closure on the provider that will be called before the request is made. This closure will receive a Map with +all the requests attributes defined on it. + +```groovy +pact { + + serviceProviders { + + provider1 { + + requestFilter = { req -> + // Add an authorization header to each request + req.headers['Authorization] = 'OAUTH eyJhbGciOiJSUzI1NiIsImN0eSI6ImFw...' + } + + hasPactWith('consumer1') { + pactFile = file('path/to/provider1-consumer1-pact.json') + } + + } + + } + +} +``` + ## Project Properties The following project properties can be specified with `-Pproperty=value` on the command line: diff --git a/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/PactVerificationTask.groovy b/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/PactVerificationTask.groovy index 52922bff4a..2d163edadb 100644 --- a/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/PactVerificationTask.groovy +++ b/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/PactVerificationTask.groovy @@ -22,7 +22,6 @@ class PactVerificationTask extends DefaultTask { @TaskAction void verifyPact() { ext.failures = [:] - Matchers$.registerStandardMatchers() providerToVerify.consumers.each { consumer -> AnsiConsole.out().println(Ansi.ansi().a('\nVerifying a pact between ').bold().a(consumer.name) .boldOff().a(' and ').bold().a(providerToVerify.name).boldOff()) diff --git a/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderClient.groovy b/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderClient.groovy index 96606b0bd5..b60e8f37b2 100644 --- a/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderClient.groovy +++ b/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderClient.groovy @@ -17,7 +17,7 @@ class ProviderClient { def requestMap = [path: request.path()] requestMap.headers = [:] if (request.headers().defined) { - requestMap.headers = JavaConverters$.MODULE$.mapAsJavaMapConverter(request.headers().get()).asJava() + requestMap.headers += JavaConverters$.MODULE$.mapAsJavaMapConverter(request.headers().get()).asJava() } if (requestMap.headers['Content-Type']) { @@ -31,11 +31,17 @@ class ProviderClient { } if (request.query().defined) { + // TODO: Accept multiple parameters with the same name requestMap.query = request.query().get().split('&')*.split('=').inject([:]) { map, entry -> - map[entry[0]] = entry[1]; map + map[entry[0]] = entry[1] + map } } + if (provider.requestFilter != null) { + provider.requestFilter(requestMap) + } + client.handler.failure = { resp -> resp } switch (request.method()) { case 'POST': diff --git a/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderInfo.groovy b/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderInfo.groovy index 984449f143..56967c07ce 100644 --- a/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderInfo.groovy +++ b/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/ProviderInfo.groovy @@ -8,6 +8,7 @@ class ProviderInfo { String name def startProviderTask def terminateProviderTask + def requestFilter List consumers = []