Implemented additional HTTP status codes. Closes #3

This commit is contained in:
Erik C. Thauvin 2021-06-04 23:41:30 -07:00
parent f492c712bd
commit 3971b2fc36
2 changed files with 43 additions and 12 deletions

View file

@ -35,7 +35,25 @@ package net.thauvin.erik.bitly
/** /**
* Provides a data class to hold the JSON response. * Provides a data class to hold the JSON response.
*/ */
data class CallResponse(var body: String = Constants.EMPTY_JSON, var resultCode: Int = -1) { data class CallResponse(val body: String = Constants.EMPTY_JSON, val resultCode: Int = -1) {
val isSuccessful: Boolean val isSuccessful: Boolean
get() = resultCode in 200..299 get() = resultCode in 200..299
val isCreated: Boolean
get() = resultCode == 201
val isBadRequest: Boolean
get() = resultCode == 400
val isUpgradeRequired: Boolean
get() = resultCode == 402
val isForbidden: Boolean
get() = resultCode == 403
val isNotFound: Boolean
get() = resultCode == 404
val isExpectationFailed: Boolean
get() = resultCode == 417
val isUnprocessableEntity: Boolean
get() = resultCode == 422
val isInternalError: Boolean
get() = resultCode == 500
val isTemporarilyUnavailable: Boolean
get() = resultCode == 503
} }

View file

@ -132,9 +132,19 @@ class BitlyTest {
fun `bitlinks lastCallResponse`() { fun `bitlinks lastCallResponse`() {
val bl = Bitlinks(bitly.accessToken) val bl = Bitlinks(bitly.accessToken)
bl.shorten(longUrl, domain = "bit.ly") bl.shorten(longUrl, domain = "bit.ly")
assertEquals(true, bl.lastCallResponse.isSuccessful, "is successful") with(bl.lastCallResponse) {
assertEquals(200, bl.lastCallResponse.resultCode, "resultCode == 200") assertEquals(true, isSuccessful, "is successful")
assertTrue(bl.lastCallResponse.body.contains("\"link\":\"$shortUrl\""), "valid body") assertEquals(200, resultCode, "resultCode == 200")
assertTrue(body.contains("\"link\":\"$shortUrl\""), "valid body")
}
bl.shorten(shortUrl)
with(bl.lastCallResponse) {
assertEquals(false, isSuccessful, "is not successful")
assertEquals(400, resultCode, "resultCode == 400")
assertTrue(isBadRequest, "is bad request")
assertTrue(body.contains("ALREADY_A_BITLY_LINK"), "already a bitlink")
}
} }
@Test @Test
@ -146,21 +156,24 @@ class BitlyTest {
fun `create bitlink`() { fun `create bitlink`() {
assertEquals( assertEquals(
shortUrl, shortUrl,
bitly.bitlinks() bitly.bitlinks().create(
.create( domain = "bit.ly",
domain = "bit.ly", title = "Erik's Blog",
title = "Erik's Blog", tags = arrayOf("erik", "thauvin", "blog", "weblog"),
tags = arrayOf("erik", "thauvin", "blog", "weblog"), long_url = longUrl
long_url = longUrl )
)
) )
} }
@Test @Test
fun `update bitlink`() { fun `update bitlink`() {
val bl = bitly.bitlinks()
assertEquals( assertEquals(
Constants.TRUE, Constants.TRUE,
bitly.bitlinks().update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog")) bl.update(shortUrl, title = "Erik's Weblog", tags = arrayOf("blog", "weblog"))
) )
bl.update(shortUrl, link = longUrl)
assertTrue(bl.lastCallResponse.isUnprocessableEntity, "422 Unprocessable")
} }
} }