Skip to content

Commit

Permalink
fix: content type header check must be case insenstive #1121
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 14, 2020
1 parent 04236db commit 0fa689f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@ protected PactDslRequestBase queryMatchingDatetimeBase(String field, String patt
}
return this;
}

protected boolean isContentTypeHeaderNotSet() {
return requestHeaders.keySet().stream().noneMatch(key -> key.equalsIgnoreCase(CONTENT_TYPE));
}

protected String getContentTypeHeader() {
return requestHeaders.entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase(CONTENT_TYPE))
.findFirst()
.map(entry -> entry.getValue().get(0)).orElse("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.function.Supplier;

public class PactDslRequestWithPath extends PactDslRequestBase {
private static final String CONTENT_TYPE = "Content-Type";

private final ConsumerPactBuilder consumerPactBuilder;

Expand Down Expand Up @@ -267,11 +266,11 @@ public PactDslRequestWithPath bodyWithSingleQuotes(String body, ContentType cont
public PactDslRequestWithPath body(JSONObject body) {
requestBody = OptionalBody.body(body.toString().getBytes(),
au.com.dius.pact.core.model.ContentType.Companion.getJSON());
if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_JSON.toString()));
requestBody = OptionalBody.body(body.toString().getBytes());
} else {
ContentType contentType = ContentType.parse(requestHeaders.get(CONTENT_TYPE).get(0));
ContentType contentType = ContentType.parse(getContentTypeHeader());
Charset charset = contentType.getCharset() != null ? contentType.getCharset() : Charset.defaultCharset();
requestBody = OptionalBody.body(body.toString().getBytes(charset),
new au.com.dius.pact.core.model.ContentType(contentType.toString()));
Expand All @@ -296,10 +295,10 @@ public PactDslRequestWithPath body(DslPart body) {

Charset charset = Charset.defaultCharset();
String contentType = ContentType.APPLICATION_JSON.toString();
if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(contentType));
} else {
contentType = requestHeaders.get(CONTENT_TYPE).get(0);
contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
}
Expand All @@ -320,13 +319,13 @@ public PactDslRequestWithPath body(DslPart body) {
* @param body XML Document
*/
public PactDslRequestWithPath body(Document body) throws TransformerException {
if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
String contentType = ContentType.APPLICATION_XML.toString();
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(contentType));
requestBody = OptionalBody.body(ConsumerPactBuilder.xmlToString(body).getBytes(),
new au.com.dius.pact.core.model.ContentType(contentType));
} else {
String contentType = requestHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
requestBody = OptionalBody.body(ConsumerPactBuilder.xmlToString(body).getBytes(charset),
Expand All @@ -345,11 +344,11 @@ public PactDslRequestWithPath body(PactXmlBuilder xmlBuilder) {
requestMatchers.addCategory(xmlBuilder.getMatchingRules());
requestGenerators.addGenerators(xmlBuilder.getGenerators());

if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_XML.toString()));
requestBody = OptionalBody.body(xmlBuilder.asBytes());
} else {
String contentType = requestHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
requestBody = OptionalBody.body(xmlBuilder.asBytes(charset),
Expand Down Expand Up @@ -612,4 +611,5 @@ public PactDslRequestWithPath queryMatchingISODatetime(String field, String exam
public PactDslRequestWithPath queryMatchingISODatetime(String field) {
return queryMatchingISODatetime(field, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ public PactDslRequestWithoutPath bodyWithSingleQuotes(String body, ContentType c
* @param body Request body in JSON form
*/
public PactDslRequestWithoutPath body(JSONObject body) {
if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_JSON.toString()));
requestBody = OptionalBody.body(body.toString().getBytes());
} else {
String contentType = requestHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
requestBody = OptionalBody.body(body.toString().getBytes(charset),
Expand All @@ -232,11 +232,11 @@ public PactDslRequestWithoutPath body(DslPart body) {
DslPart parent = body.close();
requestMatchers.addCategory(parent.matchers);
requestGenerators.addGenerators(parent.generators);
if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_JSON.toString()));
requestBody = OptionalBody.body(parent.toString().getBytes());
} else {
String contentType = requestHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
requestBody = OptionalBody.body(parent.toString().getBytes(charset),
Expand All @@ -251,11 +251,11 @@ public PactDslRequestWithoutPath body(DslPart body) {
* @param body XML Document
*/
public PactDslRequestWithoutPath body(Document body) throws TransformerException {
if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_XML.toString()));
requestBody = OptionalBody.body(xmlToString(body).getBytes());
} else {
String contentType = requestHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
requestBody = OptionalBody.body(xmlToString(body).getBytes(charset),
Expand All @@ -274,11 +274,11 @@ public PactDslRequestWithoutPath body(PactXmlBuilder xmlBuilder) {
requestMatchers.addCategory(xmlBuilder.getMatchingRules());
requestGenerators.addGenerators(xmlBuilder.getGenerators());

if (!requestHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
requestHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_XML.toString()));
requestBody = OptionalBody.body(xmlBuilder.asBytes());
} else {
String contentType = requestHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
requestBody = OptionalBody.body(xmlBuilder.asBytes(charset),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package au.com.dius.pact.consumer.dsl;

import au.com.dius.pact.consumer.ConsumerPactBuilder;
import au.com.dius.pact.consumer.MessagePactBuilder;
import au.com.dius.pact.consumer.xml.PactXmlBuilder;
import au.com.dius.pact.core.model.OptionalBody;
import au.com.dius.pact.core.model.ProviderState;
Expand Down Expand Up @@ -207,11 +206,11 @@ public PactDslResponse bodyWithSingleQuotes(String body, ContentType contentType
* @param body Response body in JSON form
*/
public PactDslResponse body(JSONObject body) {
if (!responseHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
matchHeader(CONTENT_TYPE, DEFAULT_JSON_CONTENT_TYPE_REGEX, ContentType.APPLICATION_JSON.toString());
this.responseBody = OptionalBody.body(body.toString().getBytes());
} else {
String contentType = responseHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
this.responseBody = OptionalBody.body(body.toString().getBytes(charset),
Expand All @@ -237,10 +236,10 @@ public PactDslResponse body(DslPart body) {

Charset charset = Charset.defaultCharset();
String contentType = ContentType.APPLICATION_JSON.toString();
if (!responseHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
matchHeader(CONTENT_TYPE, DEFAULT_JSON_CONTENT_TYPE_REGEX, contentType);
} else {
contentType = responseHeaders.get(CONTENT_TYPE).get(0);
contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
}
Expand All @@ -255,17 +254,17 @@ public PactDslResponse body(DslPart body) {
return this;
}

/**
/**
* Response body to return
*
* @param body Response body as an XML Document
*/
public PactDslResponse body(Document body) throws TransformerException {
if (!responseHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_XML.toString()));
responseBody = OptionalBody.body(ConsumerPactBuilder.xmlToString(body).getBytes());
} else {
String contentType = responseHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
responseBody = OptionalBody.body(ConsumerPactBuilder.xmlToString(body).getBytes(charset),
Expand Down Expand Up @@ -391,11 +390,11 @@ public PactDslResponse body(PactXmlBuilder xmlBuilder) {
responseMatchers.addCategory(xmlBuilder.getMatchingRules());
responseGenerators.addGenerators(xmlBuilder.getGenerators());

if (!responseHeaders.containsKey(CONTENT_TYPE)) {
if (isContentTypeHeaderNotSet()) {
responseHeaders.put(CONTENT_TYPE, Collections.singletonList(ContentType.APPLICATION_XML.toString()));
responseBody = OptionalBody.body(xmlBuilder.asBytes());
} else {
String contentType = responseHeaders.get(CONTENT_TYPE).get(0);
String contentType = getContentTypeHeader();
ContentType ct = ContentType.parse(contentType);
Charset charset = ct.getCharset() != null ? ct.getCharset() : Charset.defaultCharset();
responseBody = OptionalBody.body(xmlBuilder.asBytes(charset),
Expand All @@ -404,4 +403,14 @@ public PactDslResponse body(PactXmlBuilder xmlBuilder) {

return this;
}

protected boolean isContentTypeHeaderNotSet() {
return responseHeaders.keySet().stream().noneMatch(key -> key.equalsIgnoreCase(CONTENT_TYPE));
}

protected String getContentTypeHeader() {
return responseHeaders.entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase(CONTENT_TYPE))
.findFirst()
.map(entry -> entry.getValue().get(0)).orElse("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,20 @@ class PactDslRequestWithPathSpec extends Specification {
]
}
@Issue('#1121')
def 'content type header is case sensitive'() {
given:
ConsumerPactBuilder consumerPactBuilder = ConsumerPactBuilder.consumer('spec')
when:
PactDslRequestWithPath request = new PactDslRequestWithPath(consumerPactBuilder,
'test', 'test', [], 'test', '/', 'GET', [:], [:], OptionalBody.missing(), new MatchingRulesImpl(),
new Generators(), null, null)
.headers('content-type', 'text/plain')
.body(new PactDslJsonBody())
then:
request.requestHeaders == ['content-type': ['text/plain']]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package au.com.dius.pact.consumer.dsl

import au.com.dius.pact.consumer.ConsumerPactBuilder
import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.generators.Generators
import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl
import spock.lang.Issue
import spock.lang.Specification

class PactDslRequestWithoutPathSpec extends Specification {
Expand All @@ -28,4 +31,20 @@ class PactDslRequestWithoutPathSpec extends Specification {
subject.requestBody == OptionalBody.body('{"test":true}'.bytes)
}

@Issue('#1121')
def 'content type header is case sensitive'() {
given:
ConsumerPactBuilder consumerPactBuilder = ConsumerPactBuilder.consumer('spec')
PactDslWithState pactDslWithState = new PactDslWithState(consumerPactBuilder, 'spec', 'spec', null, null)

when:
PactDslRequestWithoutPath request = new PactDslRequestWithoutPath(consumerPactBuilder,
pactDslWithState, 'test', null, null)
.headers('content-type', 'text/plain')
.body(new PactDslJsonBody())

then:
request.requestHeaders == ['content-type': ['text/plain']]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,20 @@ class PactDslResponseSpec extends Specification {
pact.interactions*.request.path == ['/response/1', '/response/1']
}

@Issue('#1121')
def 'content type header is case sensitive'() {
given:
def builder = ConsumerPactBuilder.consumer('spec').hasPactWith('provider')

when:
def response = builder.uponReceiving('a request for response No 1')
.path('/')
.willRespondWith()
.headers(['content-type': 'text/plain'])
.body(new PactDslJsonBody())

then:
response.responseHeaders == ['content-type': ['text/plain']]
}

}

0 comments on commit 0fa689f

Please sign in to comment.