-
-
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.
refactor: move content detection to OptionalBody and ContentType classes
- Loading branch information
Ronald Holshausen
committed
Jun 13, 2020
1 parent
5bf94d7
commit b8ee6cd
Showing
28 changed files
with
299 additions
and
170 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
57 changes: 57 additions & 0 deletions
57
consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/PostImageBodyTest.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,57 @@ | ||
package au.com.dius.pact.consumer.junit5 | ||
|
||
import au.com.dius.pact.consumer.MockServer | ||
import au.com.dius.pact.consumer.dsl.PactDslJsonBody | ||
import au.com.dius.pact.consumer.dsl.PactDslWithProvider | ||
import au.com.dius.pact.core.model.RequestResponsePact | ||
import au.com.dius.pact.core.model.annotations.Pact | ||
import org.apache.http.client.methods.RequestBuilder | ||
import org.apache.http.entity.ContentType | ||
import org.apache.http.entity.mime.HttpMultipartMode | ||
import org.apache.http.entity.mime.MultipartEntityBuilder | ||
import org.apache.http.impl.client.CloseableHttpClient | ||
import org.apache.http.impl.client.HttpClients | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
|
||
@ExtendWith(PactConsumerTestExt) | ||
@PactTestFor(providerName = 'ProviderThatAcceptsImages') | ||
class PostImageBodyTest { | ||
@Pact(consumer = 'Consumer') | ||
RequestResponsePact pact(PactDslWithProvider builder) { | ||
PostImageBodyTest.getResourceAsStream('/ron.jpg').withCloseable { stream -> | ||
builder | ||
.uponReceiving('a request with an image') | ||
.method('POST') | ||
.path('/images') | ||
.withFileUpload('photo', 'ron.jpg', 'image/jpeg', stream.bytes) | ||
.willRespondWith() | ||
.status(200) | ||
.body(new PactDslJsonBody() | ||
.integerType('version', 1) | ||
.integerType('status', 0) | ||
.stringValue('errorMessage', '') | ||
.array('issues').closeArray()) | ||
.toPact() | ||
} | ||
} | ||
|
||
@Test | ||
void testFiles(MockServer mockServer) { | ||
CloseableHttpClient httpclient = HttpClients.createDefault() | ||
def result = httpclient.withCloseable { | ||
PostImageBodyTest.getResourceAsStream('/RAT.JPG').withCloseable { stream -> | ||
def data = MultipartEntityBuilder.create() | ||
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE) | ||
.addBinaryBody('photo', stream, ContentType.create('image/jpeg'), 'ron.jpg') | ||
.build() | ||
def request = RequestBuilder | ||
.post(mockServer.url + '/images') | ||
.setEntity(data) | ||
.build() | ||
httpclient.execute(request) | ||
} | ||
} | ||
assert result.statusLine.statusCode == 200 | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
67 changes: 48 additions & 19 deletions
67
core/model/src/main/kotlin/au/com/dius/pact/core/model/ContentType.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 |
---|---|---|
@@ -1,43 +1,72 @@ | ||
package au.com.dius.pact.core.model | ||
|
||
import au.com.dius.pact.core.support.isNotEmpty | ||
import mu.KLogging | ||
import org.apache.tika.mime.MediaType | ||
import java.nio.charset.Charset | ||
|
||
private val jsonRegex = Regex("application\\/.*json") | ||
private val xmlRegex = Regex("application\\/.*xml") | ||
private val jsonRegex = Regex(".*json") | ||
private val xmlRegex = Regex(".*xml") | ||
|
||
data class ContentType(val contentType: String?) { | ||
data class ContentType(val contentType: MediaType?) { | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
private val parsedContentType: org.apache.http.entity.ContentType? = try { | ||
if (contentType.isNullOrEmpty()) { | ||
null | ||
} else { | ||
org.apache.http.entity.ContentType.parse(contentType) | ||
} | ||
} catch (e: Exception) { | ||
logger.debug { "Failed to parse content type '$contentType'" } | ||
null | ||
} | ||
constructor(contentType: String) : this(MediaType.parse(contentType)) | ||
|
||
fun isJson(): Boolean = if (contentType != null) jsonRegex.matches(contentType.subtype.toLowerCase()) else false | ||
|
||
fun isJson(): Boolean = if (contentType != null) jsonRegex.matches(contentType.toLowerCase()) else false | ||
fun isXml(): Boolean = if (contentType != null) xmlRegex.matches(contentType.subtype.toLowerCase()) else false | ||
|
||
fun isXml(): Boolean = if (contentType != null) xmlRegex.matches(contentType.toLowerCase()) else false | ||
fun isOctetStream(): Boolean = if (contentType != null) | ||
contentType.baseType.toString() == "application/octet-stream" | ||
else false | ||
|
||
override fun toString() = contentType.toString() | ||
|
||
fun asCharset(): Charset = parsedContentType?.charset ?: Charset.defaultCharset() | ||
fun asString() = contentType?.toString() | ||
|
||
fun asCharset(): Charset { | ||
return if (contentType != null && contentType.hasParameters()) { | ||
val cs = contentType.parameters["charset"] | ||
if (cs.isNotEmpty()) { | ||
Charset.forName(cs) | ||
} else { | ||
Charset.defaultCharset() | ||
} | ||
} else { | ||
Charset.defaultCharset() | ||
} | ||
} | ||
|
||
fun or(other: ContentType) = if (contentType == null) { | ||
other | ||
} else { | ||
this | ||
} | ||
|
||
fun asMimeType() = parsedContentType?.mimeType ?: contentType | ||
fun getBaseType() = contentType?.baseType?.toString() | ||
|
||
companion object : KLogging() { | ||
@JvmStatic | ||
val UNKNOWN = ContentType("") | ||
fun fromString(contentType: String?) = if (contentType.isNullOrEmpty()) { | ||
UNKNOWN | ||
} else { | ||
ContentType(contentType) | ||
} | ||
|
||
val XMLREGEXP = """^\s*<\?xml\s*version.*""".toRegex() | ||
val HTMLREGEXP = """^\s*(<!DOCTYPE)|(<HTML>).*""".toRegex() | ||
val JSONREGEXP = """^\s*(true|false|null|[0-9]+|"\w*|\{\s*(}|"\w+)|\[\s*).*""".toRegex() | ||
val XMLREGEXP2 = """^\s*<\w+\s*(:\w+=[\"”][^\"”]+[\"”])?.*""".toRegex() | ||
|
||
@JvmStatic | ||
val UNKNOWN = ContentType(null) | ||
@JvmStatic | ||
val TEXT_PLAIN = ContentType("text/plain") | ||
@JvmStatic | ||
val HTML = ContentType("text/html") | ||
@JvmStatic | ||
val JSON = ContentType("application/json") | ||
@JvmStatic | ||
val XML = ContentType("application/xml") | ||
} | ||
} |
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
Oops, something went wrong.