Implemented additional HTTP status codes. Closes #3
This commit is contained in:
parent
f492c712bd
commit
3971b2fc36
2 changed files with 43 additions and 12 deletions
|
@ -35,7 +35,25 @@ package net.thauvin.erik.bitly
|
|||
/**
|
||||
* 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
|
||||
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
|
||||
}
|
||||
|
|
|
@ -132,9 +132,19 @@ class BitlyTest {
|
|||
fun `bitlinks lastCallResponse`() {
|
||||
val bl = Bitlinks(bitly.accessToken)
|
||||
bl.shorten(longUrl, domain = "bit.ly")
|
||||
assertEquals(true, bl.lastCallResponse.isSuccessful, "is successful")
|
||||
assertEquals(200, bl.lastCallResponse.resultCode, "resultCode == 200")
|
||||
assertTrue(bl.lastCallResponse.body.contains("\"link\":\"$shortUrl\""), "valid body")
|
||||
with(bl.lastCallResponse) {
|
||||
assertEquals(true, isSuccessful, "is successful")
|
||||
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
|
||||
|
@ -146,21 +156,24 @@ class BitlyTest {
|
|||
fun `create bitlink`() {
|
||||
assertEquals(
|
||||
shortUrl,
|
||||
bitly.bitlinks()
|
||||
.create(
|
||||
domain = "bit.ly",
|
||||
title = "Erik's Blog",
|
||||
tags = arrayOf("erik", "thauvin", "blog", "weblog"),
|
||||
long_url = longUrl
|
||||
)
|
||||
bitly.bitlinks().create(
|
||||
domain = "bit.ly",
|
||||
title = "Erik's Blog",
|
||||
tags = arrayOf("erik", "thauvin", "blog", "weblog"),
|
||||
long_url = longUrl
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `update bitlink`() {
|
||||
val bl = bitly.bitlinks()
|
||||
assertEquals(
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue