Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9290377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:24:21+00:00 2026-06-18T20:24:21+00:00

I want to test a controller action using Action composition. Here’s an example of

  • 0

I want to test a controller action using Action composition.
Here’s an example of the composed action and its test code.

The Secured trait:

trait Secured {
   def username(request: RequestHeader) = request.session.get(Security.username)
   def onUnauthorized(request: RequestHeader) = Results.Redirect(routes.Auth.login)

   def withAuth(f: => String => Request[AnyContent] => Result) = {
      Security.Authenticated(username, onUnauthorized) { user =>
        Action(request => f(user)(request))
   }
}

The controller:

MyController extends Contrller with Secured {
   def simple = Action { Ok("ok") }
   def simpleWithauth = withAuth { implicit username => implicit request=> Ok("ok") }
}

The test code:

// This work fine
val result1 = controller.simple()(FakeRequest())

// This wont compile
val result2 = controller.simpleWithAuth()(FakeRequest())

The latter would require a Request[Action[AnyContent], AnyContent]
but FakeRequest returns a Request[AnyContent]

Any pointers on how to create a fake request of the appropriate type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:24:22+00:00Added an answer on June 18, 2026 at 8:24 pm

    This is what I had to do to test secured action

    def wrappedActionResult[A](wrapped: Action[(Action[A], A)], request: Request[A]): Result = wrapped.parser(request).run.await.get match {
      case Left(errorResult) => errorResult
      case Right((innerAction, _)) => innerAction(request)
    }
    

    and the test

        running(app) {
          val result = wrappedActionResult(FakeController().securedAction, invalidRequest)
          status(result) must_== UNAUTHORIZED
          contentAsString(result) must_== "must be authenticated"
        }
    

    That doesn’t work with Play 2.1 though, types have been changed…

    UPDATE:
    in Play 2.1 it’s even easier

    I’ve added some sugar

    implicit class ActionExecutor(action: EssentialAction) {
      def process[A](request: Request[A]): Result = concurrent.Await.result(action(request).run, Duration(1, "sec"))
    }
    

    and the test now looks like this

      running(app) {
        val result = FakeController().securedAction process invalidRequest
        status(result) must_== UNAUTHORIZED
        contentAsString(result) must_== "must be authenticated"
      }
    

    UPDATE: here is a related blog post i wrote sometime ago
    http://www.daodecode.com/blog/2013/03/08/testing-security-dot-authenticated-in-play-2-dot-0-and-2-dot-1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in my controller and want to test this code line
I am trying to test my controller's create action. I am using Devise for
I want to unit test the following ASP.NET MVC controller Index action. What do
I have a file upload application. I want to test my Upload controller. For
I want to test the login() action in my UsersController.php <?php class UsersController extends
I am using Rails 3.1.0 and Rspec 2.12.2. I want to have an action
I am trying to test a controller action that allows edition of user profiles.
I want to test the OnException , OnActionExecuted event of an MVC controller. If
While using form_remote_tag(options = {}, &block) form_remote_tag :html => { :action => url_for(:controller =>
I'm trying to write a test for an ASP.Net MVC controller action. I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.