Skip to content

Commit

Permalink
Fixed cookie management issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjstehno committed Sep 10, 2017
1 parent e8ebf12 commit b167a13
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,6 @@ server.expectations {

This allows for additional flexibility in configuring expectations.


==== Matching Cookies

There are four methods for matching cookies associated with a request (found in the `com.stehno.ersatz.Request` interface):
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/com/stehno/ersatz/Cookie.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import groovy.transform.ToString
import java.util.function.Consumer

/**
* Ersatz abstraction of a request cookie. See also the <code>CookieMatcher</code>.
* Ersatz abstraction of a request or response cookie. See also the <code>CookieMatcher</code>.
*/
@CompileStatic @EqualsAndHashCode @ToString(includeNames = true)
@SuppressWarnings('ConfusingMethodName')
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/com/stehno/ersatz/CookieMatcher.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class CookieMatcher extends BaseMatcher<Cookie> {

@Override
void describeTo(final Description description) {
description.appendText('CookieMatcher: ')
description.appendText('Cookie matching ')

matchers.each { field, matcher ->
description.appendText("${field}(")
Expand Down
16 changes: 15 additions & 1 deletion src/main/groovy/com/stehno/ersatz/ErsatzServer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,21 @@ class ErsatzServer implements ServerConfig {
}

response.cookies.each { k, v ->
exchange.responseCookies.put(k, new CookieImpl(k, v))
if (v instanceof Cookie) {
Cookie ersatzCookie = v as Cookie
CookieImpl cookie = new CookieImpl(k, ersatzCookie.value)
cookie.path = ersatzCookie.path
cookie.domain = ersatzCookie.domain
cookie.maxAge = ersatzCookie.maxAge
cookie.secure = ersatzCookie.secure
cookie.version = ersatzCookie.version
cookie.httpOnly = ersatzCookie.httpOnly
cookie.setComment(ersatzCookie.comment)
exchange.responseCookies.put(k, cookie)

} else {
exchange.responseCookies.put(k, new CookieImpl(k, v as String))
}
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/groovy/com/stehno/ersatz/Response.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ interface Response {
*/
Response cookie(final String name, final String value)

need to be able to define cookie properties in the
/**
* Used to add a cookie to the response.
*
* @param name the cookie name
* @param cookie the cookie definition
* @return this response
*/
Response cookie(final String name, final Cookie cookie)

/**
* Used to add multiple cookies to the response, with the provided names and values.
Expand Down Expand Up @@ -171,11 +178,12 @@ interface Response {
Map<String, List<String>> getHeaders()

/**
* Used to retrieve the configured response cookies.
* Used to retrieve the configured response cookies. The map keys will be the cookie names, while the values will be either the cookie value as
* as String, or a <code>Cookie</code> object.
*
* @return the response cookies
*/
Map<String, String> getCookies()
Map<String, Object> getCookies()

/**
* Used to retrieve the configured response content. The content will be converted to a String based on the encoder configured for the
Expand Down
10 changes: 8 additions & 2 deletions src/main/groovy/com/stehno/ersatz/impl/ErsatzResponse.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ErsatzResponse implements Response {
}

private final Map<String, List<String>> headers = [:]
private final Map<String, String> cookies = [:]
private final Map<String, Object> cookies = [:]
private Object content
private Integer code = 200
private long delayTime
Expand Down Expand Up @@ -128,6 +128,12 @@ class ErsatzResponse implements Response {
this
}

@Override
Response cookie(final String name, final Cookie cookie) {
cookies[name] = cookie
this
}

@Override
Response contentType(final String contentType) {
header(CONTENT_TYPE_HEADER, contentType)
Expand Down Expand Up @@ -173,7 +179,7 @@ class ErsatzResponse implements Response {
}

@Override
Map<String, String> getCookies() {
Map<String, Object> getCookies() {
cookies.asImmutable()
}

Expand Down
109 changes: 109 additions & 0 deletions src/test/groovy/com/stehno/ersatz/CookieSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.stehno.ersatz

import spock.lang.Specification

import java.util.function.Consumer

class CookieSpec extends Specification {

def 'cookie (closure: methods)'() {
when:
Cookie cookie = Cookie.cookie {
value 'alpha'
comment 'Something'
domain 'localhost'
path '/foo'
version 1
httpOnly true
maxAge 100
secure true
}

then:
cookie.value == 'alpha'
cookie.comment == 'Something'
cookie.domain == 'localhost'
cookie.path == '/foo'
cookie.version == 1
cookie.httpOnly
cookie.maxAge == 100
cookie.secure
}

def 'cookie (closure: properties)'() {
when:
Cookie cookie = Cookie.cookie {
value = 'alpha'
comment = 'Something'
domain = 'localhost'
path = '/foo'
version = 1
httpOnly = true
maxAge = 100
secure = true
}

then:
cookie.value == 'alpha'
cookie.comment == 'Something'
cookie.domain == 'localhost'
cookie.path == '/foo'
cookie.version == 1
cookie.httpOnly
cookie.maxAge == 100
cookie.secure
}

def 'cookie (consumer: methods)'() {
when:
Cookie cookie = Cookie.cookie(new Consumer<Cookie>() {
@Override void accept(final Cookie c) {
c.value 'alpha'
c.comment 'Something'
c.domain 'localhost'
c.path '/foo'
c.version 1
c.httpOnly true
c.maxAge 100
c.secure true
}
})

then:
cookie.value == 'alpha'
cookie.comment == 'Something'
cookie.domain == 'localhost'
cookie.path == '/foo'
cookie.version == 1
cookie.httpOnly
cookie.maxAge == 100
cookie.secure
}

def 'cookie (consumer: properties)'() {
when:
Cookie cookie = Cookie.cookie(new Consumer<Cookie>() {
@Override void accept(final Cookie c) {
c.value = 'alpha'
c.comment = 'Something'
c.domain = 'localhost'
c.path = '/foo'
c.version = 1
c.httpOnly = true
c.maxAge = 100
c.secure = true
}
})

then:
cookie.value == 'alpha'
cookie.comment == 'Something'
cookie.domain == 'localhost'
cookie.path == '/foo'
cookie.version == 1
cookie.httpOnly
cookie.maxAge == 100
cookie.secure
}

}
24 changes: 17 additions & 7 deletions src/test/groovy/com/stehno/ersatz/ErsatzServerSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import java.util.function.Consumer

import static MultipartResponseContent.multipart
import static com.stehno.ersatz.ContentType.*
import static com.stehno.ersatz.CookieMatcher.cookieMatcher
import static com.stehno.ersatz.HttpMethod.*
import static org.hamcrest.Matchers.greaterThanOrEqualTo
import static org.hamcrest.Matchers.startsWith
Expand Down Expand Up @@ -446,20 +447,29 @@ class ErsatzServerSpec extends Specification {
ersatzServer.verify()
}
def 'baking cookies'(){
def 'baking cookies'() {
setup:
ersatzServer.expectations {
get('/setkermit').called(1).responder {
content('ok', TEXT_PLAIN)
cookie('kermit', 'frog; path=/showkermit')
cookie('kermit', Cookie.cookie {
value 'frog'
path '/showkermit'
})
}
get('/showkermit').cookie('kermit', { Cookie c->
c.path.startsWith('frog')
get('/showkermit').cookie('kermit', cookieMatcher {
value startsWith('frog')
}).called(1).responder {
content('ok', TEXT_PLAIN)
cookie('miss', 'piggy; path=/')
cookie('fozzy', 'bear; path=/some/deep/path')
cookie('miss', Cookie.cookie {
value 'piggy'
path '/'
})
cookie('fozzy', Cookie.cookie {
value 'bear'
path '/some/deep/path'
})
}
}
Expand All @@ -473,7 +483,7 @@ class ErsatzServerSpec extends Specification {
when:
response = client.newCall(
new okhttp3.Request.Builder().get().url(url('/showkermit')).addHeader('Cookie','kermit=frog; path=/showkermit').build()
new okhttp3.Request.Builder().get().url(url('/showkermit')).addHeader('Cookie', 'kermit=frog; path=/showkermit').build()
).execute()
then:
Expand Down

0 comments on commit b167a13

Please sign in to comment.