Merge branch '1.2.x'

This commit is contained in:
Marcin Grzejszczak
2018-04-11 12:48:22 +02:00
39 changed files with 1041 additions and 10 deletions

View File

@@ -64,6 +64,12 @@ interface ContractTemplate {
*/
String header(String key, int index)
/**
* Retruns the tempalte for retrieving the first value of a cookie with certain key
* @param key
*/
String cookie(String key)
/**
* Request body text (avoid for non-text bodies) e.g. {{{ request.body }}} . The body will not be escaped
* so you won't be able to directly embed it in a JSON for example.

View File

@@ -0,0 +1,49 @@
/*
* 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
/**
* Represents a http cookie
*
* @author Alex Xandra Albert Sim
* @since 1.2.5
*/
@EqualsAndHashCode(includeFields = true, callSuper = true)
@ToString(includePackage = false, includeFields = true, ignoreNulls = true, includeNames = true, includeSuper = true)
@CompileStatic
class Cookie extends DslProperty {
String key
Cookie(String key, DslProperty dslProperty) {
super(dslProperty.clientValue, dslProperty.serverValue)
this.key = key
}
Cookie(String key, MatchingStrategy value) {
super(value)
this.key = key
}
Cookie(String key, Object value) {
super(value)
this.key = key
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.EqualsAndHashCode
import groovy.transform.ToString
import groovy.transform.TypeChecked
/**
* Represents a set of http cookies
*
* @author Alex Xandra Albert Sim
* @since 1.2.5
*/
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false, includeFields = true, ignoreNulls = true, includeNames = true)
@TypeChecked
class Cookies {
Set<Cookie> entries = []
void cookie(Map<String, Object> singleCookie) {
Map.Entry<String, Object> first = singleCookie.entrySet().first()
entries << new Cookie(first?.key, first?.value)
}
void cookie(String cookieKey, Object cookieValue) {
entries << new Cookie(cookieKey, cookieValue)
}
void executeForEachCookie(Closure closure) {
entries?.each {
cookie -> closure(cookie)
}
}
DslProperty matching(String value) {
return new DslProperty(value)
}
boolean equals(o) {
if (this.is(o)) return true
if (getClass() != o.class) return false
Cookies cookies = (Cookies) o
if (cookies != cookies.entries) return false
return true
}
int hashCode() {
return entries.hashCode()
}
/**
* Converts the headers into their stub side representations and returns as
* a map of String key => Object value.
*/
Map<String , Object> asStubSideMap() {
def acc = [:].withDefault { [] as Collection<Object> }
return entries.inject(acc as Map<String, Object>) { Map<String, Object> map, Cookie cookie ->
map[cookie.key] = cookie.clientValue
return map
} as Map<String , Object>
}
/**
* Converts the headers into their stub side representations and returns as
* a map of String key => Object value.
*/
Map<String , Object> asTestSideMap() {
def acc = [:].withDefault { [] as Collection<Object> }
return entries.inject(acc as Map<String, Object>) { Map<String, Object> map, Cookie cookie ->
map[cookie.key] = cookie.serverValue
return map
} as Map<String , Object>
}
}

View File

@@ -77,6 +77,14 @@ class FromRequest {
return new DslProperty(template.header(key, index))
}
/**
* Retruns the tempalte for retrieving the first value of a cookie with certain key
* @param key
*/
DslProperty cookie(String key) {
return new DslProperty(template.cookie(key))
}
/**
* Request body text (avoid for non-text bodies)
*/

View File

@@ -58,6 +58,11 @@ class HandlebarsContractTemplate implements ContractTemplate {
return wrapped("request.headers.${key}.[${index}]")
}
@Override
String cookie(String key) {
return wrapped("request.cookies.${key}")
}
@Override
String body() {
return wrapped("request.body")

View File

@@ -43,6 +43,7 @@ class OutputMessage extends Common {
DslProperty<String> sentTo
Headers headers
Cookies cookies
DslProperty body
ExecutionProperty assertThat
ResponseBodyMatchers bodyMatchers
@@ -52,6 +53,7 @@ class OutputMessage extends Common {
OutputMessage(OutputMessage outputMessage) {
this.sentTo = outputMessage.sentTo
this.headers = outputMessage.headers
this.cookies = outputMessage.cookies
this.body = outputMessage.body
}
@@ -77,6 +79,12 @@ class OutputMessage extends Common {
closure()
}
void cookies(@DelegatesTo(Cookies) Closure closure) {
this.cookies = new Cookies()
closure.delegate = cookies
closure()
}
void assertThat(String assertThat) {
this.assertThat = new ExecutionProperty(assertThat)
}

View File

@@ -47,6 +47,7 @@ class Request extends Common {
Url url
UrlPath urlPath
Headers headers
Cookies cookies
Body body
Multipart multipart
BodyMatchers bodyMatchers
@@ -59,6 +60,7 @@ class Request extends Common {
this.url = request.url
this.urlPath = request.urlPath
this.headers = request.headers
this.cookies = request.cookies
this.body = request.body
this.multipart = request.multipart
}
@@ -121,6 +123,12 @@ class Request extends Common {
closure()
}
void cookies(@DelegatesTo(RequestCookies) Closure closure) {
this.cookies = new RequestCookies()
closure.delegate = cookies
closure()
}
void body(Map<String, Object> body) {
this.body = new Body(convertObjectsToDslProperties(body))
}
@@ -272,6 +280,18 @@ class Request extends Common {
}
}
@CompileStatic
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false)
private class RequestCookies extends Cookies {
@Override
DslProperty matching(String value) {
return $(c(regex("${RegexpUtils.escapeSpecialRegexWithSingleEscape(value)}.*")),
p(value))
}
}
@CompileStatic
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false)

View File

@@ -46,6 +46,7 @@ class Response extends Common {
DslProperty status
DslProperty delay
Headers headers
Cookies cookies
Body body
boolean async
ResponseBodyMatchers bodyMatchers
@@ -56,6 +57,7 @@ class Response extends Common {
Response(Response response) {
this.status = response.status
this.headers = response.headers
this.cookies = response.cookies
this.body = response.body
}
@@ -73,6 +75,12 @@ class Response extends Common {
closure()
}
void cookies(@DelegatesTo(ResponseCookies) Closure closure) {
this.cookies = new ResponseCookies()
closure.delegate = cookies
closure()
}
void body(Map<String, Object> body) {
this.body = new Body(convertObjectsToDslProperties(body))
}
@@ -184,6 +192,17 @@ class Response extends Common {
}
}
@CompileStatic
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false)
private class ResponseCookies extends Cookies {
@Override
DslProperty matching(String value) {
return $(p(regex("${RegexpUtils.escapeSpecialRegexWithSingleEscape(value)}.*")), c(value))
}
}
@CompileStatic
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false)

View File

@@ -0,0 +1,28 @@
package org.springframework.cloud.contract.spec.internal
import spock.lang.Specification
/**
* @author Marcin Grzejszczak
*/
class CookiesSpec extends Specification {
Cookies cookies = cookie()
def "should convert cookies to a stub side map"() {
expect:
cookies.asStubSideMap() == [foo: "client", bar: "client"]
}
def "should convert cookies to a test side map"() {
expect:
cookies.asTestSideMap() == [foo: "server", bar: "server"]
}
private Cookies cookie() {
Cookies cookies = new Cookies()
cookies.cookie("foo", new DslProperty("client", "server"))
cookies.cookie(["bar": new DslProperty<>("client", "server")])
return cookies
}
}