[rest-gradle-plugin] 56/83: Clean response handler code and add more test cases
Alastair McKinstry
mckinstry at moszumanska.debian.org
Wed Oct 25 15:59:07 UTC 2017
This is an automated email from the git hooks/post-receive script.
mckinstry pushed a commit to branch debian/master
in repository rest-gradle-plugin.
commit 666147261e22f41128124b4b4cd0c0d8f57fe6d6
Author: noamt <noam at 10ne.org>
Date: Sun Sep 20 22:53:55 2015 +0300
Clean response handler code and add more test cases
---
.../groovy/org/_10ne/gradle/rest/RestTask.groovy | 32 +++++----
.../org/_10ne/gradle/rest/RestTaskSpec.groovy | 79 ++++++++++++++++++----
2 files changed, 87 insertions(+), 24 deletions(-)
diff --git a/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy b/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
index 4699d9b..742f078 100644
--- a/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
+++ b/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
@@ -128,22 +128,30 @@ class RestTask extends DefaultTask {
try {
serverResponse = client."${httpMethod.toLowerCase()}"(params)
- if (responseHandler && responseHandler.maximumNumberOfParameters == 1) {
- def parameterType = responseHandler.parameterTypes.first()
- if (InputStream.isAssignableFrom(parameterType)) {
- responseHandler.call(serverResponse.entity.content)
- } else if (String.isAssignableFrom(parameterType)) {
- serverResponse.entity.content.withStream {
- responseHandler.call(it.text)
- }
- } else {
- responseHandler.call(serverResponse.data)
- }
- } else {
+ if (noResponseHandler()) {
slf4jLogger.info "Server Response:" + System.lineSeparator() + serverResponse.getData()
+ } else {
+ callResponseHandler()
}
} catch (groovyx.net.http.HttpResponseException e) {
throw new GradleException(e.getResponse().getData().toString(), e)
}
}
+
+ private boolean noResponseHandler() {
+ !responseHandler || responseHandler.maximumNumberOfParameters != 1
+ }
+
+ void callResponseHandler() {
+ def parameterType = responseHandler.parameterTypes.first()
+ if (InputStream.isAssignableFrom(parameterType)) {
+ responseHandler.call(serverResponse.entity.content)
+ } else if (String.isAssignableFrom(parameterType)) {
+ serverResponse.entity.content.withStream {
+ responseHandler.call(it.text)
+ }
+ } else {
+ responseHandler.call(serverResponse.data)
+ }
+ }
}
diff --git a/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy b/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
index d1352b4..55f3062 100644
--- a/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
+++ b/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
@@ -176,17 +176,12 @@ class RestTaskSpec extends Specification {
'https' | 8443
}
- def 'Configure and execute a request with a custom response handler'() {
+ def 'Configure and execute a request with a custom string response handler'() {
setup:
def responseCalled = false
Task task = project.tasks.create(name: 'request', type: RestTask) {
- httpMethod = 'post'
uri = 'bob.com'
- username = 'username'
- password = 'password'
- requestContentType = 'requestContentType'
- requestBody = 'requestBody'
contentType = 'contentType'
responseHandler = { String responseText ->
responseCalled = (responseText == 'called')
@@ -195,7 +190,39 @@ class RestTaskSpec extends Specification {
def mockClient = Mock(RESTClient)
task.client = mockClient
- def mockAuth = Mock(AuthConfig)
+ def mockResponse = Mock(HttpResponseDecorator) {
+ getEntity() >> {
+ def entity = new BasicHttpEntity()
+ entity.content = new StringInputStream('called')
+ entity
+ }
+ }
+
+ when:
+ task.executeRequest()
+
+ then:
+ 1 * mockClient.setUri('bob.com')
+ 1 * mockClient.get(_ as Map) >> { Map params ->
+ assert params.contentType == 'contentType'
+ mockResponse
+ }
+ responseCalled
+ }
+
+ def 'Configure and execute a request with a custom input stream response handler'() {
+ setup:
+ def responseCalled = false
+
+ Task task = project.tasks.create(name: 'request', type: RestTask) {
+ uri = 'bob.com'
+ contentType = 'contentType'
+ responseHandler = { InputStream is ->
+ responseCalled = (is.text == 'called')
+ }
+ }
+ def mockClient = Mock(RESTClient)
+ task.client = mockClient
def mockResponse = Mock(HttpResponseDecorator) {
getEntity() >> {
@@ -210,12 +237,40 @@ class RestTaskSpec extends Specification {
then:
1 * mockClient.setUri('bob.com')
- 1 * mockClient.getAuth() >> { mockAuth }
- 1 * mockAuth.basic('username', 'password')
- 1 * mockClient.post(_ as Map) >> { Map params ->
- assert params.body == 'requestBody'
+ 1 * mockClient.get(_ as Map) >> { Map params ->
+ assert params.contentType == 'contentType'
+ mockResponse
+ }
+ responseCalled
+ }
+
+ def 'Configure and execute a request with a custom data response handler'() {
+ setup:
+ def responseCalled = false
+
+ Task task = project.tasks.create(name: 'request', type: RestTask) {
+ uri = 'bob.com'
+ contentType = 'contentType'
+ responseHandler = { Map map ->
+ responseCalled = (map.content == 'called')
+ }
+ }
+ def mockClient = Mock(RESTClient)
+ task.client = mockClient
+
+ def mockResponse = Mock(HttpResponseDecorator) {
+ getData() >> {
+ [content: 'called']
+ }
+ }
+
+ when:
+ task.executeRequest()
+
+ then:
+ 1 * mockClient.setUri('bob.com')
+ 1 * mockClient.get(_ as Map) >> { Map params ->
assert params.contentType == 'contentType'
- assert params.requestContentType == 'requestContentType'
mockResponse
}
responseCalled
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/rest-gradle-plugin.git
More information about the pkg-java-commits
mailing list