-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: header values need to be parsed when loaded from older spec pact…
… files #1398
- Loading branch information
Ronald Holshausen
committed
Jul 24, 2021
1 parent
8e3aa2d
commit a11b05c
Showing
11 changed files
with
106 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
core/model/src/main/kotlin/au/com/dius/pact/core/model/HeaderParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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.parseHeaderValue | ||
|
||
object HeaderParser { | ||
private val SINGLE_VALUE_HEADERS = setOf("date", "accept-datetime", "if-modified-since", "if-unmodified-since", | ||
"expires", "retry-after") | ||
|
||
fun fromJson(key: String, value: JsonValue): List<String> { | ||
return when { | ||
value is JsonValue.Array -> value.values.map { Json.toString(it).trim() } | ||
SINGLE_VALUE_HEADERS.contains(key.toLowerCase()) -> listOf(Json.toString(value).trim()) | ||
else -> { | ||
val sval = Json.toString(value).trim() | ||
parseHeaderValue(sval).map { hvToString(it) } | ||
} | ||
} | ||
} | ||
|
||
private fun hvToString(headerValue: HeaderValue): String { | ||
return if (headerValue.params.isEmpty()) { | ||
headerValue.value.trim() | ||
} else { | ||
headerValue.value.trim() + ";" + headerValue.params.joinToString(";") { it.name + "=" + it.value } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core/model/src/test/groovy/au/com/dius/pact/core/model/HeaderParserSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package au.com.dius.pact.core.model | ||
|
||
import au.com.dius.pact.core.support.json.JsonValue | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
@SuppressWarnings('LineLength') | ||
class HeaderParserSpec extends Specification { | ||
|
||
private static final String 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' | ||
|
||
@Unroll | ||
def 'loading string headers from JSON - #desc'() { | ||
expect: | ||
HeaderParser.INSTANCE.fromJson(key, new JsonValue.StringValue(value)) == result | ||
|
||
where: | ||
|
||
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 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'] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters