Skip to content

Commit

Permalink
added code and type accessors to response
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Coser committed May 20, 2020
1 parent ee34333 commit f636cac
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {

compile 'org.eclipse.jetty:jetty-server:9.4.27.v20200227'
compile 'org.eclipse.jetty:jetty-servlet:9.4.27.v20200227'
compile 'com.github.DaikonWeb:daikon-core:1.3.3'
compile 'com.github.DaikonWeb:daikon-core:1.3.4'

testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/daikon/HttpResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class HttpResponse(private val response: HttpServletResponse) : Response {
response.contentType = contentType
}

override fun type() = response.contentType

override fun status(code: Int) {
response.status = code
}

override fun status() = response.status

override fun write(text: String) {
writer.write(text)
write(text.toByteArray(UTF_8))
Expand Down
21 changes: 21 additions & 0 deletions src/test/kotlin/daikon/ResponseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daikon

import daikon.core.HttpStatus.CREATED_201
import daikon.core.HttpStatus.MOVED_PERMANENTLY_301
import daikon.core.HttpStatus.NOT_FOUND_404
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import topinambur.http
Expand Down Expand Up @@ -79,4 +80,24 @@ class ResponseTest {
assertThat(local("/").http.get().header("Server")).isEqualTo("Daikon")
}
}

@Test
fun `can access to response status code`() {
HttpServer()
.any("/") { _, res -> res.status(NOT_FOUND_404)}
.after("/") { _, res -> res.write("StatusCode = ${res.status()}")}
.start().use {
assertThat(local("/").http.get().body).isEqualTo("StatusCode = 404")
}
}

@Test
fun `can access to response type`() {
HttpServer()
.any("/") { _, res -> res.type("application/json")}
.after("/") { _, res -> res.write("Type = ${res.type()}")}
.start().use {
assertThat(local("/").http.get().body).isEqualTo("Type = application/json")
}
}
}

0 comments on commit f636cac

Please sign in to comment.