Skip to content

Commit 2955ca5

Browse files
Phil EndsleyPhil Endsley
Phil Endsley
authored and
Phil Endsley
committed
Step 10 - Add request filter in provider test
1 parent 497c57a commit 2955ca5

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,43 @@ FAILURE: Build failed with an exception.
921921
```
922922

923923
Oh, dear. _More_ tests are failing. Can you understand why?
924+
925+
## Step 10 - Request Filters on the Provider
926+
927+
Because our pact file has static data in it, our bearer token is now out of date, so when Pact verification passes it to the Provider we get a `401`. There are multiple ways to resolve this - mocking or stubbing out the authentication component is a common one. In our use case, we are going to use a process referred to as _Request Filtering_, using a `RequestFilter`.
928+
929+
_NOTE_: This is an advanced concept and should be used carefully, as it has the potential to invalidate a contract by bypassing its constraints. See https://docs.pact.io/implementation_guides/jvm/provider/junit5/#modifying-the-requests-before-they-are-sent for more details on this.
930+
931+
The approach we are going to take to inject the header is as follows:
932+
933+
1. If we receive any Authorization header, we override the incoming request with a valid (in time) Authorization header, and continue with whatever call was being made
934+
1. If we don't receive an Authorization header, we do nothing
935+
936+
_NOTE_: We are not considering the `403` scenario in this example.
937+
938+
In `provider/src/test/java/au/com/dius/pactworkshop/provider/ProductPactProviderTest.java`:
939+
940+
```java
941+
@TestTemplate
942+
@ExtendWith(PactVerificationInvocationContextProvider.class)
943+
void verifyPact(PactVerificationContext context, HttpRequest request) {
944+
replaceAuthHeader(request);
945+
context.verifyInteraction();
946+
}
947+
948+
private void replaceAuthHeader(HttpRequest request) {
949+
if (request.containsHeader("Authorization")) {
950+
String header = "Bearer " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(new Date());
951+
request.removeHeaders("Authorization");
952+
request.addHeader("Authorization", header);
953+
}
954+
}
955+
```
956+
957+
We can now run the Provider tests
958+
959+
```console
960+
./gradlew provider:test --tests *Pact*Test
961+
962+
BUILD SUCCESSFUL in 1s
963+
```

provider/src/test/java/au/com/dius/pactworkshop/provider/ProductPactProviderTest.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import au.com.dius.pact.provider.junitsupport.Provider;
77
import au.com.dius.pact.provider.junitsupport.State;
88
import au.com.dius.pact.provider.junitsupport.loader.PactFolder;
9+
import org.apache.http.HttpRequest;
910
import org.junit.jupiter.api.BeforeEach;
1011
import org.junit.jupiter.api.TestTemplate;
1112
import org.junit.jupiter.api.extension.ExtendWith;
@@ -14,7 +15,9 @@
1415
import org.springframework.boot.web.server.LocalServerPort;
1516
import org.springframework.test.context.junit.jupiter.SpringExtension;
1617

18+
import java.text.SimpleDateFormat;
1719
import java.util.Collections;
20+
import java.util.Date;
1821
import java.util.List;
1922
import java.util.Optional;
2023

@@ -39,10 +42,19 @@ void setUp(PactVerificationContext context) {
3942

4043
@TestTemplate
4144
@ExtendWith(PactVerificationInvocationContextProvider.class)
42-
void verifyPact(PactVerificationContext context) {
45+
void verifyPact(PactVerificationContext context, HttpRequest request) {
46+
replaceAuthHeader(request);
4347
context.verifyInteraction();
4448
}
4549

50+
private void replaceAuthHeader(HttpRequest request) {
51+
if (request.containsHeader("Authorization")) {
52+
String header = "Bearer " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(new Date());
53+
request.removeHeaders("Authorization");
54+
request.addHeader("Authorization", header);
55+
}
56+
}
57+
4658
@State("products exist")
4759
void toProductsExistState() {
4860
when(productRepository.fetchAll()).thenReturn(

0 commit comments

Comments
 (0)