Skip to content

Commit

Permalink
Fix for 11673 (#11674) (#13356)
Browse files Browse the repository at this point in the history
Fix for #11673 "Partial URI for Location HTTP-header is missing context-path"

Co-authored-by: Anders Aaberg <25567229+andersaaberg@users.noreply.github.com>
  • Loading branch information
puneetbehl and andersaaberg authored Jan 26, 2024
1 parent f0c188b commit 5fac52b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ResponseRedirector {
if (absolute) {
redirectURI = processedActualUri.contains("://") ? processedActualUri : serverBaseURL + processedActualUri
} else {
redirectURI = processedActualUri
redirectURI = linkGenerator.contextPath + processedActualUri
}

String redirectUrl = useJessionId ? response.encodeRedirectURL(redirectURI) : redirectURI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ import spock.lang.Specification
*/
abstract class AbstractUrlMappingsSpec extends Specification{

static final String CONTEXT_PATH = 'app-context'

def setup() {
WebUtils.clearGrailsWebRequest()
}

LinkGenerator getLinkGeneratorWithContextPath(Closure mappings) {
LinkGeneratorFactory linkGeneratorFactory = new LinkGeneratorFactory()
linkGeneratorFactory.contextPath = CONTEXT_PATH
linkGeneratorFactory.create(mappings)
}
LinkGenerator getLinkGenerator(Closure mappings) {
new LinkGeneratorFactory().create(mappings)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,25 @@ class RedirectNonAbsoluteURISpec extends AbstractUrlMappingsSpec {
cleanup:
RequestContextHolder.setRequestAttributes(null)
}

@Issue('11673')
void 'An "absolute=false" redirect includes context-path in Location header'() {
given:
def linkGenerator = getLinkGeneratorWithContextPath {
"/$controller/$action?/$id?"()
}
def responseRedirector = new ResponseRedirector(linkGenerator)
HttpServletRequest request = Mock(HttpServletRequest) { lookup() >> GrailsWebMockUtil.bindMockWebRequest() }
HttpServletResponse response = Mock(HttpServletResponse)

when: 'redirecting with absolute=false where context-path is set'
responseRedirector.redirect(request, response, [controller: 'test', action: 'foo', absolute: false])

then: 'the partial URI includes context-path'
1 * response.setStatus(302)
1 * response.setHeader(HttpHeaders.LOCATION, CONTEXT_PATH + '/test/foo')

cleanup:
RequestContextHolder.setRequestAttributes(null)
}
}

0 comments on commit 5fac52b

Please sign in to comment.