Skip to content

Commit

Permalink
pact-foundation#1013 - Fix to issue when in pact contract content typ…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
wieslawmlynarski committed Mar 12, 2020
1 parent 6fd8adb commit d1b192a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import java.util.concurrent.Callable
import java.util.function.Consumer
import java.util.function.Function
import java.util.function.Supplier

import au.com.dius.pact.core.model.ContentType as PactContentType
interface IHttpClientFactory {
fun newClient(provider: IProviderInfo): CloseableHttpClient
}
Expand Down Expand Up @@ -294,7 +294,11 @@ open class ProviderClient(
}

if (!method.containsHeader(CONTENT_TYPE) && request.body.isPresent()) {
method.addHeader(CONTENT_TYPE, "text/plain; charset=ISO-8859-1")
val contentType = when (request.body.contentType) {
PactContentType.UNKNOWN -> "text/plain; charset=ISO-8859-1"
else -> request.body.contentType.toString()
}
method.addHeader(CONTENT_TYPE, contentType)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import au.com.dius.pact.core.model.OptionalBody
import au.com.dius.pact.core.model.ProviderState
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.model.ContentType as PactContentType
@SuppressWarnings('UnusedImport')
import au.com.dius.pact.provider.GroovyScalaUtils$
import au.com.dius.pact.provider.IHttpClientFactory
Expand Down Expand Up @@ -106,6 +107,28 @@ class ProviderClientSpec extends Specification {
0 * httpRequest._
}

def 'setting up headers adds an content type if none was provided and there is a body with content type'() {
given:
def headers = [
A: ['a'],
B: ['b'],
C: ['c']
]
request = new Request('PUT', '/', [:], headers, OptionalBody.body('{}'.bytes, PactContentType.JSON))

when:
client.setupHeaders(request, httpRequest)

then:
1 * httpRequest.containsHeader('Content-Type') >> false
headers.each {
1 * httpRequest.addHeader(it.key, it.value[0])
}
1 * httpRequest.addHeader('Content-Type', ContentType.APPLICATION_JSON.getMimeType())

0 * httpRequest._
}

def 'setting up headers does not add an TEXT content type if there is no body'() {
given:
def headers = [
Expand Down

0 comments on commit d1b192a

Please sign in to comment.