Skip to content

Instantly share code, notes, and snippets.

@delor
Created August 12, 2014 20:44
Show Gist options
  • Save delor/0f72e225d39862684f1c to your computer and use it in GitHub Desktop.
Save delor/0f72e225d39862684f1c to your computer and use it in GitHub Desktop.
Testing RoboSpice with mocked MockWebServer
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.mockwebserver.MockResponse
import com.squareup.okhttp.mockwebserver.MockWebServer
import com.squareup.okhttp.mockwebserver.RecordedRequest
import spock.lang.Specification
class OAuthTokenRequestSpec extends Specification {
def POST = "POST"
def anyClient = "123"
def secret = "secret"
def path = "/2/oauth/access-token"
def accessToken = "A3lkjdsf65sfFJJH734jhuvEdTE7"
def tokenType = "Bearer"
def expiresIn = "3599"
MockWebServer server
def setup() {
server = new MockWebServer()
}
def cleanup() {
server.shutdown()
}
def "request for oauth2 token returns correct token dto"() {
def body =
"""
{
"access_token": "${accessToken}",
"token_type": "${tokenType}",
"expires_in": "${expiresIn}"
}
"""
def response = new MockResponse().setBody(body)
server.enqueue(response)
server.play()
def url = server.getUrl(path).toString()
def request = OAuthTokenRequest.builder()
.url(url)
.clientId(anyClient)
.clientSecret(secret)
.build()
request.okHttpClient = new OkHttpClient()
when:
def token = request.loadDataFromNetwork()
then:
token.accessToken == accessToken
token.tokenType == tokenType
token.expiresIn == expiresIn
RecordedRequest request1 = server.takeRequest()
request1.path == path
request1.method == POST
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment