Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix for GRAILS-8248 "Unit test withFilters not working for grails 2.0…
….0.RC1" Filter unit testing support was swallowing exceptions giving the impression that it was not working, when there were in fact problems in the user code
- Loading branch information
1 parent
804b865
commit fa87b29
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
...ite-web/src/test/groovy/org/codehaus/groovy/grails/web/filters/FiltersUnitTestSpec.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.codehaus.groovy.grails.web.filters | ||
|
||
import javax.servlet.http.HttpServletResponse | ||
import grails.artefact.Artefact | ||
import grails.test.mixin.TestFor | ||
import grails.test.mixin.Mock | ||
import spock.lang.Specification | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: graemerocher | ||
* Date: 11/9/11 | ||
* Time: 3:48 PM | ||
* To change this template use File | Settings | File Templates. | ||
*/ | ||
@TestFor(UserController) | ||
@Mock(AuthenticationFilters) | ||
class FiltersUnitTestSpec extends Specification{ | ||
void "test filters are applied for a unit test"() { | ||
when:"A filter is used around a controller" | ||
params.username = '' | ||
withFilters(action: "create") { | ||
controller.create() | ||
|
||
} | ||
then:"Check that the filter logic is applied" | ||
400 == response.status | ||
} | ||
|
||
void "test filters relay exceptions"() { | ||
when:"A filter is used around a controller" | ||
params.username = '' | ||
withFilters(action: "update") { | ||
controller.update() | ||
} | ||
then:"Check that an exception is rethrown" | ||
RuntimeException e = thrown() | ||
e.message == "bad things happened" | ||
} | ||
|
||
} | ||
|
||
@Artefact("Controller") | ||
class UserController { | ||
|
||
def create() {} | ||
def update() {} | ||
} | ||
|
||
@Artefact("Filters") | ||
class AuthenticationFilters { | ||
def filters = { | ||
create(controller: 'user', action: 'create') { | ||
before = { | ||
if (params.username == '') { | ||
render(status: HttpServletResponse.SC_BAD_REQUEST) | ||
return false | ||
} | ||
} | ||
} | ||
create(controller: 'user', action: 'update') { | ||
before = { | ||
if (params.username == '') { | ||
throw new RuntimeException("bad things happened") | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
} |