Skip to content

Commit

Permalink
Fix encoding in request body
Browse files Browse the repository at this point in the history
  • Loading branch information
fracassi-marco committed Jul 15, 2020
1 parent e0d62f3 commit 8d442eb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
testImplementation 'net.wuerl.kotlin:assertj-core-kotlin:0.2.1'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0'
testImplementation 'com.github.DaikonWeb:topinambur:1.2.3'
testImplementation 'com.github.DaikonWeb:topinambur:1.4.0'
}

test {
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/daikon/HttpRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import daikon.core.Method
import daikon.core.PathParams
import daikon.core.Request
import java.net.URLDecoder.decode
import java.nio.charset.StandardCharsets
import java.nio.charset.StandardCharsets.UTF_8
import javax.servlet.http.HttpServletRequest

class HttpRequest(private val request: HttpServletRequest) : Request {
private val body by lazy { request.reader.readText() }
private val body by lazy { request.inputStream.readBytes() }
private lateinit var pathParams: PathParams

override fun withPathParams(value: String): HttpRequest {
Expand All @@ -31,7 +32,7 @@ class HttpRequest(private val request: HttpServletRequest) : Request {

override fun url() = request.requestURL.toString() + if(request.queryString.isNullOrEmpty()) "" else "?${request.queryString}"

override fun body() = body
override fun body() = body.toString(UTF_8)

override fun header(name: String): String = request.getHeader(name)

Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/daikon/CharacterEncodingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ class CharacterEncodingTest {
}
}

@Test
fun `post body supports utf8`() {
HttpServer()
.post("/") { req, res -> res.write(req.body()) }
.start().use {
val response = local("/").http.post(body = "è$&")
assertThat(response.body).isEqualTo("è$&")
}
}

@Test
fun `header does not supports utf8`() {
HttpServer()
Expand Down

0 comments on commit 8d442eb

Please sign in to comment.