Skip to content

Commit 7e6b8c0

Browse files
author
Ronald Holshausen
committedJan 22, 2020
fix: handle headers with comma-seprated values #997
1 parent 6eee482 commit 7e6b8c0

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed
 

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,20 @@ open class ProviderClient(
269269
}
270270

271271
open fun setupBody(request: Request, method: HttpRequest) {
272-
if (method is HttpEntityEnclosingRequest && request.body != null && request.body!!.isPresent()) {
272+
if (method is HttpEntityEnclosingRequest && request.body.isPresent()) {
273273
method.entity = StringEntity(request.body.valueAsString())
274274
}
275275
}
276276

277277
open fun setupHeaders(request: Request, method: HttpRequest) {
278278
val headers = request.headers
279-
if (headers != null && headers.isNotEmpty()) {
280-
headers.forEach { key, value ->
279+
if (headers.isNotEmpty()) {
280+
headers.forEach { (key, value) ->
281281
method.addHeader(key, value.joinToString(", "))
282282
}
283283
}
284284

285-
if (!method.containsHeader(CONTENT_TYPE) && request.body?.isPresent() == true) {
285+
if (!method.containsHeader(CONTENT_TYPE) && request.body.isPresent()) {
286286
method.addHeader(CONTENT_TYPE, "application/json")
287287
}
288288
}
@@ -354,7 +354,9 @@ open class ProviderClient(
354354
val response = mutableMapOf<String, Any>("statusCode" to httpResponse.statusLine.statusCode,
355355
"contentType" to ContentType.TEXT_PLAIN)
356356

357-
response["headers"] = httpResponse.allHeaders.groupBy({ header -> header.name }, { header -> header.value })
357+
response["headers"] = httpResponse.allHeaders
358+
.groupBy({ header -> header.name }, { header -> header.value.split(',').map { it.trim() } })
359+
.mapValues { it.value.flatten() }
358360

359361
val entity = httpResponse.entity
360362
if (entity != null) {

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

+24
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,28 @@ class ProviderClientSpec extends Specification {
602602
]
603603
}
604604

605+
def 'handles headers with comma-seperated values'() {
606+
given:
607+
StatusLine statusLine = new BasicStatusLine(new ProtocolVersion('http', 1, 1), 200, 'OK')
608+
Header[] headers = [
609+
new BasicHeader('Server', 'Apigee-Edge'),
610+
new BasicHeader('Access-Control-Expose-Headers', 'content-length,content-type'),
611+
new BasicHeader('Access-Control-Expose-Headers', 'accept')
612+
] as Header[]
613+
HttpResponse response = Mock(HttpResponse) {
614+
getStatusLine() >> statusLine
615+
getAllHeaders() >> headers
616+
}
617+
618+
when:
619+
def result = client.handleResponse(response)
620+
621+
then:
622+
result.statusCode == 200
623+
result.headers == [
624+
Server: ['Apigee-Edge'],
625+
'Access-Control-Expose-Headers': ['content-length', 'content-type', 'accept']
626+
]
627+
}
628+
605629
}

0 commit comments

Comments
 (0)
Please sign in to comment.