API URL must not be null.
This commit is contained in:
parent
e17758f71b
commit
f9594e9dc3
3 changed files with 59 additions and 67 deletions
|
@ -35,7 +35,7 @@ import kotlinx.serialization.json.Json
|
||||||
import net.thauvin.erik.semver.Version
|
import net.thauvin.erik.semver.Version
|
||||||
import okhttp3.FormBody
|
import okhttp3.FormBody
|
||||||
import okhttp3.HttpUrl
|
import okhttp3.HttpUrl
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.logging.HttpLoggingInterceptor
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
|
@ -45,7 +45,7 @@ import java.time.OffsetDateTime
|
||||||
import java.time.ZoneId
|
import java.time.ZoneId
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
import java.time.temporal.ChronoUnit
|
import java.time.temporal.ChronoUnit
|
||||||
import java.util.*
|
import java.util.Date
|
||||||
import java.util.logging.Level
|
import java.util.logging.Level
|
||||||
import java.util.logging.Logger
|
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.)
|
* @param trueOnError Set to return `true` on error (IO, empty response, etc.)
|
||||||
*/
|
*/
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun executeMethod(apiUrl: HttpUrl?, formBody: FormBody, trueOnError: Boolean = false): Boolean {
|
fun executeMethod(apiUrl: HttpUrl, formBody: FormBody, trueOnError: Boolean = false): Boolean {
|
||||||
reset()
|
reset()
|
||||||
if (apiUrl != null) {
|
|
||||||
val request = if (formBody.size == 0) {
|
val request = if (formBody.size == 0) {
|
||||||
Request.Builder().url(apiUrl).header("User-Agent", buildUserAgent()).build()
|
Request.Builder().url(apiUrl).header("User-Agent", buildUserAgent()).build()
|
||||||
} else {
|
} else {
|
||||||
|
@ -356,10 +355,7 @@ open class Akismet(apiKey: String) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
errorMessage = "An IO error occurred while communicating with the Akismet service."
|
errorMessage = "An IO error occurred while communicating with the Akismet service: ${e.message}"
|
||||||
}
|
|
||||||
} else {
|
|
||||||
errorMessage = "Invalid API end point URL."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorMessage.isNotEmpty()) {
|
if (errorMessage.isNotEmpty()) {
|
||||||
|
@ -384,11 +380,11 @@ open class Akismet(apiKey: String) {
|
||||||
response = ""
|
response = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.toApiUrl(): HttpUrl? {
|
private fun String.toApiUrl(): HttpUrl {
|
||||||
return if (this == verifyMethod) {
|
return if (this == verifyMethod) {
|
||||||
String.format(apiEndPoint, "", this).toHttpUrlOrNull()
|
String.format(apiEndPoint, "", this).toHttpUrl()
|
||||||
} else {
|
} else {
|
||||||
String.format(apiEndPoint, "$apiKey.", this).toHttpUrlOrNull()
|
String.format(apiEndPoint, "$apiKey.", this).toHttpUrl()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,9 +36,6 @@ import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import javax.servlet.http.HttpServletRequest
|
import javax.servlet.http.HttpServletRequest
|
||||||
import kotlin.collections.Map
|
|
||||||
import kotlin.collections.emptyMap
|
|
||||||
import kotlin.collections.iterator
|
|
||||||
import kotlin.collections.set
|
import kotlin.collections.set
|
||||||
|
|
||||||
private fun String?.ifNull() = this ?: ""
|
private fun String?.ifNull() = this ?: ""
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
package net.thauvin.erik.akismet
|
package net.thauvin.erik.akismet
|
||||||
|
|
||||||
import okhttp3.FormBody
|
import okhttp3.FormBody
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||||
import org.mockito.Mockito
|
import org.mockito.Mockito
|
||||||
import org.mockito.Mockito.`when`
|
import org.mockito.Mockito.`when`
|
||||||
import org.testng.Assert.assertEquals
|
import org.testng.Assert.assertEquals
|
||||||
|
@ -256,7 +256,7 @@ class AkismetTest {
|
||||||
with(akismet) {
|
with(akismet) {
|
||||||
assertTrue(
|
assertTrue(
|
||||||
executeMethod(
|
executeMethod(
|
||||||
"https://postman-echo.com/status/200".toHttpUrlOrNull(), emptyFormBody, true
|
"https://postman-echo.com/status/200".toHttpUrl(), emptyFormBody, true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
var expected = "{\"status\":200}"
|
var expected = "{\"status\":200}"
|
||||||
|
@ -268,7 +268,7 @@ class AkismetTest {
|
||||||
|
|
||||||
assertTrue(
|
assertTrue(
|
||||||
executeMethod(
|
executeMethod(
|
||||||
"https://erik.thauvin.net/blank.html".toHttpUrlOrNull(), emptyFormBody, true
|
"https://erik.thauvin.net/blank.html".toHttpUrl(), emptyFormBody, true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
expected = ""
|
expected = ""
|
||||||
|
@ -282,7 +282,7 @@ class AkismetTest {
|
||||||
with(akismet) {
|
with(akismet) {
|
||||||
assertFalse(
|
assertFalse(
|
||||||
executeMethod(
|
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
|
emptyFormBody
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -317,7 +317,7 @@ class AkismetTest {
|
||||||
fun executeMethodTest() {
|
fun executeMethodTest() {
|
||||||
with(akismet) {
|
with(akismet) {
|
||||||
executeMethod(
|
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()
|
FormBody.Builder().apply { add("is_test", "1") }.build()
|
||||||
)
|
)
|
||||||
assertTrue(debugHelp.isNotEmpty(), "debugHelp not empty")
|
assertTrue(debugHelp.isNotEmpty(), "debugHelp not empty")
|
||||||
|
@ -327,15 +327,14 @@ class AkismetTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expectedExceptions = [IllegalArgumentException::class])
|
||||||
fun invalidApiTest() {
|
fun invalidApiTest() {
|
||||||
akismet.executeMethod("https://.com".toHttpUrlOrNull(), emptyFormBody)
|
akismet.executeMethod("https://.com".toHttpUrl(), emptyFormBody)
|
||||||
assertTrue(akismet.errorMessage.startsWith("Invalid API"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun ioErrorTest() {
|
fun ioErrorTest() {
|
||||||
akismet.executeMethod("https://www.doesnotexists.com".toHttpUrlOrNull(), emptyFormBody)
|
akismet.executeMethod("https://www.foobarxyz.com".toHttpUrl(), emptyFormBody)
|
||||||
assertTrue(akismet.errorMessage.contains("IO error"))
|
assertTrue(akismet.errorMessage.contains("IO error"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue