In my real-world scenario I have a REST service for AJAX purposes. It renders data series for graphs. I want to test it with groovy's excellent HttpBuilder. There is a problem though - these requests are only available for already logged in users.
In this post I present a complete solution to maintain a session state between HttpBuilder's requests.
Session in HttpBuilder
First of all a quick reminder about session. Session is a simulation of state for HTTP requests, which are stateless by its nature. Once you log in you receive a unique cookie (one or more) that identifies you for sequential requests. Every time you send request you send this cookie along. This way server recognizes you and matches you to your session, which is kept on server. Cookie gets invlid once you log out or it times out, for example after 20 minutes of inactivity. Next time you visit a page you get a new, unique cookie.
In order to keep session alive in HttpBuilder I need to:
- log in to my Grails application
- receive a JSESSIONID cookie in response
- store that cookie and send it along with every subsenquential request
I've created RestConnector
class that wraps up HttpBuilder. It's main improvement is that it keeps received cookie in a list.
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 def 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['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
}
}
A few things to notice in a class above. Constructor sets base URL and creates HttpBuilder instance that can be reused. Next, there is a handler on successful request that checks if I receive any cookie. It adds received cookies to list. Finally, there is a request
method that calls HttpBuilder#request
but it adds cookies to HTTP headers so server can recognize me as a logged in user.
Sending cookies with every request is a core component in here. It simulates browser's behavior and maintains session.
How to use it?
I will show you how to use this utility class it in Spock test below. It is fairly simple.
First I login to my application and I ensure that I receive a cookie in return, which is equivalent to being logged in. Then I send a request with that cookie sent in HTTP header. This is a Spock test that implements it:
package eu.spoonman.specs.rest
import eu.spoonman.connectors.RestConnector.RestConnector
import groovyx.net.http.ContentType
import groovyx.net.http.Method
import spock.lang.Shared
import spock.lang.Specification
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, "frontoffice/chart/series", params)
then:
result != null
result.series.size() > 0
}
}
I create a new RestConnector
instance in setupSpec
with my application's base URL. Please notice that it has @Shared
annotation so it's shared between tests.
@Stepwise
is crucial annotation for this specification. It means that Spock executes tests exactly in order they're defined. I need to ensure that login is executed first. I also need to assert that I receive a cookie and list is not empty. I could move this step into setupSpec
method too, but I prefer it to be a first test in a specification.
Second test is always executed after login thus it sends cookies within request headers. This is exactly what I wanted to achieve.
Hi,
ReplyDeleteIt is great , can you tell me how to use it to send the the cookie for each request .
I am getting the cookie , but don't know to send them back
Hi Denise,
ReplyDeleteTake a look at RestConnector#request method. You send cookies with headers like this: headers['Cookie'] = cookies.join(';'). So if you want to send two cookies use it like this: headers['Cookie'] = 'firstcookie=firstvalue;secondcookie=secondvalue'.
Hello Tom
ReplyDeleteThank you for this blog post! I had a very similar requirement and it was really helpful.
You're welcome Tom. I'm glad it helps.
DeleteThis post was very helpful. Thanks
ReplyDeleteThis is super. Thanks for putting this together.
ReplyDeleteSorry I don't understand why you try to manage cookies by hand. HttpBuilder is based on HttpClient which manage this for you.
ReplyDeleteGoogle loves cookie, the code below send 2 requests, the 2nd contains the cookies returned by the first one ...
def http = new HTTPBuilder('http://www.google.fr')
def html = http.get( path : '/')
html = http.get( path : '/search', query : [q:'Groovy'] )
If you want to manually add one, just reach the cookiestore
http.getClient().getCookieStore().addCookie(.....)
Thanks! That's a valuable hint.
Deletehello Tomasz, it doesn,t seem to work with an asp session. I manage to connect but the session is not kept? Do you have an idea?
ReplyDeletethanks for your help
hello Tomasz, helpful post. Thanks for sharing
ReplyDeleteQuick question as I am not super literate on HttpBuilder details--how would one check the HTTP status on a request using RestConnector?
ReplyDeleteCasino Tycoon 2020 - Mapyro
ReplyDeleteThe 포항 출장마사지 first 순천 출장안마 casino to bring Vegas gambling 남원 출장샵 to 제천 출장마사지 Atlantic City is Atlantic City Casino of 파주 출장샵 the Year in 2016. With the addition of the Borgata, there are many new