Skip to content

Commit

Permalink
Fix for assertReverseUrlMapping to properly handle http method in url…
Browse files Browse the repository at this point in the history
… mappings.
  • Loading branch information
tlefevre committed May 26, 2016
1 parent 277fc99 commit 74ddacf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Expand Up @@ -274,17 +274,17 @@ class UrlMappingsUnitTestMixin extends ControllerUnitTestMixin {
UrlMappingsHolder mappingsHolder = applicationContext.getBean("grailsUrlMappingsHolder", UrlMappingsHolder)
def controller = assertions.controller
def action = assertions.action
def method = assertions.method
def params = [:]
if (paramAssertions) {
paramAssertions.delegate = params
paramAssertions.resolveStrategy = Closure.DELEGATE_ONLY
paramAssertions.call()
}
def urlCreator = mappingsHolder.getReverseMapping(controller, action, params)
def urlCreator = mappingsHolder.getReverseMapping(controller, action, null, null, method, params)
assertNotNull("could not create reverse mapping of '$url' for {controller = $controller, action = $action, params = $params}", urlCreator)
def createdUrl = urlCreator.createRelativeURL(controller, action, params, "UTF-8")
assertEquals("reverse mapping assertion for {controller = $controller, action = $action, params = $params}", url, createdUrl)

}

private GrailsControllerClass getControllerClass(controller) {
Expand Down
Expand Up @@ -3,14 +3,10 @@ package grails.test.mixin
import grails.artefact.Artefact
import grails.rest.RestfulController
import grails.test.mixin.web.UrlMappingsUnitTestMixin
import grails.web.Action
import junit.framework.AssertionFailedError
import junit.framework.ComparisonFailure

import org.junit.Test
import org.springframework.web.context.WebApplicationContext
import spock.lang.Issue

/**
* Tests for the UrlMappingsTestMixin class
*/
Expand Down Expand Up @@ -157,6 +153,19 @@ class UrlMappingsTestMixinTests {
}
}

@Test
void testMethodMappings() {
mockController(UserController)
mockUrlMappings(MethodTestUrlMappings)

request.method = 'GET'
assertUrlMapping('/users/timmy', controller: 'user', action: 'show', method: 'get') { name = 'timmy' }
assertUrlMapping('/users', controller: 'user', action: 'list', method: 'get')

request.method = 'PUT'
assertUrlMapping('/users/timmy', controller: 'user', action: 'update', method: 'put') { name = 'timmy' }
}

@Test
@Issue('https://github.com/grails/grails-core/issues/9065')
void testResourcesUrlMapping() {
Expand Down Expand Up @@ -212,6 +221,9 @@ class GrailsUrlMappingsTestCaseFakeController {
@Artefact("Controller")
class UserController {
def publicProfile() {}
def update() {}
def show() {}
def list() {}
}

class MyUrlMappings {
Expand Down Expand Up @@ -250,6 +262,14 @@ class GRAILS9110UrlMappings {
}
}

class MethodTestUrlMappings {
static mappings = {
"/users/$name"(controller: 'user', action: 'update', method: 'put')
"/users/$name"(controller: 'user', action: 'show', method: 'get')
"/users"(controller: 'user', action: 'list', method: 'get')
}
}

@Artefact("Controller")
class PersonController extends RestfulController<String> {
PersonController() {
Expand Down

0 comments on commit 74ddacf

Please sign in to comment.