Wiremock stubs with cookies + some tests

This commit is contained in:
Marcin Grzejszczak
2018-04-11 10:05:43 +07:00
committed by Alex Xandra Albert Sim
parent e7996e846e
commit a0641790a9
11 changed files with 71 additions and 13 deletions

View File

@@ -86,6 +86,17 @@ public class LoanApplicationService {
return response.getBody().getCount();
}
public String getCookies() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Cookie", "name=foo");
httpHeaders.add("Cookie", "name2=bar");
ResponseEntity<String> response =
restTemplate.exchange("http://localhost:" + port + "/frauds/name", HttpMethod.GET,
new HttpEntity<>(httpHeaders),
String.class);
return response.getBody();
}
public void setPort(int port) {
this.port = port;
}

View File

@@ -71,4 +71,12 @@ public class LoanApplicationServiceTests {
assertThat(count).isEqualTo(100);
}
@Test
public void shouldSuccessfullyGetCookies() {
// when:
String cookies = service.getCookies();
// then:
assertThat(cookies).isEqualTo("foo bar");
}
}

View File

@@ -1,5 +1,7 @@
package com.example.fraud;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@@ -24,6 +26,12 @@ class FraudNameController {
}
return new NameResponse("Don't worry " + request.getName() + " you're not a fraud");
}
@GetMapping(value = "/frauds/name")
public String checkByName(@CookieValue("name") String value,
@CookieValue("name2") String value2) {
return value + " " + value2;
}
}
interface FraudVerifier {

View File

@@ -0,0 +1,16 @@
package contracts.fraudname
org.springframework.cloud.contract.spec.Contract.make {
request {
method GET()
url '/frauds/name'
cookies {
cookie("name", "foo")
cookie(name2: "bar")
}
}
response {
status 200
body("foo bar")
}
}

View File

@@ -31,19 +31,19 @@ import groovy.transform.TypeChecked
@TypeChecked
class Cookies {
Set<Cookie> cookies = []
Set<Cookie> entries = []
void cookie(Map<String, Object> singleCookie) {
Map.Entry<String, Object> first = singleCookie.entrySet().first()
cookies << new Cookie(first?.key, first?.value)
entries << new Cookie(first?.key, first?.value)
}
void cookie(String cookieKey, Object cookieValue) {
cookies << new Cookie(cookieKey, cookieValue)
entries << new Cookie(cookieKey, cookieValue)
}
void executeForEachCookie(Closure closure) {
cookies?.each {
entries?.each {
cookie -> closure(cookie)
}
}
@@ -56,11 +56,11 @@ class Cookies {
if (this.is(o)) return true
if (getClass() != o.class) return false
Cookies cookies = (Cookies) o
if (cookies != cookies.cookies) return false
if (cookies != cookies.entries) return false
return true
}
int hashCode() {
return cookies.hashCode()
return entries.hashCode()
}
}

View File

@@ -1,6 +0,0 @@
#Fri Aug 19 15:38:58 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.0-bin.zip

View File

@@ -12,7 +12,7 @@
<snapshotVersion>
<extension>jar</extension>
<value>0.0.1-SNAPSHOT</value>
<updated>20160916125313</updated>
<updated>20170916125313</updated>
</snapshotVersion>
<snapshotVersion>
<extension>pom</extension>

View File

@@ -23,6 +23,7 @@ import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder
import com.github.tomakehurst.wiremock.matching.StringValuePattern
import com.github.tomakehurst.wiremock.matching.UrlPattern
import groovy.json.JsonOutput
import groovy.transform.CompileDynamic
import groovy.transform.PackageScope
import groovy.transform.TypeChecked
import groovy.transform.TypeCheckingMode
@@ -72,6 +73,7 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
}
RequestPatternBuilder requestPatternBuilder = appendMethodAndUrl()
appendHeaders(requestPatternBuilder)
appendCookies(requestPatternBuilder)
appendQueryParameters(requestPatternBuilder)
appendBody(requestPatternBuilder)
appendMultipart(requestPatternBuilder)
@@ -148,6 +150,15 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
}
}
private void appendCookies(RequestPatternBuilder requestPattern) {
if(!request.cookies) {
return
}
request.cookies.entries.each {
requestPattern.withCookie(it.key, convertToValuePattern(it.clientValue))
}
}
private UrlPattern urlPattern() {
Object urlPath = urlPathOrUrlIfQueryPresent()
if (urlPath) {
@@ -293,6 +304,7 @@ class WireMockRequestStubStrategy extends BaseWireMockStubStrategy {
return containsPattern(map.entrySet())
}
@CompileDynamic
private boolean containsPattern(Collection collection) {
return collection.collect(this.&containsPattern).inject('') { a, b -> a || b }
}

View File

@@ -1750,6 +1750,9 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
header(authorization(), "secret")
header(authorization(), "secret2")
}
cookies {
cookie("foo", "bar")
}
body(foo: "bar", baz: 5)
}
response {
@@ -1785,6 +1788,11 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
"equalTo" : "secret2"
}
},
"cookies" : {
"foo" : {
"equalTo" : "bar"
}
},
"queryParameters" : {
"foo" : {
"equalTo" : "bar2"
@@ -2017,6 +2025,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
RequestEntity.post(URI.create("http://localhost:" + port + "/api/v1/xxxx?foo=bar&foo=bar2"))
.header("Authorization", "secret")
.header("Authorization", "secret2")
.header("Cookie", "foo=bar")
.body("{\"foo\":\"bar\",\"baz\":5}"), String.class)
}
}