/** * Response body to return * * @param body body in string form * @param mimeType the Content-Type response header value */ public PactDslResponse body(String body, ContentType mimeType) { return body(body, mimeType.toString()); }
/** * Define the response to return */ public PactDslResponse willRespondWith() { return new PactDslResponse(consumerPactBuilder, this, defaultRequestValues, defaultResponseValues); }
/** * Response body to return * * @param body Response body in JSON form */ public PactDslResponse body(JSONObject body) { this.responseBody = OptionalBody.body(body.toString()); if (!responseHeaders.containsKey(CONTENT_TYPE)) { matchHeader(CONTENT_TYPE, DEFAULT_JSON_CONTENT_TYPE_REGEX, ContentType.APPLICATION_JSON.toString()); } return this; }
@Pact(consumer="test_consumer") public PactFragment find(PactDslWithProvider builder) { return builder .given("test state") .uponReceiving("Find request") .path("/entity/default/" + RequestResponseJsonObjects.aggregateType + "/" + RequestResponseJsonObjects.ENTITY_ID) .method("GET") .willRespondWith() .status(200) .body(RequestResponseJsonObjects.makeFindResponse()) .toFragment(); }
@Pact(state = "provider accepts a new person", provider = "userservice", consumer = "userclient") RequestResponsePact createPersonPact(PactDslWithProvider builder) { // @formatter:off return builder .given("provider accepts a new person") .uponReceiving("a request to POST a person") .path("/user-service/users") .method("POST") .willRespondWith() .status(201) .matchHeader("Content-Type", "application/json") .body(new PactDslJsonBody() .integerType("id", 42)) .toPact(); // @formatter:on }
@Pact(consumer="sample_microservice") public RequestResponsePact createPact(PactDslWithProvider builder) throws IOException { return builder .given("weather forecast data") .uponReceiving("a request for a weather request for Hamburg") .path("/some-test-api-key/53.5511,9.9937") .method("GET") .willRespondWith() .status(200) .body(FileLoader.read("classpath:weatherApiResponse.json"), ContentType.APPLICATION_JSON) .toPact(); }
@Pact(consumer="test_consumer") public PactFragment create500(PactDslWithProvider builder) { return builder .given("test state") .uponReceiving("Create Request causing 500") .matchPath("/entity/default.*") .method("POST") .willRespondWith() .status(500) .toFragment(); }
/** * Description of the request that is expected to be received * * @param description request description */ public PactDslRequestWithPath uponReceiving(String description) { addInteraction(); return new PactDslRequestWithPath(consumerPactBuilder, request, description, defaultRequestValues, defaultResponseValues); }
public PactDslResponse(ConsumerPactBuilder consumerPactBuilder, PactDslRequestWithPath request, PactDslRequestWithoutPath defaultRequestValues, PactDslResponse defaultResponseValues) { this.consumerPactBuilder = consumerPactBuilder; this.request = request; this.defaultRequestValues = defaultRequestValues; this.defaultResponseValues = defaultResponseValues; setupDefaultValues(); }
/** * The body of the request with possible single quotes as delimiters * and using {@link QuoteUtil} to convert single quotes to double quotes if required. * * @param body Request body in string form * @param mimeType the Content-Type response header value */ public PactDslResponse bodyWithSingleQuotes(String body, ContentType mimeType) { return bodyWithSingleQuotes(body, mimeType.toString()); }
@Pact(consumer="test_consumer") public PactFragment getNotFound(PactDslWithProvider builder) { return builder .given("test state") .uponReceiving("Find non existent request") .path("/entity/default/" + RequestResponseJsonObjects.aggregateType + "/" + RequestResponseJsonObjects.ENTITY_ID) .method("GET") .willRespondWith() .status(200) .body(RequestResponseJsonObjects.makeFindNonExistentResponse()) .toFragment(); }
@Pact(state = "person 42 exists", provider = "userservice", consumer = "userclient") RequestResponsePact updatePersonPact(PactDslWithProvider builder) { // @formatter:off return builder .given("person 42 exists") .uponReceiving("a request to PUT a person") .path("/user-service/users/42") .method("PUT") .willRespondWith() .status(200) .matchHeader("Content-Type", "application/json") .body(new PactDslJsonBody() .stringType("firstName", "Zaphod") .stringType("lastName", "Beeblebrox")) .toPact(); // @formatter:on }
@Pact(consumer="test_consumer") public PactFragment post500(PactDslWithProvider builder) { return builder .given("test state") .uponReceiving("POST Request causing 500") .matchPath("/entity/default.*") .method("POST") .willRespondWith() .status(500) .toFragment(); }
/** * Description of the request that is expected to be received * * @param description request description */ public PactDslRequestWithPath uponReceiving(String description) { addInteraction(); return new PactDslRequestWithPath(consumerPactBuilder, request, description, defaultRequestValues, defaultResponseValues); }
public PactDslResponse(ConsumerPactBuilder consumerPactBuilder, PactDslRequestWithPath request, PactDslRequestWithoutPath defaultRequestValues, PactDslResponse defaultResponseValues) { this.consumerPactBuilder = consumerPactBuilder; this.request = request; this.defaultRequestValues = defaultRequestValues; this.defaultResponseValues = defaultResponseValues; setupDefaultValues(); }
/** * The body of the request with possible single quotes as delimiters * and using {@link QuoteUtil} to convert single quotes to double quotes if required. * * @param body Request body in string form * @param mimeType the Content-Type response header value */ public PactDslResponse bodyWithSingleQuotes(String body, ContentType mimeType) { return bodyWithSingleQuotes(body, mimeType.toString()); }
@Pact(consumer="test_consumer") public PactFragment findWithTriggeringEvent(PactDslWithProvider builder) { return builder .given("test state") .uponReceiving("findWithTriggeringEvent request") .path("/entity/default/" + RequestResponseJsonObjects.aggregateType + "/" + RequestResponseJsonObjects.ENTITY_ID) .query("triggeringEventToken=myeventtoken") .method("GET") .willRespondWith() .status(200) .body(RequestResponseJsonObjects.makeFindResponse()) .toFragment(); }
/** * The body of the request * * @param body Response body in Java Functional Interface Supplier that must return a string * @param mimeType the Content-Type response header value */ public PactDslResponse body(Supplier<String> body, ContentType mimeType) { return body(body, mimeType.toString()); }
/** * Define the response to return */ public PactDslResponse willRespondWith() { return new PactDslResponse(consumerPactBuilder, this, defaultRequestValues, defaultResponseValues); }
/** * Response body to return * * @param body Response body in JSON form */ public PactDslResponse body(JSONObject body) { this.responseBody = OptionalBody.body(body.toString()); if (!responseHeaders.containsKey(CONTENT_TYPE)) { matchHeader(CONTENT_TYPE, DEFAULT_JSON_CONTENT_TYPE_REGEX, ContentType.APPLICATION_JSON.toString()); } return this; }