Skip to content

Commit

Permalink
Merge pull request #331 from lordofthejars/issue-330
Browse files Browse the repository at this point in the history
Issue #330 Implementing several minimal features in PactDsl
  • Loading branch information
Ronald Holshausen authored Oct 19, 2016
2 parents 1c90746 + 37beb2f commit 1f8ca88
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.xml.transform.TransformerException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class PactDslRequestWithPath {
private static final String CONTENT_TYPE = "Content-Type";
Expand Down Expand Up @@ -76,6 +77,26 @@ public PactDslRequestWithPath method(String method) {
return this;
}

/**
* Headers to be included in the request
*
* @param firstHeaderName The name of the first header
* @param firstHeaderValue The value of the first header
* @param headerNameValuePairs Additional headers in name-value pairs.
*/
public PactDslRequestWithPath headers(String firstHeaderName, String firstHeaderValue, String... headerNameValuePairs) {
if (headerNameValuePairs.length % 2 != 0) {
throw new IllegalArgumentException("Pair key value should be provided, but there is one key without value.");
}
requestHeaders.put(firstHeaderName, firstHeaderValue);

for (int i = 0; i < headerNameValuePairs.length; i+=2) {
requestHeaders.put(headerNameValuePairs[i], headerNameValuePairs[i+1]);
}

return this;
}

/**
* Headers to be included in the request
*
Expand Down Expand Up @@ -126,6 +147,75 @@ public PactDslRequestWithPath body(String body, ContentType mimeType) {
return body(body, mimeType.toString());
}

/**
* The body of the request
*
* @param body Request body in Java Functional Interface Supplier that must return a string
*/
public PactDslRequestWithPath body(Supplier<String> body) {
requestBody = OptionalBody.body(body.get());
return this;
}

/**
* The body of the request
*
* @param body Request body in Java Functional Interface Supplier that must return a string
*/
public PactDslRequestWithPath body(Supplier<String> body, String mimeType) {
requestBody = OptionalBody.body(body.get());
requestHeaders.put(CONTENT_TYPE, mimeType);
return this;
}

/**
* The body of the request
*
* @param body Request body in Java Functional Interface Supplier that must return a string
*/
public PactDslRequestWithPath body(Supplier<String> body, ContentType mimeType) {
return body(body, mimeType.toString());
}

/**
* 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
*/
public PactDslRequestWithPath bodyWithSingleQuotes(String body) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body);
}

/**
* 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
*/
public PactDslRequestWithPath bodyWithSingleQuotes(String body, String mimeType) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body, mimeType);
}

/**
* 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
*/
public PactDslRequestWithPath bodyWithSingleQuotes(String body, ContentType mimeType) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body, mimeType);
}

/**
* The body of the request
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import au.com.dius.pact.consumer.ConsumerPactBuilder;
import au.com.dius.pact.model.OptionalBody;
import com.mifmif.common.regex.Generex;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.entity.ContentType;
import org.json.JSONObject;
import org.w3c.dom.Document;

import javax.xml.transform.TransformerException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

import static au.com.dius.pact.consumer.ConsumerPactBuilder.xmlToString;

Expand Down Expand Up @@ -56,6 +61,26 @@ public PactDslRequestWithoutPath headers(Map<String, String> headers) {
return this;
}

/**
* Headers to be included in the request
*
* @param firstHeaderName The name of the first header
* @param firstHeaderValue The value of the first header
* @param headerNameValuePairs Additional headers in name-value pairs.
*/
public PactDslRequestWithoutPath headers(String firstHeaderName, String firstHeaderValue, String... headerNameValuePairs) {
if (headerNameValuePairs.length % 2 != 0) {
throw new IllegalArgumentException("Pair key value should be provided, but there is one key without value.");
}
requestHeaders.put(firstHeaderName, firstHeaderValue);

for (int i = 0; i < headerNameValuePairs.length; i+=2) {
requestHeaders.put(headerNameValuePairs[i], headerNameValuePairs[i+1]);
}

return this;
}

