Skip to content

Commit d1b192a

Browse files
committedMar 12, 2020
#1013 - Fix to issue when in pact contract content type is not provided
When the body is of type json use the application/json content type Breaking change was between pact 4.0.6 -> 4.0.7
1 parent 6fd8adb commit d1b192a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
 

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import java.util.concurrent.Callable
4444
import java.util.function.Consumer
4545
import java.util.function.Function
4646
import java.util.function.Supplier
47-
47+
import au.com.dius.pact.core.model.ContentType as PactContentType
4848
interface IHttpClientFactory {
4949
fun newClient(provider: IProviderInfo): CloseableHttpClient
5050
}
@@ -294,7 +294,11 @@ open class ProviderClient(
294294
}
295295

296296
if (!method.containsHeader(CONTENT_TYPE) && request.body.isPresent()) {
297-
method.addHeader(CONTENT_TYPE, "text/plain; charset=ISO-8859-1")
297+
val contentType = when (request.body.contentType) {
298+
PactContentType.UNKNOWN -> "text/plain; charset=ISO-8859-1"
299+
else -> request.body.contentType.toString()
300+
}
301+
method.addHeader(CONTENT_TYPE, contentType)
298302
}
299303
}
300304

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

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import au.com.dius.pact.core.model.OptionalBody
44
import au.com.dius.pact.core.model.ProviderState
55
import au.com.dius.pact.core.model.Request
66
import au.com.dius.pact.core.support.Json
7+
import au.com.dius.pact.core.model.ContentType as PactContentType
78
@SuppressWarnings('UnusedImport')
89
import au.com.dius.pact.provider.GroovyScalaUtils$
910
import au.com.dius.pact.provider.IHttpClientFactory
@@ -106,6 +107,28 @@ class ProviderClientSpec extends Specification {
106107
0 * httpRequest._
107108
}
108109

110+
def 'setting up headers adds an content type if none was provided and there is a body with content type'() {
111+
given:
112+
def headers = [
113+
A: ['a'],
114+
B: ['b'],
115+
C: ['c']
116+
]
117+
request = new Request('PUT', '/', [:], headers, OptionalBody.body('{}'.bytes, PactContentType.JSON))
118+
119+
when:
120+
client.setupHeaders(request, httpRequest)
121+
122+
then:
123+
1 * httpRequest.containsHeader('Content-Type') >> false
124+
headers.each {
125+
1 * httpRequest.addHeader(it.key, it.value[0])
126+
}
127+
1 * httpRequest.addHeader('Content-Type', ContentType.APPLICATION_JSON.getMimeType())
128+
129+
0 * httpRequest._
130+
}
131+
109132
def 'setting up headers does not add an TEXT content type if there is no body'() {
110133
given:
111134
def headers = [

0 commit comments

Comments
 (0)
Please sign in to comment.