Skip to content

Commit

Permalink
Add redirect on response
Browse files Browse the repository at this point in the history
  • Loading branch information
fracassi-marco committed Dec 4, 2019
1 parent dbd45c7 commit 26dda2b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/main/kotlin/daikon/HttpResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class HttpResponse(private val response: HttpServletResponse) : Response {
return writer.toString()
}

override fun redirect(path: String, status: Int) {
status(status)
header("Location", path)
}

override fun content(type: String) {
response.contentType = type
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/daikon/Response.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package daikon

import org.eclipse.jetty.http.HttpStatus.MOVED_TEMPORARILY_302

interface Response {
fun write(text: String)
fun status(code: Int)
fun content(type: String)
fun header(name: String, value: String)
fun body(): String
fun redirect(path: String, status: Int = MOVED_TEMPORARILY_302)
}
26 changes: 24 additions & 2 deletions src/test/kotlin/daikon/ResponseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package daikon
import daikon.Localhost.get
import daikon.Localhost.post
import org.assertj.core.api.Assertions.assertThat
import org.eclipse.jetty.http.HttpStatus.CREATED_201
import org.eclipse.jetty.http.HttpStatus.MOVED_PERMANENTLY_301
import org.junit.jupiter.api.Test

class ResponseTest {

@Test
fun `status code`() {
HttpServer()
.any("/") { _, res -> res.status(201) }
.any("/") { _, res -> res.status(CREATED_201) }
.start().use {
assertThat(get("/").statusCode).isEqualTo(201)
assertThat(get("/").statusCode).isEqualTo(CREATED_201)
}
}

Expand Down Expand Up @@ -49,4 +51,24 @@ class ResponseTest {
assertThat(body).isEqualTo("Hi Bob")
}
}

@Test
fun `redirect to relative path`() {
HttpServer()
.any("/foo") { _, res -> res.redirect("/bar", MOVED_PERMANENTLY_301) }
.any("/bar") { _, res -> res.write("Hello") }
.start().use {
assertThat(get("/foo").text).isEqualTo("Hello")
}
}

@Test
fun `redirect to absolute path`() {
HttpServer()
.any("/foo") { _, res -> res.redirect("http://localhost:4545/bar") }
.any("/bar") { _, res -> res.write("Hello") }
.start().use {
assertThat(get("/foo").text).isEqualTo("Hello")
}
}
}

0 comments on commit 26dda2b

Please sign in to comment.