Skip to content

Commit

Permalink
Use UrlPathHelper to resolve request URI (#837)
Browse files Browse the repository at this point in the history
* Use UrlPathHelper to resolve request URI

* Use the pre-installed Chrome driver when running on GitHub

The GitHub virtual environment already provides a Chrome and a matching Chrome driver.

For installed software see:
https://github.com/actions/virtual-environments/blob/ubuntu20/20220207.1/images/linux/Ubuntu2004-Readme.md#environment-variables-1

For available environment variables see:
https://docs.github.com/en/actions/learn-github-actions/environment-variables

Thanks to @darxriggs for suggesting the same in grails/grails-cache#123 plugin.

* Downgrade Selenium version

* Use Correct Selenium Versions
  • Loading branch information
puneetbehl authored Nov 17, 2022
1 parent e8f9770 commit 6c9677d
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 25 deletions.
9 changes: 2 additions & 7 deletions examples/functional-test-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ buildscript {
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "org.grails.plugins:hibernate5:${gormVersion-".RELEASE"}"
classpath "gradle.plugin.com.energizedwork.webdriver-binaries:webdriver-binaries-gradle-plugin:$webdriverBinariesVersion"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:$assetPipelineVersion"
classpath "gradle.plugin.com.energizedwork.webdriver-binaries:webdriver-binaries-gradle-plugin:$webdriverBinariesVersion"
}
}

Expand All @@ -18,7 +18,6 @@ apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"com.energizedwork.webdriver-binaries"
apply plugin:"asset-pipeline"
apply plugin:"org.grails.grails-gsp"

Expand Down Expand Up @@ -69,13 +68,9 @@ dependencies {
compile project(':spring-security-core')
}

webdriverBinaries {
chromedriver '77.0.3865.40'
geckodriver '0.23.0'
}

apply from: "${rootProject.projectDir}/gradle/integrationTest.gradle"
apply from: "${rootProject.projectDir}/gradle/testVerbose.gradle"
apply from: "${rootProject.projectDir}/gradle/geb.gradle"

