-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
5 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
140 changes: 140 additions & 0 deletions
140
tribune-ktor/src/jvmTest/kotlin/com/sksamuel/tribune/ktor/WithParsedInputTest.kt
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,140 @@ | ||
package com.sksamuel.tribune.ktor | ||
|
||
import com.sksamuel.tribune.core.Parser | ||
import com.sksamuel.tribune.core.compose | ||
import com.sksamuel.tribune.core.filter | ||
import com.sksamuel.tribune.core.map | ||
import com.sksamuel.tribune.core.strings.length | ||
import com.sksamuel.tribune.core.strings.nonBlankString | ||
import com.sksamuel.tribune.core.strings.notNullOrBlank | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.ktor.client.request.post | ||
import io.ktor.client.request.setBody | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.contentType | ||
import io.ktor.serialization.jackson.jackson | ||
import io.ktor.server.application.call | ||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.routing.post | ||
import io.ktor.server.testing.testApplication | ||
|
||
class WithParsedInputTest : FunSpec() { | ||
init { | ||
test("happy path should pass off to lambda") { | ||
testApplication { | ||
install(ContentNegotiation) { jackson() } | ||
routing { | ||
post("/foo") { | ||
withParsedInput(bookParser, jsonHandler) { | ||
call.respond(HttpStatusCode.Created, "Book created") | ||
} | ||
} | ||
} | ||
val resp = client.post("/foo") { | ||
contentType(ContentType.Application.Json) | ||
setBody("""{ "author": "Willy Shakes", "title": "midwinters day dream", "isbn": "1234567890" }""") | ||
} | ||
resp.status shouldBe HttpStatusCode.Created | ||
} | ||
} | ||
|
||
test("unhappy path with default handler") { | ||
testApplication { | ||
install(ContentNegotiation) { jackson() } | ||
routing { | ||
post("/foo") { | ||
withParsedInput(bookParser) { | ||
call.respond(HttpStatusCode.Created, "Book created") | ||
} | ||
} | ||
} | ||
val resp = client.post("/foo") { | ||
contentType(ContentType.Application.Json) | ||
setBody("""{ "author": "Willy Shakes", "isbn": "123" }""") | ||
} | ||
resp.status shouldBe HttpStatusCode.BadRequest | ||
resp.bodyAsText() shouldBe """Title must be provided, Valid ISBNs have length 10 or 13""" | ||
} | ||
} | ||
|
||
test("unhappy path with json handler") { | ||
testApplication { | ||
install(ContentNegotiation) { jackson() } | ||
routing { | ||
post("/foo") { | ||
withParsedInput(bookParser, jsonHandler) { | ||
call.respond(HttpStatusCode.Created, "Book created") | ||
} | ||
} | ||
} | ||
val resp = client.post("/foo") { | ||
contentType(ContentType.Application.Json) | ||
setBody("""{ "author": "Willy Shakes", "isbn": "123" }""") | ||
} | ||
resp.status shouldBe HttpStatusCode.BadRequest | ||
resp.bodyAsText() shouldBe """[ | ||
"Title must be provided", | ||
"Valid ISBNs have length 10 or 13" | ||
]""" | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class BookInput( | ||
val title: String?, | ||
val author: String?, | ||
val isbn: String?, | ||
) | ||
|
||
data class ParsedBook( | ||
val title: Title, | ||
val author: Author, | ||
val isbn: Isbn, | ||
) | ||
|
||
@JvmInline | ||
value class Title internal constructor(private val value: String) { | ||
val asString get() = value | ||
} | ||
|
||
@JvmInline | ||
value class Author internal constructor(private val value: String) { | ||
val asString get() = value | ||
} | ||
|
||
@JvmInline | ||
value class Isbn internal constructor(private val value: String) { | ||
val asString get() = value | ||
} | ||
|
||
// must be at least two tokens | ||
val authorParser: Parser<String?, Author, String> = | ||
Parser.from<String?>() | ||
.notNullOrBlank { "Author must be provided" } | ||
.filter({ it.contains(" ") }) { "Author must be at least two names" } | ||
.map { Author(it) } | ||
|
||
val titleParser: Parser<String?, Title, String> = | ||
Parser.nonBlankString { "Title must be provided" } | ||
.map { Title(it) } | ||
|
||
// must be 10 or 13 characters | ||
val isbnParser: Parser<String?, Isbn, String> = | ||
Parser.from<String?>() | ||
.notNullOrBlank { "ISBN must be provided" } | ||
.length({ it == 10 || it == 13 }) { "Valid ISBNs have length 10 or 13" } | ||
.filter({ it.length == 10 || it.startsWith("9") }, { "13 Digit ISBNs must start with 9" }) | ||
.map { Isbn(it) } | ||
|
||
val bookParser: Parser<BookInput, ParsedBook, String> = | ||
Parser.compose( | ||
titleParser.contramap { it.title }, | ||
authorParser.contramap { it.author }, | ||
isbnParser.contramap { it.isbn }, | ||
::ParsedBook, | ||
) |