Skip to content

Commit 1b2de39

Browse files
author
Ronald Holshausen
committedFeb 22, 2020
fix: default to text/plain content type if no content type header is provided #1013
1 parent ab61bac commit 1b2de39

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed
 

‎provider/pact-jvm-provider/src/main/kotlin/au/com/dius/pact/provider/ProviderClient.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ open class ProviderClient(
294294
}
295295

296296
if (!method.containsHeader(CONTENT_TYPE) && request.body.isPresent()) {
297-
method.addHeader(CONTENT_TYPE, "application/json")
297+
method.addHeader(CONTENT_TYPE, "text/plain; charset=ISO-8859-1")
298298
}
299299
}
300300

@@ -374,7 +374,7 @@ open class ProviderClient(
374374
val contentType = if (entity.contentType != null) {
375375
ContentType.parse(entity.contentType.value)
376376
} else {
377-
ContentType.APPLICATION_JSON
377+
ContentType.TEXT_PLAIN
378378
}
379379
response["contentType"] = contentType
380380
response["data"] = EntityUtils.toString(entity, contentType.charset?.name() ?: UTF8)

‎provider/pact-jvm-provider/src/main/kotlin/au/com/dius/pact/provider/ResponseComparison.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ResponseComparison(
8888
var actualBodyString = ""
8989
if (actual.isNotEmpty()) {
9090
actualBodyString = if (mimeType.matches(Regex("application/.*json"))) {
91-
Json.gsonPretty.toJson(JsonParser().parse(actual))
91+
Json.gsonPretty.toJson(JsonParser.parseString(actual))
9292
} else {
9393
actual
9494
}
@@ -97,7 +97,7 @@ class ResponseComparison(
9797
var expectedBodyString = ""
9898
if (response.isNotEmpty()) {
9999
expectedBodyString = if (jsonBody) {
100-
Json.gsonPretty.toJson(JsonParser().parse(response))
100+
Json.gsonPretty.toJson(JsonParser.parseString(response))
101101
} else {
102102
response
103103
}

‎provider/pact-jvm-provider/src/test/groovy/au/com/dius/pact/provider/groovysupport/ProviderClientSpec.groovy

+25-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.apache.http.entity.StringEntity
2121
import org.apache.http.impl.client.CloseableHttpClient
2222
import org.apache.http.message.BasicHeader
2323
import org.apache.http.message.BasicStatusLine
24+
import spock.lang.Issue
2425
import spock.lang.Specification
2526
import spock.lang.Unroll
2627

@@ -83,7 +84,7 @@ class ProviderClientSpec extends Specification {
8384
0 * httpRequest._
8485
}
8586

86-
def 'setting up headers adds an JSON content type if none was provided and there is a body'() {
87+
def 'setting up headers adds an TEXT content type if none was provided and there is a body'() {
8788
given:
8889
def headers = [
8990
A: ['a'],
@@ -100,12 +101,12 @@ class ProviderClientSpec extends Specification {
100101
headers.each {
101102
1 * httpRequest.addHeader(it.key, it.value[0])
102103
}
103-
1 * httpRequest.addHeader('Content-Type', 'application/json')
104+
1 * httpRequest.addHeader('Content-Type', 'text/plain; charset=ISO-8859-1')
104105

105106
0 * httpRequest._
106107
}
107108

108-
def 'setting up headers does not add an JSON content type if there is no body'() {
109+
def 'setting up headers does not add an TEXT content type if there is no body'() {
109110
given:
110111
def headers = [
111112
A: ['a'],
@@ -122,12 +123,12 @@ class ProviderClientSpec extends Specification {
122123
headers.each {
123124
1 * httpRequest.addHeader(it.key, it.value[0])
124125
}
125-
0 * httpRequest.addHeader('Content-Type', 'application/json')
126+
0 * httpRequest.addHeader('Content-Type', 'text/plain')
126127

127128
0 * httpRequest._
128129
}
129130

130-
def 'setting up headers does not add an JSON content type if there is already one'() {
131+
def 'setting up headers does not add an TEXT content type if there is already one'() {
131132
given:
132133
def headers = [
133134
A: ['a'],
@@ -144,7 +145,7 @@ class ProviderClientSpec extends Specification {
144145
headers.each {
145146
1 * httpRequest.addHeader(it.key, it.value[0])
146147
}
147-
0 * httpRequest.addHeader('Content-Type', 'application/json')
148+
0 * httpRequest.addHeader('Content-Type', 'text/plain')
148149

149150
0 * httpRequest._
150151
}
@@ -626,4 +627,22 @@ class ProviderClientSpec extends Specification {
626627
]
627628
}
628629

630+
@Issue('#1013')
631+
def 'If no content type header is present, defaults to text/plain'() {
632+
given:
633+
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion('http', 1, 1), 200, 'OK')
634+
Header[] headers = [] as Header[]
635+
HttpResponse response = Mock(HttpResponse) {
636+
getStatusLine() >> statusLine
637+
getAllHeaders() >> headers
638+
getEntity() >> new StringEntity('HELLO', null as ContentType)
639+
}
640+
641+
when:
642+
def result = client.handleResponse(response)
643+
644+
then:
645+
result.contentType.toString() == 'text/plain; charset=ISO-8859-1'
646+
}
647+
629648
}

0 commit comments

Comments
 (0)
Please sign in to comment.