Skip to content

Instantly share code, notes, and snippets.

@mikeyhu
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeyhu/f703494a0ee8397c10a5 to your computer and use it in GitHub Desktop.
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
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