Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support any iterable or array in URL parameter encoder #32

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ class WWWFormURLEncoder(
is Map<*, *> -> value.toSortedMap(compareBy { it.toString() })
.flatMap { (nestedKey, value) -> encodeQueryComponent("$key[$nestedKey]", value as Any) }

is List<*> -> value.flatMap { element ->
is Iterable<*> -> value.flatMap { element ->
encodeQueryComponent(
arrayEncoding.encode(key),
element as Any
)
}

is Array<*> -> value.flatMap { element ->
encodeQueryComponent(
arrayEncoding.encode(key),
element as Any
Expand Down
60 changes: 58 additions & 2 deletions core/src/test/kotlin/WWWFormURLEncoderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class WWWFormURLEncoderTest {
}

@Test
fun `encodes array values in bracketed form`() {
fun `encodes list values in bracketed form`() {
val encoder = WWWFormURLEncoder(
WWWFormURLEncoder.ArrayEncoding.Bracketed,
WWWFormURLEncoder.BoolEncoding.Numeric,
Expand All @@ -77,7 +77,7 @@ class WWWFormURLEncoderTest {
}

@Test
fun `encodes array values in unbracketed form`() {
fun `encodes list values in unbracketed form`() {
val encoder = WWWFormURLEncoder(
WWWFormURLEncoder.ArrayEncoding.Unbracketed,
WWWFormURLEncoder.BoolEncoding.Numeric,
Expand All @@ -90,6 +90,62 @@ class WWWFormURLEncoderTest {
)
}

@Test
fun `encodes set values in bracketed form`() {
val encoder = WWWFormURLEncoder(
WWWFormURLEncoder.ArrayEncoding.Bracketed,
WWWFormURLEncoder.BoolEncoding.Numeric,
WWWFormURLEncoder.DateEncoding.ISO8601
)

assertThat(
encoder.encodeQueryString(mapOf("test" to setOf(1, 2, 3))),
equalTo("test%5B%5D=1&test%5B%5D=2&test%5B%5D=3")
)
}

@Test
fun `encodes set values in unbracketed form`() {
val encoder = WWWFormURLEncoder(
WWWFormURLEncoder.ArrayEncoding.Unbracketed,
WWWFormURLEncoder.BoolEncoding.Numeric,
WWWFormURLEncoder.DateEncoding.ISO8601
)

assertThat(
encoder.encodeQueryString(mapOf("test" to setOf(1, 2, 3))),
equalTo("test=1&test=2&test=3")
)
}

@Test
fun `encodes array values in bracketed form`() {
val encoder = WWWFormURLEncoder(
WWWFormURLEncoder.ArrayEncoding.Bracketed,
WWWFormURLEncoder.BoolEncoding.Numeric,
WWWFormURLEncoder.DateEncoding.ISO8601
)

assertThat(
encoder.encodeQueryString(mapOf("test" to arrayOf(1, 2, 3))),
equalTo("test%5B%5D=1&test%5B%5D=2&test%5B%5D=3")
)
}

@Test
fun `encodes array values in unbracketed form`() {
val encoder = WWWFormURLEncoder(
WWWFormURLEncoder.ArrayEncoding.Unbracketed,
WWWFormURLEncoder.BoolEncoding.Numeric,
WWWFormURLEncoder.DateEncoding.ISO8601
)

assertThat(
encoder.encodeQueryString(mapOf("test" to arrayOf(1, 2, 3))),
equalTo("test=1&test=2&test=3")
)
}

@Test
fun `encodes bool values in numeric form`() {
val encoder = WWWFormURLEncoder(
Expand Down