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 6661915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:20:05+00:00 2026-05-26T02:20:05+00:00

Update : I have changed this question to be about the specific problem I

  • 0

Update: I have changed this question to be about the specific problem I am having. This is because unit testing of Filters will be supported in Grails 2.0 so hopefully the documentation will be better then.

I am trying to write unit tests for the filters I set up to implement Shiro security in my grails app. I am using Grails 1.3.7 and wont be able to use 2.0 for a while (if ever) for this particular project.

The idea behind my filter is that I need to give anonymous access to a number or controller/action combinations but protect access to the others. I also want it to fail safe, i.e. if you forget to explicitly allow access then access is prohibited.

The filter class

class SecurityFilters {
    def filters = {

        homeAccess(controller: "home", action: "*") {
            before = {

                // Allow all access
                request.accessAllowed = true
            }
        }

        serverAccess(controller: "server", action: "list") {
            before = {

                // Allow all access
                request.accessAllowed = true
            }
        }

        layerAccess(controller: "layer", action: "list|listBaseLayersAsJson|listNonBaseLayerAsJson|showLayerByItsId") {
            before = {

                // Allow all access
                request.accessAllowed = true
            }
        }

        all(uri: "/**") {
            before = {

                // Check if request has been allowed by another filter
                if (request.accessAllowed) return true            

                // Ignore direct views (e.g. the default main index page).
                if (!controllerName) return true

                // Access control by convention.
                accessControl(auth: false)
            }
        }
    }
}

The Unit tests

import org.codehaus.groovy.grails.plugins.web.filters.FilterConfig

class SecurityFiltersTests extends FiltersUnitTestCase {

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testHomeControllerFilter() {

        checkFilter('homeAccess')
    }

    void testServerControllerFilter() {

        checkFilter('serverAccess')
    }

    void testLayerControllerFilter() {

        checkFilter('layerAccess')
    }

    void testAllFilter() {

        assertTrue "Write tests", false
    }

    void checkFilter(String filterName) {

        FilterConfig filter = initFilter(filterName)
        assertNotNull filterName + " filter should exist", filter
        assertExistsBefore(filterName)

        assertEquals "accessAllowed should be null to start with", null, filter.request.accessAllowed

        // Run filter
        filter.before()

        assertEquals "accessAllowed should be true now", true, filter.request.accessAllowed
    }
}

The Exception

The problem is that when these test are run I get the following exception:

No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at grails.test.MockUtils$_addCommonWebProperties_closure32.doCall(MockUtils.groovy:316)
    at shiro.SecurityFilters$_closure1_closure5_closure12.doCall(SecurityFilters.groovy:40)
    at shiro.SecurityFilters$_closure1_closure5_closure12.doCall(SecurityFilters.groovy)
    at shiro.SecurityFiltersTests.checkFilter(SecurityFiltersTests.groovy:92)
    at shiro.SecurityFiltersTests$checkFilter.callCurrent(Unknown Source)
    at shiro.SecurityFiltersTests.testLayerControllerFilter(SecurityFiltersTests.groovy:65)

Additionally I have placed the following line in the Unit test:

println "filter.request: " + filter.request

Which prints the following:

filter.request: org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletRequest@2914cca4

So it certainly seems to be using a mock request object.

So, the questions.

Am I using FiltersUnitTestCase correctly to execute my filters?

And, if so:

Why am I experiencing this exception?

  • 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-05-26T02:20:06+00:00Added an answer on May 26, 2026 at 2:20 am

    A reminder to the rules of debugging for everyone out there: Keep removing code until you find the exact line the problem is on. Even if it is obvious that the line is not causing the problem. Because: sometimes it actually is.

    As part of my debugging work I had the following line of code in my filter:

    println "controllerName = '${controllerName}', actionName = '${actionName}'"
    

    This was directly before the line:

    request.accessAllowed = true
    

    I had taken it out of the code pasted into this question to try and keep it tidy, but apparently I hadn’t tried commenting it out when running the tests. Sorry to anyone who looked-into that code and couldn’t find a problem. You were right, the problem wasn’t in any of the code I provided.

    So the answer is you may see the exception reported here if you try and access the controllerName or actionName variables.

    The solution is to use the setControllerName() and setActionName() methods in FiltersUnitTestCase before executing any filter that might reference controllerName or actionName.

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

Sidebar

Related Questions

NOTE: This question changed a little as I learned more about the problem, so
[ Update: Changed question title to be more specific] Sorry if I didn't make
ok. I have a question about use my data model in UI. problem is
I have a listview which has its datasource changed after update of a search
I have a question about why some SQL (running on SQL Server 2005) is
Last Updated: 2009-08-11 2:30pm EDT A few days ago I posted this question about
[UPDATE] Chosen approach is below, as a response to this question Hi, I' ve
[UPDATE] (when you read my question this is good to know) I had a
I have a design question about the communication between the model and the presenter
I have another question about binding using C# and the entity framework. Here, I'm

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.