Skip to content

Instantly share code, notes, and snippets.

@alvarosanchez
Created June 2, 2014 03:17
Show Gist options
  • Save alvarosanchez/e5152c2f10b74543e68e to your computer and use it in GitHub Desktop.
Save alvarosanchez/e5152c2f10b74543e68e to your computer and use it in GitHub Desktop.
package com.odobo.gr8conf
import spock.lang.Ignore
import spock.lang.Shared
import spock.lang.Specification
import wslite.rest.ContentType
import wslite.rest.RESTClient
import wslite.rest.Response
class CategorySpec extends Specification {
@Shared
String token
@Shared
RESTClient restClient = new RESTClient("http://localhost:8080/restful-gr8conf-2014")
def setupSpec() {
Response response = restClient.post(path: "/api/login") {
json username:"user", password: "password"
}
this.token = response.json.token
}
def "it returns all the cateogories"() {
when:
Response response = restClient.get(path: "/categories", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
response.json.size() == 5
}
def "it returns just the categories of a game when is a nested resource"() {
when:
Response response = restClient.get(path: "/games/5/categories", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
response.json.size() == 2
}
@Ignore
def "class attribute is not on the JSON response of a category"() {
when:
Response response = restClient.get(path: "/categories/1", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
!response.json.'class'
}
@Ignore
def "class attribute is not on the JSON response of a nested category"() {
when:
Response response = restClient.get(path: "/games/5/categories", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
response.json.every { !it.'class' }
}
def "it can authenticate users"() {
when:
Response response = restClient.post(path: "/api/login") {
json username:"user", password: "password"
}
then:
response.json.token
}
}
package com.odobo.gr8conf
import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONElement
import spock.lang.Ignore
import spock.lang.Shared
import spock.lang.Specification
import wslite.rest.ContentType
import wslite.rest.RESTClient
import wslite.rest.RESTClientException
import wslite.rest.Response
/**
* Created by mariscal on 01/06/14.
*/
class GameSpec extends Specification {
@Shared
RESTClient restClient = new RESTClient("http://localhost:8080/restful-gr8conf-2014")
@Shared
String token
def setupSpec() {
Response response = restClient.post(path: "/api/login") {
json username:"user", password: "password"
}
this.token = response.json.token
}
def "it returns a list of games in JSON"() {
when:
Response response = restClient.get(path: "/games", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
response.json
response.contentType == 'application/json'
}
def "it creates games"() {
when:
Response response = restClient.post(path: "/games", headers: ['X-Auth-Token': token]) {
json name:"New game"
}
then:
response.statusCode == 201
response.headers['Location']
}
def "it modifies games"() {
when:
restClient.put(path: "/games/6", headers: ['X-Auth-Token': token]) {
json name:"New game, modified"
}
and:
Response response = restClient.get(path: "/games/6", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
response.statusCode == 200
response.json.name == "New game, modified"
}
def "it removes games"() {
when:
Response response = restClient.delete(path: "/games/6", headers: ['X-Auth-Token': token])
then:
response.statusCode == 204
when:
restClient.get(path: "/games/6", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
RESTClientException e = thrown(RESTClientException)
e.response.statusCode == 404
}
@Ignore
def "class attribute is not on the JSON response of a game"() {
when:
Response response = restClient.get(path: "/games/1", accept: ContentType.JSON, headers: ['X-Auth-Token': token])
then:
!response.json.'class'
response.json.categories.every { !it.'class' }
}
def "it returns a game in HAL format"() {
when:
Response response = restClient.get(path: "/games/1", accept: 'application/hal+json', headers: ['X-Auth-Token': token])
JSONElement json = JSON.parse(response.contentAsString)
then:
json.name == 'Castle Builder'
json.'_links'.self.href == "http://localhost:8080/restful-gr8conf-2014/games/1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment