|
Play Framework/Scala example source code file (OAuthSpec.scala)
The OAuthSpec.scala Play Framework example source code
/*
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package play.api.libs.oauth
import play.api.mvc._
import play.api.test._
import play.api.Application
import scala.concurrent.{Promise, Future}
import play.api.libs.ws.WS
class OAuthSpec extends PlaySpecification {
sequential
val consumerKey = ConsumerKey("someConsumerKey", "someVerySecretConsumerSecret")
val requestToken = RequestToken("someRequestToken", "someVerySecretRequestSecret")
val oauthCalculator = OAuthCalculator(consumerKey, requestToken)
"OAuth" should {
"sign a simple get request" in {
val (request, body, hostUrl) = receiveRequest { implicit app => hostUrl =>
WS.url(hostUrl + "/foo").sign(oauthCalculator).get()
}
OAuthRequestVerifier.verifyRequest(request, body, hostUrl, consumerKey, requestToken)
}
"sign a get request with query parameters" in {
val (request, body, hostUrl) = receiveRequest { implicit app => hostUrl =>
WS.url(hostUrl + "/foo").withQueryString("param" -> "paramValue").sign(oauthCalculator).get()
}
OAuthRequestVerifier.verifyRequest(request, body, hostUrl, consumerKey, requestToken)
}
"sign a post request with a body" in {
val (request, body, hostUrl) = receiveRequest { implicit app => hostUrl =>
WS.url(hostUrl + "/foo").sign(oauthCalculator).post(Map("param" -> Seq("paramValue")))
}
OAuthRequestVerifier.verifyRequest(request, body, hostUrl, consumerKey, requestToken)
}
}
def receiveRequest(makeRequest: Application => String => Future[_]): (RequestHeader, Array[Byte], String) = {
val hostUrl = "http://localhost:" + testServerPort
val promise = Promise[(RequestHeader, Array[Byte])]()
val app = FakeApplication(withRoutes = {
case _ => Action(BodyParsers.parse.raw) { request =>
promise.success((request, request.body.asBytes().getOrElse(Array.empty[Byte])))
Results.Ok
}
})
running(TestServer(testServerPort, app)) {
await(makeRequest(app)(hostUrl))
}
val (request, body) = await(promise.future)
(request, body, hostUrl)
}
}
Other Play Framework source code examplesHere is a short list of links related to this Play Framework OAuthSpec.scala source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.