Skip to content

Commit

Permalink
Respond with status 404 when view was not found (#76)
Browse files Browse the repository at this point in the history
* Respond with status 404 when view was not found

* Remove TODO
  • Loading branch information
felipecsl committed Aug 27, 2019
1 parent 788e5a5 commit afc34a1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
17 changes: 11 additions & 6 deletions kales/src/main/kotlin/kales/KalesApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import io.ktor.features.CallLogging
import io.ktor.features.DefaultHeaders
import io.ktor.html.respondHtmlTemplate
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.files
import io.ktor.http.content.static
import io.ktor.http.content.staticRootFolder
import io.ktor.response.respond
import io.ktor.routing.*
import io.ktor.util.pipeline.PipelineContext
import kales.actionpack.ApplicationController
Expand Down Expand Up @@ -65,11 +67,14 @@ class KalesApplication<T : ApplicationLayout>(
suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit {
return {
val view = callControllerAction(controllerClass, actionName, call)
call.respondHtmlTemplate(layout.createInstance()) {
body {
// TODO: #70 Respond with 404 when a view was not found
view?.renderContent(this)
if (view != null) {
call.respondHtmlTemplate(layout.createInstance()) {
body {
view.renderContent(this)
}
}
} else {
call.respond(HttpStatusCode.NotFound, "Not Found")
}
}
}
Expand Down Expand Up @@ -175,10 +180,10 @@ class KalesApplication<T : ApplicationLayout>(
val applicationPackage = extractAppPackageNameFromControllerClass(controllerClass)
val viewClassName = "${actionName.capitalize()}View"
val viewFullyQualifiedName = "$applicationPackage.views.$modelName.$viewClassName"
return maybeLoadCachedViewClass(viewFullyQualifiedName, controllerClass.java.classLoader)
return loadViewClass(viewFullyQualifiedName, controllerClass.java.classLoader)
}

private fun maybeLoadCachedViewClass(viewFullyQualifiedName: String, classLoader: ClassLoader) =
private fun loadViewClass(viewFullyQualifiedName: String, classLoader: ClassLoader) =
try {
@Suppress("UNCHECKED_CAST")
val clazz = Class.forName(viewFullyQualifiedName, true, classLoader) as Class<ActionView<*>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ class KalesApplicationIntegrationTest {
}
}

@Test fun `responds with status 404 when view was not found`() {
withTestApplication(Application::testModule) {
with(handleRequest(HttpMethod.Get, "/should404")) {
assertThat(response.status()).isEqualTo(HttpStatusCode.NotFound)
}
}
}

private fun assertSuccessfulResponseWithBody(
response: TestApplicationResponse,
expectedContent: String
Expand All @@ -108,6 +116,7 @@ class KalesApplicationIntegrationTest {
fun Application.testModule() {
kalesApp(TestAppLayout::class) {
get<TestController>("/", "index")
get<TestController>("/should404", "actionWithoutView")
post<TestController>("/", "create")
delete<TestController>("/posts/{id}", "destroy")
put<TestController>("/posts", "put")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class TestController(call: ApplicationCall) : ApplicationController(call) {
bindings = IndexViewModel("foo")
}

fun actionWithoutView() {
}

suspend fun create() {
val params = receiveParameters()
bindings = CreateViewModel(params["message"]!!)
Expand Down

0 comments on commit afc34a1

Please sign in to comment.