API URL must not be null.

This commit is contained in:
Erik C. Thauvin 2022-01-03 12:47:53 -08:00
parent e17758f71b
commit f9594e9dc3
3 changed files with 59 additions and 67 deletions

View file

@ -35,7 +35,7 @@ import kotlinx.serialization.json.Json
import net.thauvin.erik.semver.Version
import okhttp3.FormBody
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.logging.HttpLoggingInterceptor
@ -45,7 +45,7 @@ import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.util.*
import java.util.Date
import java.util.logging.Level
import java.util.logging.Logger
@ -325,9 +325,8 @@ open class Akismet(apiKey: String) {
* @param trueOnError Set to return `true` on error (IO, empty response, etc.)
*/
@JvmOverloads
fun executeMethod(apiUrl: HttpUrl?, formBody: FormBody, trueOnError: Boolean = false): Boolean {
fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean {
reset()
if (apiUrl != null) {
val request = if (formBody.size == 0) {
Request.Builder().url(apiUrl).header("User-Agent", buildUserAgent()).build()
} else {
@ -356,10 +355,7 @@ open class Akismet(apiKey: String) {
}
}
} catch (e: IOException) {
errorMessage = "An IO error occurred while communicating with the Akismet service."
}
} else {
errorMessage = "Invalid API end point URL."
errorMessage = "An IO error occurred while communicating with the Akismet service: ${e.message}"
}
if (errorMessage.isNotEmpty()) {
@ -384,11 +380,11 @@ open class Akismet(apiKey: String) {
response = ""
}
private fun String.toApiUrl(): HttpUrl? {
private fun String.toApiUrl(): HttpUrl {
return if (this == verifyMethod) {
String.format(apiEndPoint, "", this).toHttpUrlOrNull()
String.format(apiEndPoint, "", this).toHttpUrl()
} else {
String.format(apiEndPoint, "$apiKey.", this).toHttpUrlOrNull()
String.format(apiEndPoint, "$apiKey.", this).toHttpUrl()
}
}

View file

@ -36,9 +36,6 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import javax.servlet.http.HttpServletRequest
import kotlin.collections.Map
import kotlin.collections.emptyMap
import kotlin.collections.iterator
import kotlin.collections.set
private fun String?.ifNull() = this ?: ""

View file

@ -33,7 +33,7 @@
package net.thauvin.erik.akismet
import okhttp3.FormBody
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.testng.Assert.assertEquals
@ -256,7 +256,7 @@ class AkismetTest {
with(akismet) {
assertTrue(
executeMethod(
"https://postman-echo.com/status/200".toHttpUrlOrNull(), emptyFormBody, true
"https://postman-echo.com/status/200".toHttpUrl(), emptyFormBody, true
)
)
var expected = "{\"status\":200}"
@ -268,7 +268,7 @@ class AkismetTest {
assertTrue(
executeMethod(
"https://erik.thauvin.net/blank.html".toHttpUrlOrNull(), emptyFormBody, true
"https://erik.thauvin.net/blank.html".toHttpUrl(), emptyFormBody, true
)
)
expected = ""
@ -282,7 +282,7 @@ class AkismetTest {
with(akismet) {
assertFalse(
executeMethod(
"https://postman-echo.com/response-headers?x-akismet-pro-tip=discard".toHttpUrlOrNull(),
"https://postman-echo.com/response-headers?x-akismet-pro-tip=discard".toHttpUrl(),
emptyFormBody
)
)
@ -317,7 +317,7 @@ class AkismetTest {
fun executeMethodTest() {
with(akismet) {
executeMethod(
"https://$apiKey.rest.akismet.com/1.1/comment-check".toHttpUrlOrNull(),
"https://$apiKey.rest.akismet.com/1.1/comment-check".toHttpUrl(),
FormBody.Builder().apply { add("is_test", "1") }.build()
)
assertTrue(debugHelp.isNotEmpty(), "debugHelp not empty")
@ -327,15 +327,14 @@ class AkismetTest {
}
}
@Test
@Test(expectedExceptions = [IllegalArgumentException::class])
fun invalidApiTest() {
akismet.executeMethod("https://.com".toHttpUrlOrNull(), emptyFormBody)
assertTrue(akismet.errorMessage.startsWith("Invalid API"))
akismet.executeMethod("https://.com".toHttpUrl(), emptyFormBody)
}
@Test
fun ioErrorTest() {
akismet.executeMethod("https://www.doesnotexists.com".toHttpUrlOrNull(), emptyFormBody)
akismet.executeMethod("https://www.foobarxyz.com".toHttpUrl(), emptyFormBody)
assertTrue(akismet.errorMessage.contains("IO error"))
}