Skip to content

Commit 6e87f3d

Browse files
author
Kristine Jetzke
committedOct 5, 2019
Support bearer token with JUnit annotations - Fixes gh-925
1 parent ee255ad commit 6e87f3d

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed
 

‎provider/pact-jvm-provider-junit/README.md

+13-5
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ The following keys may be managed through the environment
212212
* `pactbroker.port`
213213
* `pactbroker.scheme`
214214
* `pactbroker.tags` (comma separated)
215-
* `pactbroker.auth.scheme`
216-
* `pactbroker.auth.username`
217-
* `pactbroker.auth.password`
215+
* `pactbroker.auth.scheme` (basic or bearer)
216+
* `pactbroker.auth.username` (for basic auth)
217+
* `pactbroker.auth.password` (for basic auth)
218+
* `pactbroker.auth.token` (for bearer auth)
218219

219220

220221
#### Using tags with the pact broker
@@ -231,7 +232,7 @@ For any other value the latest pact tagged with the specified tag is loaded.
231232

232233
Specifying multiple tags is an OR operation. For example if you specify `tags = {"dev", "prod"}` then both the latest pact file tagged with `dev` and the latest pact file taggged with `prod` is loaded.
233234

234-
#### Using basic auth with the with the pact broker
235+
#### Using authentication with the with the pact broker
235236

236237
You can use basic authentication with the `@PactBroker` annotation by setting the `authentication` value to a `@PactBrokerAuth`
237238
annotation. For example:
@@ -241,7 +242,14 @@ annotation. For example:
241242
authentication = @PactBrokerAuth(username = "test", password = "test"))
242243
```
243244

244-
The `username` and `password` values also take Java system property expressions.
245+
Bearer tokens are also supported. For example:
246+
247+
```java
248+
@PactBroker(host = "${pactbroker.url:localhost}", port = "1234", tags = {"latest", "prod", "dev"},
249+
authentication = @PactBrokerAuth(scheme = "bearer", token = "test"))
250+
```
251+
252+
The `scheme`, `token`, `username` and `password` values also take Java system property expressions.
245253

246254
Preemptive Authentication can be enabled by setting the `pact.pactbroker.httpclient.usePreemptiveAuthentication` Java
247255
system property to `true`.

‎provider/pact-jvm-provider-junit/src/test/kotlin/au/com/dius/pact/provider/junit/PactBrokerAnnotationDefaultsTest.kt

+11
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ class PactBrokerAnnotationDefaultsTest {
132132
assertThat(parseListExpression(annotation.authentication.password), contains("myPass"))
133133
}
134134

135+
@Test
136+
fun `default auth token is empty`() {
137+
assertThat(parseExpression(annotation.authentication.token), `is`(""))
138+
}
139+
140+
@Test
141+
fun `can set auth token`() {
142+
props.setProperty("pactbroker.auth.token", "myToken")
143+
assertThat(parseListExpression(annotation.authentication.token), contains("myToken"))
144+
}
145+
135146
@PactBroker
136147
class SampleBrokerClass
137148
}

‎provider/pact-jvm-provider/src/main/java/au/com/dius/pact/provider/junit/loader/PactBroker.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* Authentication to use with the pact broker, by default no authentication is used
5252
*/
5353
PactBrokerAuth authentication() default @PactBrokerAuth(scheme = "${pactbroker.auth.scheme:basic}",
54-
username = "${pactbroker.auth.username:}", password = "${pactbroker.auth.password:}");
54+
username = "${pactbroker.auth.username:}", password = "${pactbroker.auth.password:}", token = "${pactbroker.auth.token:}");
5555

5656
/**
5757
* Override the default value resolver for resolving the values in the expressions

‎provider/pact-jvm-provider/src/main/java/au/com/dius/pact/provider/junit/loader/PactBrokerAuth.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
public @interface PactBrokerAuth {
1616

1717
/**
18-
* Authentication scheme to use. The default is basic.
18+
* Authentication scheme to use. Currently bearer and basic are supported. The default is basic.
1919
*/
2020
String scheme() default "Basic";
2121

2222
/**
23-
* Username to use for authentication
23+
* Username to use for basic authentication
2424
*/
2525
String username();
2626

2727
/**
28-
* Password to use for authentication
28+
* Password to use for basic authentication
2929
*/
3030
String password();
31+
32+
/**
33+
* Token to use for bearer token authentication
34+
*/
35+
String token() default "";
3136
}

‎provider/pact-jvm-provider/src/main/java/au/com/dius/pact/provider/junit/loader/PactBrokerLoader.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ PactBrokerClient newPactBrokerClient(URI url, ValueResolver resolver) {
183183
if (this.authentication != null && !this.authentication.scheme().equalsIgnoreCase("none")) {
184184
options.put("authentication", Arrays.asList(parseExpression(this.authentication.scheme(), resolver),
185185
parseExpression(this.authentication.username(), resolver),
186-
parseExpression(this.authentication.password(), resolver)));
186+
parseExpression(this.authentication.password(), resolver),
187+
parseExpression(this.authentication.token(), resolver)));
187188
}
188189
return new PactBrokerClient(url.toString(), options);
189190
}

0 commit comments

Comments
 (0)
Please sign in to comment.