Skip to content

Instantly share code, notes, and snippets.

@TheDIM47
Created August 22, 2015 16:56
Show Gist options
  • Save TheDIM47/778cb826bf588d778857 to your computer and use it in GitHub Desktop.
Save TheDIM47/778cb826bf588d778857 to your computer and use it in GitHub Desktop.
Unshort URL with Scala and Dispatch
import java.net.URI
import org.scalatest._
import scala.concurrent.duration._
class UnshortTest extends FlatSpec with Matchers {
def unshort(u: String, w: Duration): Option[URI] = {
import dispatch._, Defaults._
import scala.concurrent.Await
val r = Http.configure(_ setFollowRedirects true)(url(u) OK as.Response(_.getUri))
try {
Some(Await.result(r, w))
} catch {
case _: Throwable => None
}
}
it should "return unshorted url during 10 seconds" in {
val url = "https://en.wikipedia.org/wiki/Iceland"
assert(unshort("https://goo.gl/Plnzty", 10.seconds).get.toString == url)
}
it should "fail on bad url" in {
assert(unshort("https://goo.gle/Plnzty123", 10.seconds) == None)
}
it should "fail with URISyntaxException on empty url" in {
intercept [java.net.URISyntaxException] {
unshort("", 10.seconds)
}
}
it should "return same url" in {
val url = "http://slickdeals.net/newsearch.php?searchin=first&forumchoice%5B%5D=25&rss=1"
assert(unshort(url, 10.seconds).get.toString == url)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment