|
| 1 | +package au.com.dius.pact.provider.junit5 |
| 2 | + |
| 3 | +import au.com.dius.pact.core.model.Interaction |
| 4 | +import au.com.dius.pact.core.model.Pact |
| 5 | +import au.com.dius.pact.provider.junit.Provider |
| 6 | +import au.com.dius.pact.provider.junit.State |
| 7 | +import au.com.dius.pact.provider.junit.loader.PactFolder |
| 8 | +import com.github.tomakehurst.wiremock.WireMockServer |
| 9 | +import groovy.util.logging.Slf4j |
| 10 | +import org.apache.http.HttpRequest |
| 11 | +import org.junit.jupiter.api.BeforeEach |
| 12 | +import org.junit.jupiter.api.TestTemplate |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith |
| 14 | +import ru.lanwen.wiremock.ext.WiremockResolver |
| 15 | +import ru.lanwen.wiremock.ext.WiremockUriResolver |
| 16 | + |
| 17 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.post |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo |
| 21 | + |
| 22 | +@Provider('providerInjectedHeaders') |
| 23 | +@PactFolder('pacts') |
| 24 | +@ExtendWith([ |
| 25 | + WiremockResolver, |
| 26 | + WiremockUriResolver |
| 27 | +]) |
| 28 | +@Slf4j |
| 29 | +class StateInjectedHeadersProviderTest { |
| 30 | + |
| 31 | + @TestTemplate |
| 32 | + @ExtendWith(PactVerificationInvocationContextProvider) |
| 33 | + void testTemplate(Pact pact, Interaction interaction, HttpRequest request, PactVerificationContext context) { |
| 34 | + log.info("testTemplate called: ${pact.provider.name}, ${interaction.description}") |
| 35 | + request.addHeader('X-ContractTest', 'true') |
| 36 | + |
| 37 | + context.verifyInteraction() |
| 38 | + } |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void before(PactVerificationContext context, @WiremockResolver.Wiremock WireMockServer server, |
| 42 | + @WiremockUriResolver.WiremockUri String uri) throws MalformedURLException { |
| 43 | + log.info("BeforeEach - $uri") |
| 44 | + |
| 45 | + context.setTarget(HttpTestTarget.fromUrl(new URL(uri))) |
| 46 | + |
| 47 | + server.stubFor( |
| 48 | + post(urlPathEqualTo('/accounts')) |
| 49 | + .withHeader('X-ContractTest', equalTo('true')) |
| 50 | + .willReturn(aResponse() |
| 51 | + .withStatus(201) |
| 52 | + .withHeader('Location', 'http://localhost:8090/accounts/1234')) |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + @State('an active account exists') |
| 57 | + Map<String, Object> createAccount() { |
| 58 | + [ |
| 59 | + port: 8090, |
| 60 | + accountId: '1234' |
| 61 | + ] |
| 62 | + } |
| 63 | +} |
0 commit comments