ITV / scala-pact

A Scala implementation of CDC using the Pact standard
Other
108 stars 54 forks source link

Modifying requests before verification #201

Closed BenFradet closed 3 years ago

BenFradet commented 3 years ago

Hello, is there a way to modify requests that are sent for verification like what pact-js does?

jbwheatley commented 3 years ago

Hi @BenFradet, nothing like that is currently possible in scala-pact. If this is a common feature across other implementations we can maybe look into adding support for it.

solarmosaic-kflorence commented 3 years ago

@BenFradet this is possible now in the latest version, 3.0.0: https://github.com/ITV/scala-pact/blob/master/example/provider_pact-for-verification/src/test/scala/com/example/provider/VerifyContractsSpec.scala#L43

 describe("Verifying Consumer Contracts") {
    it("should be able to verify it's contracts") {
      val consumer = ConsumerVersionSelector("example", latest = true) // This should fetch all the latest pacts of consumer with tag example

      verifyPact
        .withPactSource(
          pactBrokerWithVersionSelectors(
            "https://test.pact.dius.com.au",
            "scala-pact-provider",
            List(consumer),
            List(),
            PendingPactSettings.PendingDisabled,
            None,
            //again, these are publicly known creds for a test pact-broker
            PactBrokerAuthorization(pactBrokerCredentials = ("dXfltyFMgNOFZAxr8io9wJ37iUpY42M", "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1"), ""),
            Some(5.seconds)
          )
        )
        .setupProviderState("given") {
          case "Results: Bob, Fred, Harry" =>
            val newHeader = "Pact" -> "modifiedRequest"
            ProviderStateResult(true, req => req.copy(headers = Option(req.headers.fold(Map(newHeader))(_ + newHeader))))
        }
        .runVerificationAgainst("localhost", 8080, 10.seconds)
    }
  }
BenFradet commented 3 years ago

Excellent, I was going to start needing it and working on it. Thanks a lot! :+1: