Add cookie header check to contract verifier

This commit is contained in:
Alex Xandra Albert Sim
2018-04-09 15:45:08 +07:00
parent 7652bcff48
commit c740f8b3dd
20 changed files with 639 additions and 0 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.3.8
*/
@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,66 @@
/*
* 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.3.8
*/
@EqualsAndHashCode(includeFields = true)
@ToString(includePackage = false, includeFields = true, ignoreNulls = true, includeNames = true)
@TypeChecked
class Cookies {
Set<Cookie> cookies = []
void cookie(Map<String, Object> singleCookie) {
Map.Entry<String, Object> first = singleCookie.entrySet().first()
cookies << new Cookie(first?.key, first?.value)
}
void cookie(String cookieKey, Object cookieValue) {
cookies << new Cookie(cookieKey, cookieValue)
}
void executeForEachCookie(Closure closure) {
cookies?.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.cookies) return false
return true
}
int hashCode() {
return cookies.hashCode()
}
}

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

@@ -34,6 +34,7 @@ class OutputMessage extends Common {
DslProperty<String> sentTo
Headers headers
Cookies cookies
DslProperty body
ExecutionProperty assertThat
ResponseBodyMatchers matchers
@@ -43,6 +44,7 @@ class OutputMessage extends Common {
OutputMessage(OutputMessage outputMessage) {
this.sentTo = outputMessage.sentTo
this.headers = outputMessage.headers
this.cookies = outputMessage.cookies
this.body = outputMessage.body
}
@@ -68,6 +70,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

@@ -43,6 +43,7 @@ class Request extends Common {
Url url
UrlPath urlPath
Headers headers
Cookies cookies
Body body
Multipart multipart
BodyMatchers matchers
@@ -55,6 +56,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
}
@@ -117,6 +119,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))
}
@@ -259,6 +267,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

@@ -40,6 +40,7 @@ class Response extends Common {
DslProperty status
DslProperty delay
Headers headers
Cookies cookies
Body body
boolean async
ResponseBodyMatchers matchers
@@ -50,6 +51,7 @@ class Response extends Common {
Response(Response response) {
this.status = response.status
this.headers = response.headers
this.cookies = response.cookies
this.body = response.body
}
@@ -67,6 +69,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))
}
@@ -169,6 +177,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)