/**
* The query string for the request
*
Expand Down Expand Up @@ -96,6 +121,76 @@ public PactDslRequestWithoutPath body(String body, ContentType mimeType) {
return body(body, mimeType.toString());
}


/**
* The body of the request
*
* @param body Request body in Java Functional Interface Supplier that must return a string
*/
public PactDslRequestWithoutPath body(Supplier<String> body) {
requestBody = OptionalBody.body(body.get());
return this;
}

/**
* The body of the request
*
* @param body Request body in Java Functional Interface Supplier that must return a string
*/
public PactDslRequestWithoutPath body(Supplier<String> body, String mimeType) {
requestBody = OptionalBody.body(body.get());
requestHeaders.put(CONTENT_TYPE, mimeType);
return this;
}

/**
* The body of the request
*
* @param body Request body in Java Functional Interface Supplier that must return a string
*/
public PactDslRequestWithoutPath body(Supplier<String> body, ContentType mimeType) {
return body(body, mimeType.toString());
}

/**
* 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
*/
public PactDslRequestWithoutPath bodyWithSingleQuotes(String body) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body);
}

/**
* 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
*/
public PactDslRequestWithoutPath bodyWithSingleQuotes(String body, String mimeType) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body, mimeType);
}

/**
* 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
*/
public PactDslRequestWithoutPath bodyWithSingleQuotes(String body, ContentType mimeType) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body, mimeType);
}

/**
* The body of the request
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.xml.transform.TransformerException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

public class PactDslResponse {
private static final String CONTENT_TYPE = "Content-Type";
Expand Down Expand Up @@ -82,6 +83,73 @@ public PactDslResponse body(String body, ContentType mimeType) {
return body(body, mimeType.toString());
}

/**
* The body of the request
*
* @param body Response body in Java Functional Interface Supplier that must return a string
*/
public PactDslResponse body(Supplier<String> body) {
responseBody = OptionalBody.body(body.get());
return this;
}

/**
* The body of the request
*
* @param body Response body in Java Functional Interface Supplier that must return a string
*/
public PactDslResponse body(Supplier<String> body, String mimeType) {
responseBody = OptionalBody.body(body.get());
responseHeaders.put(CONTENT_TYPE, mimeType);
return this;
}

/**
* The body of the request
*
* @param body Response body in Java Functional Interface Supplier that must return a string
*/
public PactDslResponse body(Supplier<String> body, ContentType mimeType) {
return body(body, mimeType.toString());
}


/**
* 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
*/
public PactDslResponse bodyWithSingleQuotes(String body) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body);
}

/**
* 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
*/
public PactDslResponse bodyWithSingleQuotes(String body, String mimeType) {
if (body != null) {
body = QuoteUtil.convert(body);
}
return body(body, mimeType);
}

/**
* 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
*/
public PactDslResponse bodyWithSingleQuotes(String body, ContentType mimeType) {
return body(body, mimeType.toString());
}

/**
* Response body to return
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package au.com.dius.pact.consumer.dsl;

/**
* Util class for dealing with Json format
*/
public final class QuoteUtil {

private QuoteUtil() {
super();
}

/**
* Reads the input text with possible single quotes as delimiters
* and returns a String correctly formatted.
* <p>For convenience, single quotes as well as double quotes
* are allowed to delimit strings. If single quotes are
* used, any quotes, single or double, in the string must be
* escaped (prepend with a '\').
*
* @param text the input data
* @return String without single quotes
*/
public static String convert(String text) {
StringBuilder builder = new StringBuilder();
boolean single_context = false;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (ch == '\\') {
i = i + 1;
if (i < text.length()) {
ch = text.charAt(i);
if (!(single_context && ch == '\'')) {
// unescape ' inside single quotes
builder.append('\\');
}
}
} else if (ch == '\'') {
// Turn ' into ", for proper string
ch = '"';
single_context = ! single_context;
}
builder.append(ch);
}

return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package au.com.dius.pact.consumer.dsl;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class QuoteUtilTest {

@Test
public void testSingleQuotes() {
final String converted = QuoteUtil.convert("{'name': 'Alex'}");
assertEquals("{\"name\": \"Alex\"}", converted);
}

@Test
public void testSkipDoubleQuotes() {
final String converted = QuoteUtil.convert("{\"name\": \"Alex\"}");
assertEquals("{\"name\": \"Alex\"}", converted);
}

}

0 comments on commit 1f8ca88

Please sign in to comment.