Added expand method.

This commit is contained in:
Erik C. Thauvin 2020-02-26 01:49:16 -08:00
parent 8bd36fc77d
commit 4fa39ba530
2 changed files with 70 additions and 45 deletions

View file

@ -34,34 +34,20 @@ package net.thauvin.erik.bitly
import org.junit.Before
import java.io.File
import java.io.FileInputStream
import java.util.Properties
import java.util.logging.Level
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
fun getKey(key: String): String {
var value = System.getenv(key) ?: ""
if (value.isBlank()) {
val localProps = File("local.properties")
if (localProps.exists())
localProps.apply {
if (exists()) {
FileInputStream(this).use { fis ->
Properties().apply {
load(fis)
value = getProperty(key, "")
}
}
}
}
}
return value
}
class BitlyTest {
private val bitly = Bitly(getKey(Bitly.Constants.ENV_ACCESS_TOKEN))
private val bitly = with(File("local.properties")) {
if (exists()) {
Bitly(toPath())
} else {
Bitly()
}
}
private val blog = "https://erik.thauvin.net/blog"
@Before
fun before() {
@ -76,7 +62,7 @@ class BitlyTest {
if (System.getenv("CI") == "true") {
test.accessToken = ""
}
assertEquals("", test.shorten("https://erik.thauvin.net/blog/"))
assertEquals("", test.shorten(blog))
}
@Test
@ -91,17 +77,18 @@ class BitlyTest {
}
@Test
fun `blog should be valid`() {
assertEquals("http://bit.ly/2SVHsnd", bitly.shorten("https://erik.thauvin.net/blog/", domain = "bit.ly"))
fun `shorten = expand`() {
val shortUrl = bitly.shorten(blog, domain = "bit.ly")
assertEquals(blog, bitly.expand(shortUrl))
}
@Test
fun `blog as json`() {
assertTrue(bitly.shorten("https://erik.thauvin.net/blog/", isJson = true).startsWith("{\"created_at\":"))
fun `as json`() {
assertTrue(bitly.shorten(blog, isJson = true).startsWith("{\"created_at\":"))
}
@Test
fun `get user`() {
assertTrue(bitly.executeCall(bitly.buildEndPointUrl("user"), emptyMap(), Methods.GET).contains("\"login\":"))
assertTrue(bitly.call(bitly.buildEndPointUrl("user"), emptyMap(), Methods.GET).contains("\"login\":"))
}
}