Created
June 2, 2014 02:34
-
-
Save alvarosanchez/34b1a316314e38334f94 to your computer and use it in GitHub Desktop.
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 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") | |
def "it returns a list of games in JSON"() { | |
when: | |
Response response = restClient.get(path: "/games", accept: ContentType.JSON) | |
then: | |
response.json | |
response.contentType == 'application/json' | |
} | |
def "it creates games"() { | |
when: | |
Response response = restClient.post(path: "/games") { | |
json name:"New game" | |
} | |
then: | |
response.statusCode == 201 | |
response.headers['Location'] | |
} | |
def "it modifies games"() { | |
when: | |
restClient.put(path: "/games/6") { | |
json name:"New game, modified" | |
} | |
and: | |
Response response = restClient.get(path: "/games/6", accept: ContentType.JSON) | |
then: | |
response.statusCode == 200 | |
response.json.name == "New game, modified" | |
} | |
def "it removes games"() { | |
when: | |
Response response = restClient.delete(path: "/games/6") | |
then: | |
response.statusCode == 204 | |
when: | |
restClient.get(path: "/games/6", accept: ContentType.JSON) | |
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) | |
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') | |
JSONElement json = JSON.parse(response.contentAsString) | |
then: | |
json.name == 'Castle Builder' | |
json.'_links'.self.href == "http://localhost:8080/restful-gr8conf-2014/games/1" | |
} | |
} |
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
import com.odobo.gr8conf.Category | |
import com.odobo.gr8conf.Game | |
import grails.rest.render.hal.HalJsonCollectionRenderer | |
import grails.rest.render.hal.HalJsonRenderer | |
// Place your Spring DSL code here | |
beans = { | |
categoriesRenderer(HalJsonCollectionRenderer, Category) | |
categoryRenderer(HalJsonRenderer, Category) | |
gamesRenderer(HalJsonCollectionRenderer, Game) | |
gameRenderer(HalJsonRenderer, Game) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment