Skip to content

Commit 74ddacf

Browse files
committed
Fix for assertReverseUrlMapping to properly handle http method in url mappings.
1 parent 277fc99 commit 74ddacf

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

grails-plugin-testing/src/main/groovy/grails/test/mixin/web/UrlMappingsUnitTestMixin.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,17 @@ class UrlMappingsUnitTestMixin extends ControllerUnitTestMixin {
274274
UrlMappingsHolder mappingsHolder = applicationContext.getBean("grailsUrlMappingsHolder", UrlMappingsHolder)
275275
def controller = assertions.controller
276276
def action = assertions.action
277+
def method = assertions.method
277278
def params = [:]
278279
if (paramAssertions) {
279280
paramAssertions.delegate = params
280281
paramAssertions.resolveStrategy = Closure.DELEGATE_ONLY
281282
paramAssertions.call()
282283
}
283-
def urlCreator = mappingsHolder.getReverseMapping(controller, action, params)
284+
def urlCreator = mappingsHolder.getReverseMapping(controller, action, null, null, method, params)
284285
assertNotNull("could not create reverse mapping of '$url' for {controller = $controller, action = $action, params = $params}", urlCreator)
285286
def createdUrl = urlCreator.createRelativeURL(controller, action, params, "UTF-8")
286287
assertEquals("reverse mapping assertion for {controller = $controller, action = $action, params = $params}", url, createdUrl)
287-
288288
}
289289

290290
private GrailsControllerClass getControllerClass(controller) {

grails-test-suite-uber/src/test/groovy/grails/test/mixin/UrlMappingsTestMixinTests.groovy

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ package grails.test.mixin
33
import grails.artefact.Artefact
44
import grails.rest.RestfulController
55
import grails.test.mixin.web.UrlMappingsUnitTestMixin
6-
import grails.web.Action
7-
import junit.framework.AssertionFailedError
86
import junit.framework.ComparisonFailure
9-
107
import org.junit.Test
118
import org.springframework.web.context.WebApplicationContext
129
import spock.lang.Issue
13-
1410
/**
1511
* Tests for the UrlMappingsTestMixin class
1612
*/
@@ -157,6 +153,19 @@ class UrlMappingsTestMixinTests {
157153
}
158154
}
159155

156+
@Test
157+
void testMethodMappings() {
158+
mockController(UserController)
159+
mockUrlMappings(MethodTestUrlMappings)
160+
161+
request.method = 'GET'
162+
assertUrlMapping('/users/timmy', controller: 'user', action: 'show', method: 'get') { name = 'timmy' }
163+
assertUrlMapping('/users', controller: 'user', action: 'list', method: 'get')
164+
165+
request.method = 'PUT'
166+
assertUrlMapping('/users/timmy', controller: 'user', action: 'update', method: 'put') { name = 'timmy' }
167+
}
168+
160169
@Test
161170
@Issue('https://github.com/grails/grails-core/issues/9065')
162171
void testResourcesUrlMapping() {
@@ -212,6 +221,9 @@ class GrailsUrlMappingsTestCaseFakeController {
212221
@Artefact("Controller")
213222
class UserController {
214223
def publicProfile() {}
224+
def update() {}
225+
def show() {}
226+
def list() {}
215227
}
216228

217229
class MyUrlMappings {
@@ -250,6 +262,14 @@ class GRAILS9110UrlMappings {
250262
}
251263
}
252264

265+
class MethodTestUrlMappings {
266+
static mappings = {
267+
"/users/$name"(controller: 'user', action: 'update', method: 'put')
268+
"/users/$name"(controller: 'user', action: 'show', method: 'get')
269+
"/users"(controller: 'user', action: 'list', method: 'get')
270+
}
271+
}
272+
253273
@Artefact("Controller")
254274
class PersonController extends RestfulController<String> {
255275
PersonController() {

0 commit comments

Comments
 (0)