Reworked DSL for multipart

This commit is contained in:
Tim Ysewyn
2019-08-11 01:10:35 +02:00
parent 9954efdf28
commit 4dc32e848c
7 changed files with 167 additions and 32 deletions

View File

@@ -23,18 +23,18 @@ contract {
request {
method = POST
url = url("/tests")
multipart = multipart(mapOf(
"file1" to named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer("content1"))),
"file2" to named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer("content1"))),
"test" to named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer(fileAsBytes("test.json"))),
value("application/json"))
))
multipart {
field("file1", named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer("content1"))))
field("file2", named(
value(consumer(regex(nonEmpty())), producer("filename2")),
value(consumer(regex(nonEmpty())), producer("content2"))))
field("test", named(
value(consumer(regex(nonEmpty())), producer("filename3")),
value(consumer(regex(nonEmpty())), producer(fileAsBytes("test.json"))),
value("application/json")))
}
headers {
contentType = "multipart/form-data"
}

View File

@@ -23,18 +23,18 @@ contract {
request {
method = POST
url = url("/tests")
multipart = multipart(mapOf(
"file1" to named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer("content1"))),
"file2" to named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer("content2"))),
"test" to named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer(file("test.json"))),
value("application/json"))
))
multipart {
field("file1", named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer("content1"))))
field("file2", named(
value(consumer(regex(nonEmpty())), producer("filename2")),
value(consumer(regex(nonEmpty())), producer("content2"))))
field("test", named(
value(consumer(regex(nonEmpty())), producer("filename3")),
value(consumer(regex(nonEmpty())), producer(file("test.json"))),
value("application/json")))
}
headers {
contentType = "multipart/form-data"
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.spec.internal
import org.springframework.cloud.contract.spec.toDslProperty
/**
* @author Tim Ysewyn
*/
class MultipartDsl {
private val fields = LinkedHashMap<String, DslProperty<Any>>()
fun field(name: String, content: Any) {
this.fields[name] = content.toDslProperty()
}
internal fun get(): Multipart {
return Multipart(fields)
}
}

View File

@@ -68,16 +68,12 @@ open class RequestDsl : CommonDsl(), RegexCreatingProperty<ClientDslProperty> {
fun body(body: Any) = Body(body)
fun multipart(multipart: Map<String, Any>) = Multipart(multipart.toDslProperties())
fun multipart(multipart: List<Any>) = Multipart(multipart.toDslProperties())
fun multipart(multipart: DslProperty<Any>) = Multipart(multipart)
fun multipart(multipart: Any) = Multipart(multipart)
fun multipart(configurer: MultipartDsl.() -> Unit) {
this.multipart = MultipartDsl().apply(configurer).get()
}
fun bodyMatchers(configurer: BodyMatchersDsl.() -> Unit) {
bodyMatchers = BodyMatchersDsl().apply(configurer).get()
this.bodyMatchers = BodyMatchersDsl().apply(configurer).get()
}
/* HELPER VARIABLES */

View File

@@ -16,13 +16,20 @@
package org.springframework.cloud.contract.spec
import java.io.File
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
import org.springframework.cloud.contract.spec.internal.Cookie
import org.springframework.cloud.contract.spec.internal.DslProperty
import org.springframework.cloud.contract.spec.internal.FromFileProperty
import org.springframework.cloud.contract.spec.internal.KotlinContractConverter
import org.springframework.cloud.contract.spec.internal.MatchingType
import org.springframework.cloud.contract.spec.internal.NamedProperty
import org.springframework.cloud.contract.spec.internal.RegexProperty
/**
@@ -708,4 +715,50 @@ then:
}
}
@Test
@Suppress("UNCHECKED_CAST")
fun `should support multipart`() {
val contract = KotlinContractConverter()
.convertFrom(File(javaClass.classLoader.getResource("contracts/multipart.kts")!!.toURI()))
.elementAt(0)
assertDoesNotThrow {
Contract.assertContract(contract)
}.also {
val request = contract.request
assertThat(request.multipart).isNotNull()
assertThat(request.multipart.clientValue).isNotNull()
assertThat(request.multipart.clientValue).isInstanceOf(LinkedHashMap::class.java)
val multipartData = request.multipart.clientValue as LinkedHashMap<String, Any>
assertThat(multipartData).hasSize(3)
assertThat(multipartData.keys).containsExactly("file1", "file2", "test")
var namedProperty: NamedProperty
assertThat(multipartData["file1"]).isInstanceOf(NamedProperty::class.java)
namedProperty = multipartData["file1"] as NamedProperty
assertThat(((namedProperty.name as DslProperty<Any>).clientValue as RegexProperty).pattern()).isEqualTo("[\\S\\s]+")
assertThat((namedProperty.name as DslProperty<Any>).serverValue as String).isEqualTo("filename1")
assertThat(((namedProperty.value as DslProperty<Any>).clientValue as RegexProperty).pattern()).isEqualTo("[\\S\\s]+")
assertThat((namedProperty.value as DslProperty<Any>).serverValue as String).isEqualTo("content1")
assertThat(namedProperty.contentType).isNull()
assertThat(multipartData["file2"]).isInstanceOf(NamedProperty::class.java)
namedProperty = multipartData["file2"] as NamedProperty
assertThat(((namedProperty.name as DslProperty<Any>).clientValue as RegexProperty).pattern()).isEqualTo("[\\S\\s]+")
assertThat((namedProperty.name as DslProperty<Any>).serverValue as String).isEqualTo("filename2")
assertThat(((namedProperty.value as DslProperty<Any>).clientValue as RegexProperty).pattern()).isEqualTo("[\\S\\s]+")
assertThat((namedProperty.value as DslProperty<Any>).serverValue as String).isEqualTo("content2")
assertThat(namedProperty.contentType).isNull()
assertThat(multipartData["test"]).isInstanceOf(NamedProperty::class.java)
namedProperty = multipartData["test"] as NamedProperty
assertThat(((namedProperty.name as DslProperty<Any>).clientValue as RegexProperty).pattern()).isEqualTo("[\\S\\s]+")
assertThat((namedProperty.name as DslProperty<Any>).serverValue as String).isEqualTo("filename3")
assertThat(((namedProperty.value as DslProperty<Any>).clientValue as RegexProperty).pattern()).isEqualTo("[\\S\\s]+")
assertThat(((namedProperty.value as DslProperty<Any>).serverValue as FromFileProperty).fileName()).isEqualTo("test.json")
assertThat((namedProperty.contentType as DslProperty<Any>).clientValue).isEqualTo("application/json")
assertThat((namedProperty.contentType as DslProperty<Any>).serverValue).isEqualTo("application/json")
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package contracts
import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
contract {
request {
method = POST
url = url("/tests")
multipart {
field("file1", named(
value(consumer(regex(nonEmpty())), producer("filename1")),
value(consumer(regex(nonEmpty())), producer("content1"))))
field("file2", named(
value(consumer(regex(nonEmpty())), producer("filename2")),
value(consumer(regex(nonEmpty())), producer("content2"))))
field("test", named(
value(consumer(regex(nonEmpty())), producer("filename3")),
value(consumer(regex(nonEmpty())), producer(file("test.json"))),
value("application/json")))
}
headers {
contentType = "multipart/form-data"
}
}
response {
status = OK
headers {
contentType = "application/json"
}
}
}

View File

@@ -0,0 +1,3 @@
{
"status": "test"
}