feature #6: start to implement path for article
This commit is contained in:
4
.idea/gradle.xml
generated
4
.idea/gradle.xml
generated
@@ -3,9 +3,11 @@
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="delegatedBuild" value="true" />
|
||||
<option name="testRunner" value="GRADLE" />
|
||||
<option name="distributionType" value="DEFAULT_WRAPPED" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="gradleJvm" />
|
||||
<option name="gradleJvm" value="11" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package fr.dcproject
|
||||
|
||||
import io.ktor.application.*
|
||||
import io.ktor.response.*
|
||||
import io.ktor.request.*
|
||||
import io.ktor.routing.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.locations.*
|
||||
import io.ktor.auth.*
|
||||
import io.ktor.gson.*
|
||||
import io.ktor.features.*
|
||||
import fr.dcproject.routes.article
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.install
|
||||
import io.ktor.auth.Authentication
|
||||
import io.ktor.features.ContentNegotiation
|
||||
import io.ktor.gson.gson
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Locations
|
||||
import io.ktor.routing.Routing
|
||||
|
||||
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@Suppress("unused") // Referenced in application.conf
|
||||
@kotlin.jvm.JvmOverloads
|
||||
fun Application.module(testing: Boolean = false) {
|
||||
@@ -26,36 +27,8 @@ fun Application.module(testing: Boolean = false) {
|
||||
}
|
||||
}
|
||||
|
||||
routing {
|
||||
get("/") {
|
||||
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
|
||||
}
|
||||
|
||||
get<MyLocation> {
|
||||
call.respondText("Location: name=${it.name}, arg1=${it.arg1}, arg2=${it.arg2}")
|
||||
}
|
||||
// Register nested routes
|
||||
get<Type.Edit> {
|
||||
call.respondText("Inside $it")
|
||||
}
|
||||
get<Type.List> {
|
||||
call.respondText("Inside $it")
|
||||
}
|
||||
|
||||
get("/json/gson") {
|
||||
call.respond(mapOf("hello" to "world"))
|
||||
}
|
||||
install(Routing) {
|
||||
article()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Location("/location/{name}")
|
||||
class MyLocation(val name: String, val arg1: Int = 42, val arg2: String = "default")
|
||||
|
||||
@Location("/type/{name}") data class Type(val name: String) {
|
||||
@Location("/edit")
|
||||
data class Edit(val type: Type)
|
||||
|
||||
@Location("/list/{page}")
|
||||
data class List(val type: Type, val page: Int)
|
||||
}
|
||||
|
||||
|
||||
15
src/routes/Article.kt
Normal file
15
src/routes/Article.kt
Normal file
@@ -0,0 +1,15 @@
|
||||
package fr.dcproject.routes
|
||||
|
||||
import Paths
|
||||
import io.ktor.application.call
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.response.respondText
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
fun Route.article() {
|
||||
get<Paths.ArticlesRequest> {
|
||||
call.respondText("todo")
|
||||
}
|
||||
}
|
||||
8
src/routes/Paths.kt
Normal file
8
src/routes/Paths.kt
Normal file
@@ -0,0 +1,8 @@
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object Paths {
|
||||
@Location("/articles") class ArticlesRequest
|
||||
@Location("/articles") class PostArticleRequest
|
||||
}
|
||||
@@ -1,24 +1,19 @@
|
||||
package fr.dcproject
|
||||
|
||||
import io.ktor.application.*
|
||||
import io.ktor.response.*
|
||||
import io.ktor.request.*
|
||||
import io.ktor.routing.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.locations.*
|
||||
import io.ktor.auth.*
|
||||
import io.ktor.gson.*
|
||||
import io.ktor.features.*
|
||||
import kotlin.test.*
|
||||
import io.ktor.server.testing.*
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.testing.handleRequest
|
||||
import io.ktor.server.testing.withTestApplication
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ApplicationTest {
|
||||
@Test
|
||||
fun testRoot() {
|
||||
withTestApplication({ module(testing = true) }) {
|
||||
handleRequest(HttpMethod.Get, "/").apply {
|
||||
handleRequest(HttpMethod.Get, "/articles").apply {
|
||||
assertEquals(HttpStatusCode.OK, response.status())
|
||||
assertEquals("HELLO WORLD!", response.content)
|
||||
assertEquals("todo", response.content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user