Skip to content

Instantly share code, notes, and snippets.

@ctoestreich
Created January 31, 2016 02:35
Show Gist options
  • Save ctoestreich/77d6a38f10af62fe3e3c to your computer and use it in GitHub Desktop.
Save ctoestreich/77d6a38f10af62fe3e3c to your computer and use it in GitHub Desktop.
package org.grails.demo
import grails.plugins.rest.client.RequestCustomizer
import grails.plugins.rest.client.RestResponse
import grails.test.mixin.TestFor
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
@TestFor(ExternalRestBuilderService)
class extends BaseRestBuilderSpec {
def "Mocked Post InvokeRestBuilderWithUrl"() {
given:
final String restUrl = "http://api.twitter.com"
String capturedUrl = null
def requestCustomizer = Mock(RequestCustomizer)
MockRestBuilder.metaClass.post = { String url, Closure closure ->
capturedUrl = url
closure.delegate = requestCustomizer
closure.call()
return new RestResponse(new ResponseEntity(HttpStatus.OK))
}
when:
service.invokeRestBuilderPostWithUrl(restUrl)
then:
capturedUrl == restUrl
1 * requestCustomizer.header('Cookie', "TEST=test")
1 * requestCustomizer.header('X-DEBUG', 'true')
1 * requestCustomizer.contentType("application/json")
}
def "Mocked Get InvokeRestBuilderWithUrl"() {
given:
final String restUrl = "http://api.twitter.com"
String capturedUrl = null
def requestCustomizer = Mock(RequestCustomizer)
MockRestBuilder.metaClass.get = { String url, Closure closure ->
capturedUrl = url
closure.delegate = requestCustomizer
closure.call()
return new RestResponse(new ResponseEntity(HttpStatus.OK))
}
when:
service.invokeRestBuilderGetWithUrl(restUrl)
then:
capturedUrl == restUrl
1 * requestCustomizer.header('Cookie', "TEST=test2")
1 * requestCustomizer.header('X-DEBUG', 'false')
1 * requestCustomizer.contentType("application/json")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment