Skip to content

Commit 6a10193

Browse files
authored
Merge pull request #15227 from Noirtam/issues_15132
Issues 15132
2 parents 9fce3fe + 53eb5f9 commit 6a10193

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

grails-web-url-mappings/src/main/groovy/grails/web/mapping/ResponseRedirector.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ class ResponseRedirector {
9393

9494
boolean permanent = getBooleanArgument(ARGUMENT_PERMANENT, arguments)
9595
boolean moved = getBooleanArgument(ARGUMENT_MOVED, arguments, true)
96+
boolean absolute = getBooleanArgument(ARGUMENT_ABSOLUTE, arguments, true)
9697

9798
final Map namedParameters = new LinkedHashMap<>(arguments)
9899
// we generate a relative link with no context path so that the absolute can be calculated by combining the serverBaseURL
99100
// which includes the contextPath
100-
namedParameters.put(LinkGenerator.ATTRIBUTE_CONTEXT_PATH, BLANK)
101-
102-
boolean absolute = getBooleanArgument(ARGUMENT_ABSOLUTE, arguments, true)
101+
// @Issue('11673')
102+
if (absolute) namedParameters.put(LinkGenerator.ATTRIBUTE_CONTEXT_PATH, BLANK)
103103

104104
// If the request parameters contain "keepParamsWhenRedirect = true", then we add the original params. The
105105
// new attribute can be used from UrlMappings to redirect from old URLs to new ones while keeping the params
@@ -129,7 +129,7 @@ class ResponseRedirector {
129129
if (absolute) {
130130
redirectURI = processedActualUri.contains('://') ? processedActualUri : serverBaseURL + processedActualUri
131131
} else {
132-
redirectURI = linkGenerator.contextPath + processedActualUri
132+
redirectURI = processedActualUri
133133
}
134134

135135
String redirectUrl = useJessionId ? response.encodeRedirectURL(redirectURI) : redirectURI

grails-web-url-mappings/src/test/groovy/grails/web/mapping/RedirectNonAbsoluteURISpec.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,26 @@ class RedirectNonAbsoluteURISpec extends AbstractUrlMappingsSpec {
114114
cleanup:
115115
RequestContextHolder.setRequestAttributes(null)
116116
}
117+
118+
@Issue('15132')
119+
void 'An "absolute=false" redirect not added context-path with url params'() {
120+
given:
121+
def linkGenerator = getLinkGeneratorWithContextPath {
122+
"/$controller/$action?/$id?"()
123+
}
124+
def responseRedirector = new ResponseRedirector(linkGenerator)
125+
HttpServletRequest request = Mock(HttpServletRequest) { lookup() >> GrailsWebMockUtil.bindMockWebRequest() }
126+
HttpServletResponse response = Mock(HttpServletResponse)
127+
128+
when: 'redirecting with absolute=false where context-path is set'
129+
String url = linkGenerator.link(controller:"test", action: "foo")
130+
responseRedirector.redirect(request, response, [url:url, absolute: false])
131+
132+
then: 'the partial URL includes context-path'
133+
1 * response.setHeader(HttpHeaders.LOCATION, "${CONTEXT_PATH}/test/foo")
134+
135+
cleanup:
136+
RequestContextHolder.setRequestAttributes(null)
137+
}
138+
117139
}

0 commit comments

Comments
 (0)