Hello, I have a small problem using pact testing a...
# pact-jvm
m
Hello, I have a small problem using pact testing and the junit5 integration, from gradle, testCompile group: 'au.com.dius.pact.provider', name: 'junit5', version: '4.2.0'. I have ane endpoint which I'm testing, which returns a content type of application/x-json-stream, the contents of which are like: { "id": 1, "foo": "bar" } { "id": 2, "foo": "baz" } When running context.verifyInteraction(), the JsonParser is throwing an exception, saying that it can't parse the above, because it isn't valid JSON. Which it isn't. If I understand correctly, this comes down to the following code in Ma tchingConfig.kt: val bodyMatchers = mapOf( "application/.*xml" to "au.com.dius.pact.core.matchers.XmlBodyMatcher", "text/xml" to "au.com.dius.pact.core.matchers.XmlBodyMatcher", ".*json.*" to "au.com.dius.pact.core.matchers.JsonBodyMatcher", "text/plain" to "au.com.dius.pact.core.matchers.PlainTextBodyMatcher", "multipart/form-data" to "au.com.dius.pact.core.matchers.MultipartMessageBodyMatcher", "multipart/mixed" to "au.com.dius.pact.core.matchers.MultipartMessageBodyMatcher", "application/x-www-form-urlencoded" to "au.com.dius.pact.core.matchers.FormPostBodyMatcher" ) @JvmStatic fun lookupBodyMatcher(contentType: String?): BodyMatcher? { return if (contentType != null) { val matcher = bodyMatchers.entries.find { contentType.matches(Regex(it.key)) }?.value if (matcher != null) { val clazz = Class.forName(matcher).kotlin (clazz.objectInstance ?: clazz.createInstance()) as BodyMatcher? } else { val override = System.getProperty("pact.content_type.override.$contentType") The call to lookupBodyMatcher has contentType == application/x-json-stream, so it is matching the regexp ".*json.*" and trying to parse the expected content as JSON. And the JSON isn't valid. It never will be. It doesn't look like there is any way of overriding this either. My question is this: what is the best approach for this situation? I currently use reflection to insert a new entry in the bodyMatchers map which handles the x-json-stream body matcher correctly. This obviously is quite fragile, but I can't see a way around it for the minute?
u
That is not one JSON document, it is multiple. By default, the JSON parser is expecting a single document
I don't think there is a dynamic way to add new content type handlers, as this has not been required for any before. Can you raise a GitHub issue to add that?