Last active
August 29, 2015 14:14
-
-
Save mikeyhu/f703494a0ee8397c10a5 to your computer and use it in GitHub Desktop.
Check for 302 Status using groovy HttpURLClient and copy cookies from Selenium webdriver as well
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package courtdocuments.helpers | |
import groovyx.net.http.HttpResponseDecorator | |
import groovyx.net.http.HttpURLClient | |
import org.openqa.selenium.WebDriver | |
import static groovyx.net.http.ContentType.TEXT | |
import static groovyx.net.http.Method.GET | |
/* | |
Selenium doesnt return statuscode/location so use this instead. | |
Inspired by: http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/ | |
*/ | |
public class URLStatusChecker { | |
static checkHTTPStatusCode(WebDriver driver, String uri) { | |
def cookies = mimicCookieState(driver.manage().getCookies()) | |
def http = new HttpURLClient() | |
http.followRedirects = false | |
HttpResponseDecorator resp = http.request(method: GET, url: uri, contentType: TEXT, headers: cookies) | |
[ | |
resp.getStatus(), | |
resp.getHeaders().find { h -> h.name == "Location" }?.value | |
] | |
} | |
static private def mimicCookieState(Set seleniumCookieSet) { | |
seleniumCookieSet.inject([:]) { acc, seleniumCookie -> | |
if (seleniumCookie.getName() == "MMSESSION") { | |
println seleniumCookie.toString() | |
acc.put("Cookie", seleniumCookie.toString()) | |
} | |
acc | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment