Skip to content

Instantly share code, notes, and snippets.

@spaceCamel
Created June 4, 2013 11:51
Show Gist options
  • Save spaceCamel/5705366 to your computer and use it in GitHub Desktop.
Save spaceCamel/5705366 to your computer and use it in GitHub Desktop.
Spock Test of a simple request to the GitHub api that fails for HttpBuilder but works via the URL.text
package com.qmetric.renewal.githubapi
import groovy.json.JsonSlurper
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient
import spock.lang.Specification
import static groovyx.net.http.ContentType.JSON
class GitHubApiTest extends Specification
{
public static final String GITHUB_API = 'https://api.github.com'
public static final String USER_PATH = '/users/xan'
def 'access git-hub web api via HTTPBuilder'()
{
def http = new HTTPBuilder(GITHUB_API)
when:
def user = http.get(path: USER_PATH, contentType: JSON)
then:
user.login == 'xan'
}
def 'access git-hub web api via RESTClient'()
{
def http = new RESTClient(GITHUB_API)
when:
def user = http.get(path: USER_PATH, contentType: JSON)
then:
user.login == 'xan'
}
def 'access git-hub web api via JsonSlurper'()
{
when:
def json = "${GITHUB_API}${USER_PATH}".toURL().text
def user = new JsonSlurper().parseText(json)
then:
user.login == 'xan'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment