Adds support for binary payloads (#818)
with this change we're adding support for binary payloads both for HTTP and messaging. fixes #664
This commit is contained in:
committed by
GitHub
parent
928b32ecce
commit
0058b987ae
@@ -49,6 +49,10 @@ class Body extends DslProperty {
|
||||
this("${bodyAsValue}")
|
||||
}
|
||||
|
||||
Body(byte[] bodyAsValue) {
|
||||
super(bodyAsValue)
|
||||
}
|
||||
|
||||
Body(Number bodyAsValue) {
|
||||
super(bodyAsValue)
|
||||
}
|
||||
@@ -65,6 +69,10 @@ class Body extends DslProperty {
|
||||
super(bodyAsValue.clientValue, bodyAsValue.serverValue)
|
||||
}
|
||||
|
||||
Body(Serializable serializable) {
|
||||
super(serializable, serializable)
|
||||
}
|
||||
|
||||
Body(MatchingStrategy matchingStrategy) {
|
||||
super(matchingStrategy, matchingStrategy)
|
||||
}
|
||||
|
||||
@@ -16,10 +16,12 @@
|
||||
|
||||
package org.springframework.cloud.contract.spec.internal
|
||||
|
||||
import groovy.transform.PackageScope
|
||||
import java.nio.charset.Charset
|
||||
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
/**
|
||||
* Contains useful common methods for the DSL.
|
||||
*
|
||||
@@ -29,7 +31,6 @@ import java.util.regex.Pattern
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@TypeChecked
|
||||
@PackageScope
|
||||
class Common {
|
||||
|
||||
@Delegate private final RegexPatterns regexPatterns = new RegexPatterns()
|
||||
@@ -170,12 +171,49 @@ class Common {
|
||||
return new ServerDslProperty(serverValue)
|
||||
}
|
||||
|
||||
String file(String relativePath) {
|
||||
/**
|
||||
* Read file contents as String
|
||||
*
|
||||
* @param relativePath of the file to read
|
||||
* @return String file contents
|
||||
*/
|
||||
FromFileProperty file(String relativePath) {
|
||||
return file(relativePath, Charset.defaultCharset())
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file contents as bytes[]
|
||||
*
|
||||
* @param relativePath of the file to read
|
||||
* @return String file contents
|
||||
*/
|
||||
FromFileProperty fileAsBytes(String relativePath) {
|
||||
return new FromFileProperty(fileLocation(relativePath), byte[])
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file contents as String with the given Charset
|
||||
*
|
||||
* @param relativePath of the file to read
|
||||
* @param charset to use for converting the bytes to String
|
||||
* @return String file contents
|
||||
*/
|
||||
FromFileProperty file(String relativePath, Charset charset) {
|
||||
return new FromFileProperty(fileLocation(relativePath), String, charset)
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file contents as array of bytes
|
||||
*
|
||||
* @param relativePath of the file to read
|
||||
* @return file contents as an array of bytes
|
||||
*/
|
||||
private File fileLocation(String relativePath) {
|
||||
URL resource = Thread.currentThread().getContextClassLoader().getResource(relativePath)
|
||||
if (resource == null) {
|
||||
throw new IllegalStateException("File [${relativePath}] is not present")
|
||||
}
|
||||
return new File(resource.toURI()).text
|
||||
return new File(resource.toURI())
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,7 +25,7 @@ import groovy.transform.ToString
|
||||
*/
|
||||
@CompileStatic
|
||||
@ToString(includePackage = false, includeNames = true)
|
||||
class DslProperty<T> {
|
||||
class DslProperty<T> implements Serializable {
|
||||
|
||||
final T clientValue
|
||||
final T serverValue
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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
|
||||
*
|
||||
* http://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 groovy.transform.CompileStatic
|
||||
import groovy.transform.EqualsAndHashCode
|
||||
import groovy.transform.ToString
|
||||
|
||||
import java.nio.charset.Charset
|
||||
|
||||
/**
|
||||
* Represents a property that will become a File content
|
||||
*
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@CompileStatic
|
||||
@EqualsAndHashCode
|
||||
@ToString(includePackage = false, includeNames = true)
|
||||
class FromFileProperty implements Serializable {
|
||||
|
||||
final File file
|
||||
final Charset charset
|
||||
final Class type
|
||||
|
||||
FromFileProperty(File file, Class type) {
|
||||
this(file, type, Charset.defaultCharset())
|
||||
}
|
||||
|
||||
FromFileProperty(File file, Class type, Charset charset) {
|
||||
this.file = file
|
||||
this.type = type
|
||||
this.charset = charset
|
||||
}
|
||||
|
||||
boolean isString() {
|
||||
return this.type == String
|
||||
}
|
||||
|
||||
boolean isByte() {
|
||||
return this.type == byte[]
|
||||
}
|
||||
|
||||
String asString() {
|
||||
return new String(this.file.bytes, this.charset)
|
||||
}
|
||||
|
||||
String fileName() {
|
||||
return this.file.getName()
|
||||
}
|
||||
|
||||
byte[] asBytes() {
|
||||
return this.file.bytes
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,7 +55,7 @@ class MatchingStrategy extends DslProperty {
|
||||
|
||||
enum Type {
|
||||
EQUAL_TO("equalTo"), CONTAINS("containing"), MATCHING("matching"), NOT_MATCHING("notMatching"),
|
||||
EQUAL_TO_JSON("equalToJson"), EQUAL_TO_XML("equalToXml"), ABSENT("absent")
|
||||
EQUAL_TO_JSON("equalToJson"), EQUAL_TO_XML("equalToXml"), ABSENT("absent"), BINARY_EQUAL_TO("binaryEqualTo")
|
||||
|
||||
final String name
|
||||
|
||||
|
||||
Reference in New Issue
Block a user