bootRun {
jvmArgs('-Dspring.output.ansi.enabled=always')
Expand Down
2 changes: 0 additions & 2 deletions examples/integration-test-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ dependencies {
testCompile "org.grails:grails-gorm-testing-support"
testCompile "org.grails.plugins:geb"
testCompile "org.grails:grails-web-testing-support"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
compile project(':spring-security-core')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ dependencies {
runtime "org.apache.tomcat:tomcat-jdbc"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:$assetPipelineVersion"
testCompile "org.grails:grails-gorm-testing-support"
testCompile "org.grails.plugins:geb"
testCompile "org.grails:grails-web-testing-support"
testCompile "org.seleniumhq.selenium:selenium-api:2.53.1"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
testRuntime "org.seleniumhq.selenium:htmlunit-driver:2.57.0"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.57.0"
compile project(':spring-security-core')
}

apply from: "${rootProject.projectDir}/gradle/ssc.gradle"
apply from: "${rootProject.projectDir}/gradle/geb.gradle"
apply from: "${rootProject.projectDir}/gradle/testVerbose.gradle"

bootRun {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ dependencies {
testCompile "org.grails:grails-gorm-testing-support"
testCompile "org.grails.plugins:geb"
testCompile "org.grails:grails-web-testing-support"
testCompile "org.seleniumhq.selenium:selenium-api:2.53.1"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
testRuntime "org.seleniumhq.selenium:htmlunit-driver:2.57.0"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.57.0"
compile project(':spring-security-core')
}

apply from: "${rootProject.projectDir}/gradle/geb.gradle"
apply from: "${rootProject.projectDir}/gradle/testVerbose.gradle"

bootRun {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ javaxServletApiVersion=3.1.0
grailsTestingSupportVersion=1.1.1
assetPipelineVersion=2.14.2
gebVersion=2.2
seleniumVersion=3.12.0
seleniumVersion=3.141.59
webdriverBinariesVersion=1.4
chromeDriverVersion=77.0.3865.40
chromeDriverVersion=107.0.5304.62
geckodriverVersion=0.23.0
seleniumSafariDriverVersion=3.14.0
micronautVersion=1.0.1
Expand Down
13 changes: 11 additions & 2 deletions gradle/geb.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
apply plugin: "com.energizedwork.webdriver-binaries"

dependencies {
testCompile("org.grails.plugins:geb") {
exclude group: 'org.gebish', module: 'geb-spock'
Expand All @@ -16,9 +18,16 @@ dependencies {
tasks.withType(Test) {
systemProperty "geb.env", System.getProperty('geb.env')
systemProperty "geb.build.reportsDir", reporting.file("geb/integrationTest")
if (!System.getenv().containsKey('CI')) {
systemProperty 'webdriver.chrome.driver', System.getProperty('webdriver.chrome.driver')
systemProperty 'webdriver.gecko.driver', System.getProperty('webdriver.gecko.driver')
} else {
systemProperty 'webdriver.chrome.driver', "${System.getenv('CHROMEWEBDRIVER')}/chromedriver"
systemProperty 'webdriver.gecko.driver', "${System.getenv('GECKOWEBDRIVER')}/geckodriver"
}
}

webdriverBinaries {
chromedriver "$chromeDriverVersion"
geckodriver "$geckodriverVersion"
chromedriver "${chromeDriverVersion}"
geckodriver "${geckodriverVersion}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package grails.plugin.springsecurity.web.access.intercept

import groovy.util.logging.Slf4j
import org.springframework.web.util.UrlPathHelper

import java.util.concurrent.CopyOnWriteArrayList

Expand Down Expand Up @@ -48,6 +49,7 @@ abstract class AbstractFilterInvocationDefinition implements FilterInvocationSec
protected static final Collection<ConfigAttribute> DENY = Collections.singletonList((ConfigAttribute)new SecurityConfig('_DENY_'))
protected static final Collection<ConfigAttribute> ALLOW404 = Collections.singletonList((ConfigAttribute)new SecurityConfig('permitAll'))
protected static final String ERROR404 = '__ERROR404__'
protected static UrlPathHelper urlPathHelper = new UrlPathHelper()

protected RoleVoter roleVoter
protected AuthenticatedVoter authenticatedVoter
Expand Down Expand Up @@ -92,7 +94,10 @@ abstract class AbstractFilterInvocationDefinition implements FilterInvocationSec
}

protected String determineUrl(FilterInvocation filterInvocation) {
lowercaseAndStripQuerystring calculateUri(filterInvocation.httpRequest)
final HttpServletRequest request = filterInvocation.httpRequest
String url = urlPathHelper.getLookupPathForRequest(request)
url = stripContextPath(url, request)
lowercaseAndStripQuerystring(url)
}

protected boolean stopAtFirstMatch() {
Expand Down Expand Up @@ -173,10 +178,23 @@ abstract class AbstractFilterInvocationDefinition implements FilterInvocationSec
Collections.unmodifiableCollection all
}

/**
*
* @param request The {@link HttpServletRequest} to calculate the URI for
* @return The URI for the request, or {@link #ERROR404} if the request is for a 404
* @deprecated Use {@link org.springframework.web.util.UrlPathHelper#getRequestUri(HttpServletRequest)} and {@link #stripContextPath(String, HttpServletRequest)} instead
*/
@Deprecated
protected String calculateUri(HttpServletRequest request) {
String url = request.requestURI.substring(request.contextPath.length())
int semicolonIndex = url.indexOf(';')
semicolonIndex == -1 ? url : url.substring(0, semicolonIndex)
stripContextPath(urlPathHelper.getRequestUri(request), request)
}

protected String stripContextPath(String uri, HttpServletRequest request) {
String contextPath = request.contextPath
if (contextPath && uri.startsWith(contextPath)) {
uri = uri.substring(contextPath.length())
}
uri
}

protected String lowercaseAndStripQuerystring(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class AnnotationFilterInvocationDefinition extends AbstractFilterInvocationDefin
HttpServletRequest request = filterInvocation.httpRequest
HttpServletResponse response = filterInvocation.httpResponse

String requestUrl = calculateUri(request)
String requestUrl = urlPathHelper.getRequestUri(request)
requestUrl = stripContextPath(requestUrl, request)

GrailsWebRequest existingRequest
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ class RequestmapFilterInvocationDefinitionSpec extends AbstractUnitSpec {
expect:
fid.supports FilterInvocation
}

void "test determineUrl with segmented url"() {
when:
def chain = new MockFilterChain()
request.contextPath = '/context'
request.requestURI = '/context/foo;f1=WWW/bar;s1=1;s2=2/index.html;i1=1;i2=2'

then:
'/foo/bar/index.html' == fid.determineUrl(new FilterInvocation(request, response, chain))
}

void "test determineUrl context path and query params are stripped from url"() {
when:
def chain = new MockFilterChain()
request.contextPath = '/context'
request.requestURI = '/context/foo/bar/index.html?type=HelloWorld'

then:
'/foo/bar/index.html' == fid.determineUrl(new FilterInvocation(request, response, chain))
}
}

@Slf4j
Expand Down

0 comments on commit 6c9677d

Please sign in to comment.