Skip to content

Commit

Permalink
feat(V4): add support for status code matchers to JUnit DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 6, 2021
1 parent 466ab17 commit c77faa7
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package au.com.dius.pact.consumer.junit.v4;

import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit.PactProviderRule;
import au.com.dius.pact.consumer.junit.PactVerification;
import au.com.dius.pact.consumer.junit.exampleclients.ConsumerClient;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.model.annotations.Pact;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class StatusCodeMatcherPactTest {

@Rule
public PactProviderRule mockTestProvider = new PactProviderRule("test_provider", PactSpecVersion.V4, this);

@Pact(provider="test_provider", consumer="v4_test_consumer")
public V4Pact createFragment(PactDslWithProvider builder) {
return builder
.uponReceiving("test interaction")
.path("/")
.method("GET")
.willRespondWith()
.successStatus()
.body("{\"responsetest\": true, \"version\": \"v3\"}")
.toPact(V4Pact.class);
}

@Test
@PactVerification
public void runTest() throws IOException {
Map expectedResponse = Map.of("responsetest", true, "version", "v3");
assertEquals(new ConsumerClient(mockTestProvider.getUrl()).getAsMap("/", ""), expectedResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import au.com.dius.pact.core.model.generators.Category
import au.com.dius.pact.core.model.generators.Generators
import au.com.dius.pact.core.model.generators.ProviderStateGenerator
import au.com.dius.pact.core.model.matchingrules.ContentTypeMatcher
import au.com.dius.pact.core.model.matchingrules.HttpStatus
import au.com.dius.pact.core.model.matchingrules.MatchingRules
import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
import au.com.dius.pact.core.model.matchingrules.RuleLogic
import au.com.dius.pact.core.model.matchingrules.StatusCodeMatcher
import au.com.dius.pact.core.support.expressions.DataType
import au.com.dius.pact.core.support.jsonArray
import com.mifmif.common.regex.Generex
Expand Down Expand Up @@ -421,6 +423,86 @@ open class PactDslResponse @JvmOverloads constructor(
return this
}

/**
* Match any HTTP Information response status (100-199)
*/
fun informationStatus(): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.Information)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = 100
return this
}

/**
* Match any HTTP success response status (200-299)
*/
fun successStatus(): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.Success)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = 200
return this
}

/**
* Match any HTTP redirect response status (300-399)
*/
fun redirectStatus(): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.Redirect)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = 300
return this
}

/**
* Match any HTTP client error response status (400-499)
*/
fun clientErrorStatus(): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.ClientError)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = 400
return this
}

/**
* Match any HTTP server error response status (500-599)
*/
fun serverErrorStatus(): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.ServerError)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = 500
return this
}

/**
* Match any HTTP non-error response status (< 400)
*/
fun nonErrorStatus(): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.NonError)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = 200
return this
}

/**
* Match any HTTP error response status (>= 400)
*/
fun errorStatus(): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.Error)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = 400
return this
}

/**
* Match any HTTP status code in the provided list
*/
fun statusCodes(statusCodes: List<Int>): PactDslResponse {
val matcher = StatusCodeMatcher(HttpStatus.StatusCodes, statusCodes)
responseMatchers.addCategory("status").addRule(matcher)
responseStatus = statusCodes.first()
return this
}

protected val isContentTypeHeaderNotSet: Boolean
get() = responseHeaders.keys.none { key -> key.equals(CONTENT_TYPE, ignoreCase = true) }
protected val contentTypeHeader: String
Expand Down

0 comments on commit c77faa7

Please sign in to comment.