Skip to content

Commit f4eef13

Browse files
author
Ben Vercammen
committedFeb 3, 2020
The request body should be encoded according to the request header (when provided)
1 parent 3860faa commit f4eef13

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed
 

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import java.lang.Boolean.getBoolean
3939
import java.net.URI
4040
import java.net.URL
4141
import java.net.URLDecoder
42+
import java.nio.charset.UnsupportedCharsetException
4243
import java.util.concurrent.Callable
4344
import java.util.function.Consumer
4445
import java.util.function.Function
@@ -270,7 +271,17 @@ open class ProviderClient(
270271

271272
open fun setupBody(request: Request, method: HttpRequest) {
272273
if (method is HttpEntityEnclosingRequest && request.body.isPresent()) {
273-
method.entity = StringEntity(request.body.valueAsString())
274+
val contentTypeHeader = request.contentTypeHeader()
275+
if (null != contentTypeHeader) {
276+
try {
277+
val contentType = ContentType.parse(contentTypeHeader)
278+
method.entity = StringEntity(request.body.valueAsString(), contentType)
279+
} catch (e: UnsupportedCharsetException) {
280+
method.entity = StringEntity(request.body.valueAsString())
281+
}
282+
} else {
283+
method.entity = StringEntity(request.body.valueAsString())
284+
}
274285
}
275286
}
276287

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

+37-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.apache.http.client.methods.CloseableHttpResponse
1212
import org.apache.http.client.methods.HttpUriRequest
1313
import org.apache.http.entity.ContentType
1414
import org.apache.http.impl.client.CloseableHttpClient
15+
import org.apache.http.message.BasicHttpEntityEnclosingRequest
1516
import org.junit.Before
1617
import org.junit.Test
1718
import org.mockito.invocation.InvocationOnMock
@@ -42,7 +43,7 @@ class ProviderClientTest {
4243
httpClientFactory = [newClient: { provider -> mockHttpClient } ] as IHttpClientFactory
4344
client = new ProviderClient(provider, httpClientFactory)
4445
when(mockHttpClient.execute(any())).thenAnswer { InvocationOnMock invocation ->
45-
args = invocation.arguments.first()
46+
args = invocation.arguments.first() as HttpUriRequest
4647
[
4748
getStatusLine: { [getStatusCode: { 200 } ] as StatusLine },
4849
getAllHeaders: { [] as Header[] },
@@ -69,4 +70,39 @@ class ProviderClientTest {
6970
assert args.entity.content.text == 'A=B'
7071
}
7172

73+
@Test
74+
void 'setupBody() needs to take Content-Type headegr into account (UTF-8)'() {
75+
def contentType = 'text/plain; charset=UTF-8'
76+
def headers = ['Content-Type': [contentType]]
77+
def body = 'ÄÉÌÕÛ'
78+
def request = new Request('PUT', '/', [:], headers, OptionalBody.body(body.bytes))
79+
def method = new BasicHttpEntityEnclosingRequest('PUT', '/')
80+
client.setupBody(request, method)
81+
assert method.entity.contentType.value == contentType
82+
assert method.entity.content.getText('UTF-8') == body
83+
}
84+
85+
@Test
86+
void 'setupBody() needs to take Content-Type header into account (ISO-8859-1)'() {
87+
def contentType = 'text/plain; charset=ISO-8859-1'
88+
def headers = ['Content-Type': [contentType]]
89+
def body = 'ÄÉÌÕÛ'
90+
def request = new Request('PUT', '/', [:], headers, OptionalBody.body(body.bytes))
91+
def method = new BasicHttpEntityEnclosingRequest('PUT', '/')
92+
client.setupBody(request, method)
93+
assert method.entity.contentType.value == contentType
94+
assert method.entity.content.getText('ISO-8859-1') == body
95+
}
96+
97+
@Test
98+
void 'setupBody() Content-Type defaults to plain text without encoding'() {
99+
def contentType = 'text/plain'
100+
def body = 'ÄÉÌÕÛ'
101+
def request = new Request('PUT', '/', [:], [:], OptionalBody.body(body.bytes))
102+
def method = new BasicHttpEntityEnclosingRequest('PUT', '/')
103+
client.setupBody(request, method)
104+
assert method.entity.contentType.value == contentType
105+
assert method.entity.content.getText('ISO-8859-1') == body
106+
}
107+
72108
}

0 commit comments

Comments
 (0)
Please sign in to comment.