alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (OAuthSpec.scala)

This example Play Framework source code file (OAuthSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, array, concurrent, fakeapplication, lib, library, oauth, oauthcalculator, oauthspec, play, play framework, playspecification, promise, requestheader, requesttoken, string, test

The OAuthSpec.scala Play Framework example source code

/*
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package play.libs.oauth

import play.api.mvc._
import play.api.test._

import scala.concurrent.Promise
import play.libs.F
import play.libs.oauth.OAuth._
import play.libs.ws.WS
import play.api.libs.oauth.OAuthRequestVerifier

class OAuthSpec extends PlaySpecification {

  sequential

  val javaConsumerKey = new ConsumerKey("someConsumerKey", "someVerySecretConsumerSecret")
  val javaRequestToken = new RequestToken("someRequestToken", "someVerySecretRequestSecret")
  val oauthCalculator = new OAuthCalculator(javaConsumerKey, javaRequestToken)

  val consumerKey = play.api.libs.oauth.ConsumerKey(javaConsumerKey.key, javaConsumerKey.secret)
  val requestToken = play.api.libs.oauth.RequestToken(javaRequestToken.token, javaRequestToken.secret)

  "OAuth" should {
    "sign a simple get request" in {
      val (request, body, hostUrl) = receiveRequest { 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 { hostUrl =>
        WS.url(hostUrl + "/foo").setQueryParameter("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 { hostUrl =>
        WS.url(hostUrl + "/foo").sign(oauthCalculator).setContentType("application/x-www-form-urlencoded")
          .post("param=paramValue")
      }
      OAuthRequestVerifier.verifyRequest(request, body, hostUrl, consumerKey, requestToken)
    }
  }

  def receiveRequest(makeRequest: String => F.Promise[_]): (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)) {
      makeRequest(hostUrl).get(30000l)
    }
    val (request, body) = await(promise.future)
    (request, body, hostUrl)
  }
}

Other Play Framework source code examples

Here 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

 

new blog posts

 

Copyright 1998-2021 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.