Last active
July 24, 2017 05:14
-
-
Save SpOOnman/4452815 to your computer and use it in GitHub Desktop.
Howto keep session in HttBuilder with cookies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eu.spoonman.specs.rest | |
import groovyx.net.http.ContentType | |
import groovyx.net.http.Method | |
import eu.spoonman.connectors.RestConnector | |
import spock.lang.Specification | |
import spock.lang.Shared | |
import spock.lang.Stepwise | |
@Stepwise | |
class RestChartSpec extends Specification { | |
@Shared | |
RestConnector restConnector | |
def setupSpec() { | |
restConnector = new RestConnector('http://localhost:8080') | |
} | |
def "should login as test"() { | |
given: | |
Map params = [j_username: 'test', j_password: 'test'] | |
when: | |
restConnector.request(Method.POST, ContentType.ANY, '/frontoffice/j_spring_security_check', params) | |
then: | |
!(restConnector.cookies.empty) | |
} | |
def "should allow access to chart data series"() { | |
given: | |
Map params = [days: 14] | |
when: | |
Map result = restConnector.request(Method.POST, ContentType.JSON, "$BASE_URL/chart/series", params) | |
then: | |
result != null | |
result.series.size() > 0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eu.spoonman.connectors.RestConnector | |
import groovyx.net.http.Method | |
import groovyx.net.http.ContentType | |
import groovyx.net.http.HTTPBuilder | |
import groovyx.net.http.HttpResponseDecorator | |
class RestConnector { | |
private String baseUrl | |
private HTTPBuilder httpBuilder | |
private List<String> cookies | |
RestConnector(String url) { | |
this.baseUrl = url | |
this.httpBuilder = initializeHttpBuilder() | |
this.cookies = [] | |
} | |
public request(Method method, ContentType contentType, String url, Map<String, Serializable> params) { | |
debug("Send $method request to ${this.baseUrl}$url: $params") | |
httpBuilder.request(method, contentType) { request -> | |
uri.path = url | |
uri.query = params | |
headers.put('Cookie', cookies.join(';')) | |
} | |
} | |
private HTTPBuilder initializeHttpBuilder() { | |
def httpBuilder = new HTTPBuilder(baseUrl) | |
httpBuilder.handler.success = { HttpResponseDecorator resp, reader -> | |
resp.getHeaders('Set-Cookie').each { | |
// [Set-Cookie: JSESSIONID=E68D4799D4D6282F0348FDB7E8B88AE9; Path=/frontoffice/; HttpOnly] | |
String cookie = it.value.split(';')[0] | |
debug("Adding cookie to collection: $cookie") | |
cookies.add(cookie) | |
} | |
debug("Response: ${reader}") | |
return reader | |
} | |
return httpBuilder | |
} | |
private debug(String message) { | |
System.out.println(message) //for Gradle | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment