Skip to content
Browse files

Cleanup and allow content type header to be passed.

  • Loading branch information...
1 parent 07d2676 commit 047d3789d1f43846aab9d61e4313ae8f5d29b1eb Antony Jones committed Dec 19, 2012
View
5 grails-app/services/uk/co/desirableobjects/oauth/scribe/OauthService.groovy
@@ -190,7 +190,10 @@ class OauthService implements InitializingBean {
url: args[1] as String,
bodyParameters: (args.length > 2) ? args[2] as Map : null
)
- resourceAccessor.addHeader('Content-Type', args[3] as String)
+
+ if (args.length > 3) {
+ resourceAccessor.addHeader('Content-Type', args[3] as String)
+ }
return oauthResourceService.accessResource(findService(serviceName), args[0] as Token, resourceAccessor)
View
2 src/groovy/uk/co/desirableobjects/oauth/scribe/resource/ResourceAccessor.groovy
@@ -1,7 +1,9 @@
package uk.co.desirableobjects.oauth.scribe.resource
+import groovy.transform.EqualsAndHashCode
import org.scribe.model.Verb
+@EqualsAndHashCode
class ResourceAccessor {
int connectTimeout
View
191 test/unit/uk/co/desirableobjects/oauth/scribe/AuthorisationProcessSpec.groovy
@@ -16,13 +16,14 @@ import org.scribe.model.Response
import org.springframework.http.HttpStatus
import spock.lang.Unroll
import uk.co.desirableobjects.oauth.scribe.exception.UnknownProviderException
+import uk.co.desirableobjects.oauth.scribe.resource.ResourceAccessor
@Stepwise
@TestMixin(GrailsUnitTestMixin)
class AuthorisationProcessSpec extends Specification {
private static final String DUMMY_OAUTH_RESOURCE_URI = 'http://example.org/list'
- @Shared Token requestToken
+ @Shared Token requestToken
@Shared Token accessToken
@Shared OauthService oaService
@@ -31,30 +32,17 @@ class AuthorisationProcessSpec extends Specification {
given:
- grailsApplication.config = [oauth:
- [providers:
- [twitter:[
- api: TwitterApi,
- key: 'myKey',
- secret: 'mySecret'
- ]
- ]
- ]
- ]
-
- mockConfig """
- import org.scribe.builder.api.TwitterApi
-
- oauth {
- providers {
- twitter {
- api = TwitterApi
- key = 'myKey'
- secret = 'mySecret'
- }
- }
- }
- """
+// grailsApplication.config = [oauth:
+// [providers:
+// [twitter:[
+// api: TwitterApi,
+// key: 'myKey',
+// secret: 'mySecret'
+// ]
+// ]
+// ]
+// ]
+
oaService = new OauthService()
oaService.grailsApplication = [config: [
oauth: [
@@ -65,147 +53,148 @@ class AuthorisationProcessSpec extends Specification {
secret: "mySecret" ] ]]]]
oaService.afterPropertiesSet()
- oaService.services.twitter.service = mock(OAuthService)
- oaService.services.twitter.service.getRequestToken().returns(new Token('a', 'b', 'c'))
+ oaService.services.twitter.service = Mock(OAuthService)
when:
- simulate {
- requestToken = oaService.getTwitterRequestToken()
- }
+ requestToken = oaService.getTwitterRequestToken()
then:
+ 1 * oaService.services.twitter.service.getRequestToken() >> { return new Token('a', 'b', 'c') }
+ 0 * _
+
+ and:
requestToken.rawResponse == 'c'
}
def 'make the user validate our token'() {
given:
+ oaService.services.twitter.service = Mock(OAuthService)
- oaService.services.twitter.service.getAuthorizationUrl(requestToken).returns('http://example.org/auth')
+ when:
+ String authUrl = oaService.getTwitterAuthorizationUrl(requestToken)
- expect:
+ then:
+ 1 * oaService.services.twitter.service.getAuthorizationUrl(requestToken) >> { return 'http://example.org/auth' }
+ 0 * _
- simulate {
- oaService.getTwitterAuthorizationUrl(requestToken) == 'http://example.org/auth'
- }
+ and:
+ authUrl == 'http://example.org/auth'
}
def 'get the access token'() {
given:
-
Token expectedToken = new Token('d', 'e', 'f')
Verifier verifier = new Verifier('abcde')
- oaService.services.twitter.service = mock(OAuthService)
- oaService.services.twitter.service.getAccessToken(requestToken, verifier).returns(expectedToken)
+ oaService.services.twitter.service = Mock(OAuthService)
when:
-
- simulate {
- accessToken = oaService.getTwitterAccessToken(requestToken, verifier)
- }
+ accessToken = oaService.getTwitterAccessToken(requestToken, verifier)
then:
+ 1 * oaService.services.twitter.service.getAccessToken(requestToken, verifier) >> { return expectedToken }
+ 0 * _
+ and:
accessToken.rawResponse == 'f'
}
// TODO: Why do we fetch and pass the token - if we don't pass it, you could automatically get it?
@Unroll
- def 'make a #verb request using the authorised connection'() {
+ def 'make a #verb request using the authorised connection with an xml payload'() {
given:
-
- oaService.services['twitter'].service = mock(OAuthService)
+ oaService.services.twitter.service = Mock(OAuthService)
+ oaService.oauthResourceService = Mock(OauthResourceService)
+ Response oaResponse = Mock(Response)
and:
-
String expectedResponse = 'Hello There.'
- Response oaResponse = mock(Response)
- oaResponse.getBody().returns(expectedResponse)
- oaResponse.getCode().returns(HttpStatus.OK.value())
-
- and:
- oaService.oauthResourceService = mock(OauthResourceService)
- oaService.oauthResourceService.accessResource(oaService.services['twitter'].service, accessToken, verb, DUMMY_OAUTH_RESOURCE_URI, null, 30000, 30000).returns(oaResponse)
when:
-
String body = null
int code = -1
and:
-
- simulate {
-
- def actualResponse = oaService."${verb.name().toLowerCase()}TwitterResource"(accessToken, DUMMY_OAUTH_RESOURCE_URI)
- body = actualResponse.body
- code = actualResponse.code
-
- }
+ def actualResponse = oaService."${verb}TwitterResourceWithPayload"(accessToken, DUMMY_OAUTH_RESOURCE_URI, payload, headers)
+ body = actualResponse.body
+ code = actualResponse.code
then:
+ 1 * oaService.oauthResourceService.accessResource(oaService.services.twitter.service, accessToken, { ResourceAccessor ra ->
+ ra.connectTimeout == 30000
+ ra.receiveTimeout == 30000
+ ra.verb == verb
+ ra.url == DUMMY_OAUTH_RESOURCE_URI
+ ra.payload = payload?.bytes
+ } as ResourceAccessor) >> { return oaResponse }
+ 1 * oaResponse.getBody() >> expectedResponse
+ 1 * oaResponse.getCode() >> HttpStatus.OK.value()
+ 0 * _
+
+ and:
code == HttpStatus.OK.value()
body == expectedResponse
where:
- verb << Verb.values()
-
+ verb | payload | headers
+ 'get' | '<xml><tag /></xml>' | ['Content-Length': 15, 'Content-Type': 'application/xml']
+ 'post' | '<xml><tag /></xml>' | [:]
+ 'delete' | 'xyzabc' | ['Content-Type': 'application/xml', Accept: 'application/pdf' ]
}
- @Unroll
- def 'make a #verb request using the authorised connection and an xml payload'() {
+ @Unroll
+ def 'make a #verb request using the authorised connection'() {
- given:
+ given:
+ oaService.services.twitter.service = Mock(OAuthService)
+ oaService.oauthResourceService = Mock(OauthResourceService)
+ Response oaResponse = Mock(Response)
- oaService.services['twitter'].service = mock(OAuthService)
+ and:
+ String expectedResponse = 'Hello There.'
- and:
+ when:
+ String body = null
+ int code = -1
- String expectedResponse = 'Hello There.'
- Response oaResponse = mock(Response)
- oaResponse.getBody().returns(expectedResponse)
- oaResponse.getCode().returns(HttpStatus.OK.value())
+ and:
+ def actualResponse = oaService."${verb}TwitterResource"(accessToken, DUMMY_OAUTH_RESOURCE_URI, [a: 'b'], 'application/pdf')
+ body = actualResponse.body
+ code = actualResponse.code
- and:
-
- oaService.oauthResourceService = mock(OauthResourceService)
- oaService.oauthResourceService.accessResource(oaService.services['twitter'].service, accessToken, verb, DUMMY_OAUTH_RESOURCE_URI, '<data>mock data</data>', 'application/xml', 30000, 30000).returns(oaResponse)
-
- when:
-
- String body = null
- int code = -1
+ then:
+ 1 * oaService.oauthResourceService.accessResource(oaService.services.twitter.service, accessToken, { ResourceAccessor ra ->
+ ra.connectTimeout == 30000
+ ra.receiveTimeout == 30000
+ ra.verb == verb
+ ra.url == DUMMY_OAUTH_RESOURCE_URI
+ ra.bodyParameters = [a: 'b']
+ println ra.headers
+ //ra.headers == [Accept: 'application/pdf']
+ } as ResourceAccessor) >> { return oaResponse }
+ 1 * oaResponse.getBody() >> expectedResponse
+ 1 * oaResponse.getCode() >> HttpStatus.OK.value()
+ 0 * _
- and:
+ and:
+ code == HttpStatus.OK.value()
+ body == expectedResponse
- simulate {
+ where:
+ verb << ['delete', 'post', 'get']
- def actualResponse = oaService."${verb.name().toLowerCase()}TwitterResourceWithPayload"(accessToken, DUMMY_OAUTH_RESOURCE_URI, '<data>mock data</data>', 'application/xml')
- body = actualResponse.body
- code = actualResponse.code
+ }
- }
-
- then:
-
- code == HttpStatus.OK.value()
- body == expectedResponse
-
- where:
-
- verb << Verb.values()
-
-
- }
- def 'try to call an invalid resource accessor method on the service' () {
+ def 'try to call an invalid resource accessor method on the service' () {
when:

0 comments on commit 047d378

Please sign in to comment.
Something went wrong with that request. Please try again.