Created
August 22, 2015 16:56
-
-
Save TheDIM47/778cb826bf588d778857 to your computer and use it in GitHub Desktop.
Unshort URL with Scala and Dispatch
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
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