Skip to content

Commit

Permalink
fix: Pact parser is removing quoting on Content-Type params #1538
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Jul 28, 2023
1 parent be48dfe commit 52cb552
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ package au.com.dius.pact.core.model
import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.json.JsonValue
import io.ktor.http.HeaderValue
import io.ktor.http.HeaderValueParam
import io.ktor.http.HeaderValueWithParameters
import io.ktor.http.parseHeaderValue

class HeaderWithParameters(
content: String,
parameters: List<HeaderValueParam>
) : HeaderValueWithParameters(content, parameters)

object HeaderParser {
private val SINGLE_VALUE_HEADERS = setOf("date", "accept-datetime", "if-modified-since", "if-unmodified-since",
"expires", "retry-after")
Expand All @@ -24,7 +31,8 @@ object HeaderParser {
return if (headerValue.params.isEmpty()) {
headerValue.value.trim()
} else {
headerValue.value.trim() + ";" + headerValue.params.joinToString(";") { it.name + "=" + it.value }
val h = HeaderWithParameters(headerValue.value, headerValue.params)
h.toString()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.com.dius.pact.core.model

import au.com.dius.pact.core.support.json.JsonValue
import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll

Expand All @@ -20,8 +21,17 @@ class HeaderParserSpec extends Specification {
desc | key | value | result
'simple header' | 'HeaderA' | 'A' | ['A']
'date header' | 'date' | 'Sat, 24 Jul 2021 04:16:53 GMT' | ['Sat, 24 Jul 2021 04:16:53 GMT']
'header with parameter' | 'content-type' | 'text/html; charset=utf-8' | ['text/html;charset=utf-8']
'header with parameter' | 'content-type' | 'text/html; charset=utf-8' | ['text/html; charset=utf-8']
'header with multiple values' | 'access-control-allow-methods' | 'POST, GET, PUT, HEAD, DELETE, OPTIONS, PATCH' | ['POST', 'GET', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'PATCH']
'header with multiple values with parameters' | 'Accept' | ACCEPT | ['application/prs.hal-forms+json;q=1.0', 'application/hal+json;q=0.9', 'application/vnd.api+json;q=0.8', 'application/vnd.siren+json;q=0.8', 'application/vnd.collection+json;q=0.8', 'application/json;q=0.7', 'text/html;q=0.6', 'application/vnd.pactbrokerextended.v1+json;q=1.0']
'header with multiple values with parameters' | 'Accept' | ACCEPT | ['application/prs.hal-forms+json; q=1.0', 'application/hal+json; q=0.9', 'application/vnd.api+json; q=0.8', 'application/vnd.siren+json; q=0.8', 'application/vnd.collection+json; q=0.8', 'application/json; q=0.7', 'text/html; q=0.6', 'application/vnd.pactbrokerextended.v1+json; q=1.0']
'header with quoted values' | 'Content-Type' | 'multipart/related; type="application/json"; boundary=myBoundary' | ['multipart/related; type="application/json"; boundary=myBoundary']
}

@Issue('#1538')
def 'support quoted values as per RFC 1341'() {
expect:
HeaderParser.INSTANCE.fromJson('Accept', new JsonValue.StringValue(
'application/hal+json;profile="https://api.example.de/examples+v1"')) ==
['application/hal+json; profile="https://api.example.de/examples+v1"']
}
}

0 comments on commit 52cb552

Please sign in to comment.