Created
August 12, 2014 20:44
-
-
Save delor/0f72e225d39862684f1c to your computer and use it in GitHub Desktop.
Testing RoboSpice with mocked MockWebServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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