This commit is contained in:
Erik C. Thauvin 2022-01-03 12:22:07 -08:00
parent c0fc862bb4
commit 191b09060b
6 changed files with 132 additions and 96 deletions

9
.idea/bitly-shorten.iml generated Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/bitly-shorten.iml" filepath="$PROJECT_DIR$/.idea/bitly-shorten.iml" />
</modules>
</component>
</project>

View file

@ -50,7 +50,7 @@ import java.util.logging.Logger
open class Utils private constructor() {
companion object {
/** The logger instance. */
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.name) }
val logger: Logger by lazy { Logger.getLogger(Utils::class.java.name) }
/**
* Executes an API call.
@ -70,8 +70,7 @@ open class Utils private constructor() {
): CallResponse {
val response = CallResponse()
if (validateCall(accessToken, endPoint)) {
val apiUrl = endPoint.toHttpUrlOrNull()
if (apiUrl != null) {
endPoint.toHttpUrlOrNull()?.let { apiUrl ->
val builder = when (method) {
Methods.POST, Methods.PATCH -> {
val formBody = JSONObject(params).toString()
@ -117,8 +116,7 @@ open class Utils private constructor() {
}
private fun parseBody(endPoint: String, result: Response): String {
val body = result.body?.string()
if (body != null) {
result.body?.string()?.let { body ->
if (!result.isSuccessful && body.isNotEmpty()) {
try {
with(JSONObject(body)) {

View file

@ -32,16 +32,14 @@
package net.thauvin.erik.bitly
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
import org.json.JSONObject
import org.junit.Before
import java.io.File
import java.util.logging.Level
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue
import kotlin.test.*
class BitlyTest {
private val bitly = with(File("local.properties")) {
@ -89,6 +87,11 @@ class BitlyTest {
assertEquals(shortUrl, bitly.bitlinks().shorten(shortUrl))
}
@Test
fun `endPoint should be specified`() {
assertFalse(bitly.call("").isSuccessful)
}
@Test
fun `shorten = expand`() {
val shortUrl = bitly.bitlinks().shorten(longUrl, domain = "bit.ly")
@ -102,7 +105,11 @@ class BitlyTest {
@Test
fun `get user`() {
assertTrue(bitly.call("/user".toEndPoint(), method = Methods.GET).isSuccessful)
assertTrue(bitly.call("user".toEndPoint(), method = Methods.GET).isSuccessful)
assertTrue(
Utils.call(bitly.accessToken, "/user".toEndPoint(), method = Methods.GET).isSuccessful,
"call(/user)"
)
}
@Test
@ -154,6 +161,10 @@ class BitlyTest {
@Test
fun `create bitlink`() {
assertTrue(
bitly.bitlinks().create(long_url = longUrl).matches("https://\\w+.\\w{2}/\\w{7}".toRegex()),
"just long_url"
)
assertEquals(
shortUrl,
bitly.bitlinks().create(
@ -173,7 +184,17 @@ class BitlyTest {
bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"))
)
assertTrue(
bl.update(shortUrl, tags = emptyArray(), toJson = true).contains("\"tags\":[]"), "update tags toJson"
)
bl.update(shortUrl, link = longUrl)
assertTrue(bl.lastCallResponse.isUnprocessableEntity, "422 Unprocessable")
}
@Test
fun `validate URL`() {
assertTrue("https://www.example.com".isValidUrl(), "valid url")
assertFalse("this is a test".isValidUrl(), "invalid url")
}
}