Search code examples
pact-jvm

how to update the incoming request in pact jvm requestFilter?


I have a spring boot API in java which is using pact-jvm for pact verification. We have a new client who wants to use the same API using a new path, which the gateway will take care of, but this causes issue for pacts, I want to intercept the request and modify the path of the request for new pacts to point to old path. I was trying to refer some material online and found this : https://medium.com/dazn-tech/pact-contract-testing-dealing-with-authentication-on-the-provider-51fd46fdaa78

The below code prints the updated value of the request, but the pact still fails with 404 error as if it is still using new path

requestFilter = { req ->
                println "incoming request : $req"
                if ("$req".contains('/new-context') ) {
                    req = "$req".replace('/new-context', '/old-context')
                    println "updated request : $req"
                }
            } 

Solution

  • The problem in the above code was I was treating req as string and doing manipulations, but it is an HttpRequest object and the below code solved the issue for me:

    requestFilter = { req ->
                    def uriText = req.getURI()
                    println "incoming request uri : $uriText"
                    if ("$uriText".contains('/new-context') ) {
                        def uriTextNew = "$uriText".replace('/new-context', '/old-context')
                        println "updated request uri : $uriTextNew"
                        URI newURI = new URI(uriTextNew)
                        req.setURI(newURI)
                    }
                }