Skip to content

Commit

Permalink
Allow the gradle plugin to accept a closure to modify the request bef…
Browse files Browse the repository at this point in the history
…ore sending it
  • Loading branch information
Ronald Holshausen committed Oct 2, 2014
1 parent 6165b71 commit 59df942
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
30 changes: 30 additions & 0 deletions pact-jvm-provider-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand All @@ -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':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ProviderInfo {
String name
def startProviderTask
def terminateProviderTask
def requestFilter

List<ConsumerInfo> consumers = []

Expand Down

0 comments on commit 59df942

Please sign in to comment.