[rest-gradle-plugin] 17/83: Added support for preemptive authentication
Alastair McKinstry
mckinstry at moszumanska.debian.org
Wed Oct 25 15:59:03 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 b9b8685ff79fe37dc2b27a4ca9d65185d56f2d0a
Author: noamt <noam at 10ne.org>
Date: Tue Jun 25 18:23:15 2013 +0300
Added support for preemptive authentication
---
.../groovy/org/_10ne/gradle/rest/RestTask.groovy | 8 +++-
.../org/_10ne/gradle/rest/RestTaskSpec.groovy | 46 +++++++++++++++++++++-
2 files changed, 51 insertions(+), 3 deletions(-)
diff --git a/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy b/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
index 55c5dec..f5f76e9 100644
--- a/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
+++ b/src/main/groovy/org/_10ne/gradle/rest/RestTask.groovy
@@ -19,6 +19,7 @@ package org._10ne.gradle.rest
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import org.apache.commons.lang.StringUtils
+import org.apache.http.HttpHeaders
import org.gradle.api.DefaultTask
import org.gradle.api.InvalidUserDataException
import org.gradle.api.tasks.Input
@@ -30,11 +31,13 @@ import org.gradle.api.tasks.TaskAction
*/
class RestTask extends DefaultTask {
- def client
+ RESTClient client
@Input String httpMethod
@Input Object uri
@Input
+ @Optional boolean preemptiveAuth
+ @Input
@Optional String username
@Input
@Optional String password
@@ -58,6 +61,9 @@ class RestTask extends DefaultTask {
client.uri = uri
if (StringUtils.isNotBlank(username)) {
+ if (preemptiveAuth) {
+ client.headers[HttpHeaders.AUTHORIZATION] = "$username:$password".toString().bytes.encodeBase64()
+ }
client.auth.basic(username, password)
}
diff --git a/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy b/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
index cf40d0d..29d4a24 100644
--- a/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
+++ b/src/test/groovy/org/_10ne/gradle/rest/RestTaskSpec.groovy
@@ -3,6 +3,7 @@ package org._10ne.gradle.rest
import groovyx.net.http.AuthConfig
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
+import org.apache.http.HttpHeaders
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Project
import org.gradle.api.Task
@@ -22,7 +23,7 @@ class RestTaskSpec extends Specification {
def 'Execute a request with no URI'() {
setup:
- Task task = project.tasks.add(name: 'noUri', type: RestTask) {}
+ Task task = project.tasks.create(name: 'noUri', type: RestTask) {}
when:
task.executeRequest()
@@ -33,7 +34,7 @@ class RestTaskSpec extends Specification {
def 'Configure and execute a request'() {
setup:
- Task task = project.tasks.add(name: 'request', type: RestTask) {
+ Task task = project.tasks.create(name: 'request', type: RestTask) {
httpMethod = 'post'
uri = 'bob.com'
username = 'username'
@@ -64,4 +65,45 @@ class RestTaskSpec extends Specification {
}
1 * mockResponse.getData() >> { 'somedata' }
}
+
+ def 'Configure and execute a preemptive authentication request'() {
+ setup:
+ Task task = project.tasks.create(name: 'request', type: RestTask) {
+ httpMethod = 'post'
+ uri = 'bob.com'
+ preemptiveAuth = true
+ username = 'username'
+ password = 'password'
+ requestContentType = 'requestContentType'
+ requestBody = 'requestBody'
+ contentType = 'contentType'
+ }
+ def mockClient = Mock(RESTClient)
+ task.client = mockClient
+
+ def mockAuth = Mock(AuthConfig)
+
+ def mockResponse = Mock(HttpResponseDecorator)
+
+ def headers = [:]
+
+ when:
+ task.executeRequest()
+
+ and:
+ headers[HttpHeaders.AUTHORIZATION] == 'dXNlcm5hbWU6cGFzc3dvcmQ='
+
+ then:
+ 1 * mockClient.setUri('bob.com')
+ 1 * mockClient.getHeaders() >> { headers }
+ 1 * mockClient.getAuth() >> { mockAuth }
+ 1 * mockAuth.basic('username', 'password')
+ 1 * mockClient.post(_ as Map) >> { Map params ->
+ assert params.body == 'requestBody'
+ assert params.contentType == 'contentType'
+ assert params.requestContentType == 'requestContentType'
+ mockResponse
+ }
+ 1 * mockResponse.getData() >> { 'somedata' }
+ }
}
--
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