-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests for some of the generic route handling
- Loading branch information
1 parent
1a3b7d5
commit 1017c7d
Showing
24 changed files
with
121 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package routes | ||
|
||
import io.ktor.http.HttpStatusCode | ||
|
||
class ClientException( | ||
val statusCode: HttpStatusCode, | ||
val errorCode: String, | ||
override val message: String, | ||
override val cause: Throwable? = null | ||
) : RuntimeException(message) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package plugins | ||
|
||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.routing | ||
import io.ktor.server.testing.testApplication | ||
import org.junit.jupiter.api.Test | ||
import routes.ClientException | ||
import testCore.AbstractTest | ||
import testCore.shouldMatchJson | ||
|
||
class RoutingTest : AbstractTest() { | ||
@Test | ||
fun `Should handle client errors`() = testApplication { | ||
application { ErrorThrowingController.installRoutes(this) } | ||
|
||
val response = client.get("/client-error") | ||
response.status shouldBe HttpStatusCode.Conflict | ||
response.bodyAsText() shouldMatchJson | ||
"""{ | ||
"errorCode": "conflictingEntity", | ||
"errorMessage": "Entity conflicts with another" | ||
}""" | ||
.trimIndent() | ||
} | ||
|
||
@Test | ||
fun `Should handle unexpected errors`() = testApplication { | ||
application { ErrorThrowingController.installRoutes(this) } | ||
|
||
val response = client.get("/internal-error") | ||
response.status shouldBe HttpStatusCode.InternalServerError | ||
response.bodyAsText() shouldMatchJson | ||
"""{ | ||
"errorCode": "internalServerError", | ||
"errorMessage": "Error handling GET - /internal-error" | ||
}""" | ||
.trimIndent() | ||
|
||
errorLogged() shouldBe true | ||
} | ||
} | ||
|
||
private object ErrorThrowingController { | ||
fun installRoutes(application: Application) { | ||
application.routing { | ||
get("/internal-error") { throw NullPointerException("Test error") } | ||
get("/client-error") { | ||
throw ClientException( | ||
HttpStatusCode.Conflict, | ||
"conflictingEntity", | ||
"Entity conflicts with another" | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package testCore | ||
|
||
import org.skyscreamer.jsonassert.JSONAssert | ||
|
||
infix fun String.shouldMatchJson(expected: String) { | ||
JSONAssert.assertEquals(expected, this, false) | ||
} |