Skip to content

Commit ab61bac

Browse files
author
Ronald Holshausen
committedFeb 22, 2020
fix: Pact server fails for requests that dont have Content-Type header #1008
1 parent 5d14f17 commit ab61bac

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed
 

‎core/model/src/main/kotlin/au/com/dius/pact/core/model/ContentType.kt

+4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ data class ContentType(val contentType: String?) {
3030
fun asMimeType() = parsedContentType?.mimeType ?: contentType
3131

3232
companion object : KLogging() {
33+
@JvmStatic
3334
val UNKNOWN = ContentType("")
35+
@JvmStatic
3436
val TEXT_PLAIN = ContentType("text/plain")
37+
@JvmStatic
3538
val HTML = ContentType("text/html")
39+
@JvmStatic
3640
val JSON = ContentType("application/json")
3741
}
3842
}

‎pact-jvm-server/src/main/scala/au/com/dius/pact/server/Conversions.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ object Conversions extends StrictLogging {
4040
def toPath(uri: String) = new URI(uri).getPath
4141

4242
def toBody(request: HttpRequest[ReceivedMessage], charset: String = "UTF-8") = {
43-
val is = if (request.headers(ContentEncoding.GZip.name).contains("gzip")) {
43+
val gzip = request.headers(ContentEncoding.GZip.name)
44+
val is = if (gzip.hasNext && gzip.next().contains("gzip")) {
4445
new GZIPInputStream(request.inputStream)
4546
} else {
4647
request.inputStream
@@ -50,7 +51,9 @@ object Conversions extends StrictLogging {
5051

5152
def unfilteredRequestToPactRequest(request: HttpRequest[ReceivedMessage]): Request = {
5253
val headers = toHeaders(request)
53-
val contentType = new ContentType(request.headers("Content-Type").next())
54+
val contentTypeHeader = request.headers("Content-Type")
55+
val contentType = if (contentTypeHeader.hasNext) new ContentType(contentTypeHeader.next())
56+
else ContentType.getTEXT_PLAIN
5457
new Request(request.method, toPath(request.uri), toQuery(request), headers,
5558
OptionalBody.body(toBody(request).getBytes(contentType.asCharset), contentType))
5659
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package au.com.dius.pact.server
2+
3+
import au.com.dius.pact.core.model.ContentType
4+
import au.com.dius.pact.core.model.Request
5+
import scala.collection.JavaConverters
6+
import spock.lang.Issue
7+
import spock.lang.Specification
8+
import unfiltered.netty.ReceivedMessage
9+
import unfiltered.request.HttpRequest
10+
11+
class ConversionsSpec extends Specification {
12+
13+
@Issue('#1008')
14+
def 'unfilteredRequestToPactRequest - handles the case where there is no content type header'() {
15+
given:
16+
def httpRequest = Mock(HttpRequest<ReceivedMessage>) {
17+
headers(_) >> JavaConverters.asScalaIterator([].iterator())
18+
headerNames() >> JavaConverters.asScalaIterator([].iterator())
19+
uri() >> '/'
20+
parameterNames() >> JavaConverters.asScalaIterator([].iterator())
21+
method() >> 'GET'
22+
inputStream() >> new ByteArrayInputStream('BOOH!'.bytes)
23+
}
24+
25+
when:
26+
Request request = Conversions$.MODULE$.unfilteredRequestToPactRequest(httpRequest)
27+
28+
then:
29+
request.body.contentType == ContentType.TEXT_PLAIN
30+
}
31+
32+
}

0 commit comments

Comments
 (0)
Please sign in to comment.