Skip to content

Commit 883a67a

Browse files
committed
fix incorrect URL encoding algorithm after replacing Apache's URIBuilder
1 parent 6d2858f commit 883a67a

File tree

2 files changed

+57
-4
lines changed
  • src
    • jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/model
    • jvmTest/kotlin/com/sunnychung/application/multiplatform/hellohttp/test

2 files changed

+57
-4
lines changed

src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/model/HttpRequest.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.sunnychung.application.multiplatform.hellohttp.model
33
//import org.apache.hc.core5.net.URIBuilder
44
import org.springframework.web.util.UriComponentsBuilder
55
import java.net.URI
6+
import java.net.URLEncoder
67

78
class HttpRequest(
89
val method: String = "",
@@ -32,16 +33,22 @@ class HttpRequest(
3233
val headers get() = initialHeaders + newHeaders
3334
val queryParameters get() = initialQueryParameters + newQueryParameters
3435

35-
fun getResolvedUri(): URI {
36+
fun getResolvedUri(): URI { // see com.sunnychung.application.multiplatform.hellohttp.test.HttpRequestTest
3637
return UriComponentsBuilder.fromUriString(url.replace(" ", "+"))
38+
.encode()
39+
.build()
40+
.toString()
3741
.run {
38-
var b = this
42+
// var b = this
43+
// println(this)
44+
var b = UriComponentsBuilder.fromUriString(this)
3945
queryParameters.forEach {
40-
b = b.queryParam(it.first, it.second)
46+
b = b.queryParam(it.first, URLEncoder.encode(it.second, Charsets.UTF_8))
47+
// b = b.queryParam(it.first, it.second)
4148
}
4249
b
4350
}
44-
.encode()
51+
// .encode()
4552
.build()
4653
.let { URI.create(it.toString()) }
4754
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.sunnychung.application.multiplatform.hellohttp.test
2+
3+
import com.sunnychung.application.multiplatform.hellohttp.model.ContentType
4+
import com.sunnychung.application.multiplatform.hellohttp.model.HttpRequest
5+
import com.sunnychung.application.multiplatform.hellohttp.model.ProtocolApplication
6+
import kotlin.test.Test
7+
import kotlin.test.assertEquals
8+
9+
class HttpRequestTest {
10+
11+
@Test
12+
fun `plus char is encoded`() {
13+
HttpRequest(
14+
method = "GET",
15+
url = "http://sunnychung.application.multiplatform.hellohttp/path",
16+
queryParameters = listOf("a" to "b+c", "d" to "e+f"),
17+
contentType = ContentType.None,
18+
application = ProtocolApplication.Http,
19+
).run {
20+
assertEquals("http://sunnychung.application.multiplatform.hellohttp/path?a=b%2bc&d=e%2bf", getResolvedUri().toString().lowercase())
21+
}
22+
23+
// HttpRequest(
24+
// method = "GET",
25+
// url = "http://sunnychung.application.multiplatform.hellohttp/path+xyz",
26+
// queryParameters = listOf("a" to "b+c", "d" to "e+f"),
27+
// contentType = ContentType.None,
28+
// application = ProtocolApplication.Http,
29+
// ).run {
30+
// assertEquals("http://sunnychung.application.multiplatform.hellohttp/path%2bxyz?a=b%2bc&d=e%2bf", getResolvedUri().toString().lowercase())
31+
// }
32+
}
33+
34+
@Test
35+
fun `unicode is encoded`() {
36+
HttpRequest(
37+
method = "GET",
38+
url = "http://sunnychung.application.multiplatform.hellohttp/anything/中文字",
39+
queryParameters = listOf("a" to "b+c", "d" to "e+f"),
40+
contentType = ContentType.None,
41+
application = ProtocolApplication.Http,
42+
).run {
43+
assertEquals("http://sunnychung.application.multiplatform.hellohttp/anything/%E4%B8%AD%E6%96%87%E5%AD%97?a=b%2bc&d=e%2bf".lowercase(), getResolvedUri().toString().lowercase())
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)