Skip to content

Commit 0c7246a

Browse files
committed
Merge remote-tracking branch 'origin/master' into regex-java-external-flags
2 parents 69f744e + 77377c1 commit 0c7246a

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

core/src/main/kotlin/org/evomaster/core/problem/rest/data/RestPath.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class RestPath(path: String) {
413413
why not using URI also for Query part???
414414
it seems unclear how to properly build it as a single string...
415415
*/
416-
val entry = URI(null, null, path.toString(), null, null).rawPath
416+
val entry = URI(null, null, path.toString(), null, null).toASCIIString()
417417
data.add(Pair(entry, false))
418418
path.setLength(0) // clear it
419419
data.add(Pair(variable, true))
@@ -446,7 +446,7 @@ class RestPath(path: String) {
446446
}
447447

448448
if(path.isNotEmpty()){
449-
val entry = URI(null, null, path.toString(), null, null).rawPath
449+
val entry = URI(null, null, path.toString(), null, null).toASCIIString()
450450
data.add(Pair(entry, false))
451451
}
452452

core/src/test/kotlin/org/evomaster/core/output/TestCaseWriterTest.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.evomaster.core.output.service.RestTestCaseWriter
1616
import org.evomaster.core.problem.enterprise.SampleType
1717
import org.evomaster.core.problem.rest.data.*
1818
import org.evomaster.core.problem.rest.param.BodyParam
19+
import org.evomaster.core.problem.rest.param.PathParam
1920
import org.evomaster.core.search.EvaluatedIndividual
2021
import org.evomaster.core.search.FitnessValue
2122
import org.evomaster.core.search.gene.*
@@ -28,6 +29,7 @@ import org.evomaster.core.search.gene.collection.EnumGene
2829
import org.evomaster.core.search.gene.numeric.IntegerGene
2930
import org.evomaster.core.search.gene.string.StringGene
3031
import org.evomaster.core.search.gene.utils.GeneUtils
32+
import org.evomaster.core.search.gene.wrapper.CustomMutationRateGene
3133
import org.evomaster.core.search.gene.wrapper.OptionalGene
3234
import org.evomaster.core.sql.schema.TableId
3335
import org.junit.jupiter.api.Assertions.*
@@ -1679,4 +1681,30 @@ public void test() throws Exception {
16791681
assertFalse(lines.toString().contains(".body()"))
16801682

16811683
}
1684+
1685+
@Test
1686+
fun testNonAsciiInPathParamIsEncoded() {
1687+
// non-ASCII characters in path parameter values must be percent-encoded in generated test output.
1688+
val format = OutputFormat.KOTLIN_JUNIT_5
1689+
1690+
val pathParam = PathParam("key", CustomMutationRateGene("key", StringGene("key", ""), 1.0))
1691+
val action = RestCallAction("1", HttpVerb.GET, RestPath("/api/{key}"), mutableListOf(pathParam))
1692+
val individual = RestIndividual(mutableListOf(action), SampleType.RANDOM)
1693+
TestUtils.doInitializeIndividualForTesting(individual)
1694+
1695+
val result = RestCallResult(action.getLocalId())
1696+
result.setTimedout(false)
1697+
result.setStatusCode(200)
1698+
val ei = EvaluatedIndividual(FitnessValue(0.0), individual, listOf(result))
1699+
1700+
val writer = RestTestCaseWriter(getConfig(format), PartialOracles())
1701+
val lines = writer.convertToCompilableTestCode(TestCase(test = ei, name = "test"), "baseUrlOfSut")
1702+
val output = lines.toString()
1703+
1704+
assertFalse(output.contains(""),
1705+
"Non-ASCII character must not appear raw in generated test output, got:\n$output")
1706+
1707+
assertTrue(output.contains("%E8%81%9A"),
1708+
"Non-ASCII character must be percent-encoded as %E8%81%9A in generated test output, got:\n$output")
1709+
}
16821710
}

core/src/test/kotlin/org/evomaster/core/problem/rest/RestPathTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,19 @@ internal class RestPathTest{
605605
assertEquals(y, resolvedY)
606606
}
607607

608+
@Test
609+
fun testPathParamWithNonAscii() {
610+
// Non-ASCII characters (e.g. from AnyCharacterRxGene sampling the full
611+
// Unicode range) must also be percent-encoded in path parameters. These
612+
// are not encoded by URI(null, null, s, null, null) in path parameters,
613+
// causing them to appear raw in the generated URL.
614+
val key = PathParam("key", CustomMutationRateGene("d_", StringGene("key", "key-聚"), 1.0))
615+
616+
val resolved = RestPath("/api/{key}").resolve(listOf(key))
617+
618+
assertFalse(resolved.contains(""),
619+
"Resolved path must not contain a raw non-ASCII character, got: $resolved")
620+
assertTrue(resolved.contains("%E8%81%9A"),
621+
"Resolved path must percent-encode non-ASCII characters, got: $resolved")
622+
}
608623
}

0 commit comments

Comments
 (0)