Skip to content

Commit 2c88d51

Browse files
authored
Merge pull request #1242 from WebFuzzing/location-output-var
Location output var
2 parents d0edaa2 + d872f4b commit 2c88d51

2 files changed

Lines changed: 30 additions & 47 deletions

File tree

core/src/main/kotlin/org/evomaster/core/output/service/RestTestCaseWriter.kt

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,6 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
6767
testName: String
6868
) {
6969
super.handleTestInitialization(lines, baseUrlOfSut, ind, insertionVars,testName)
70-
71-
if (hasChainedLocations(ind.individual)) {
72-
assert(ind.individual is RestIndividual)
73-
/*
74-
If the "location" header of a HTTP response is used in a following
75-
call, we need to save it in a variable.
76-
We declare all such variables at the beginning of the test.
77-
78-
TODO: rather declare variable first time we access it?
79-
Yes! can use location index for unique name
80-
FIXME: refactor
81-
*/
82-
lines.addEmpty()
83-
84-
ind.evaluatedMainActions().asSequence()
85-
.map { it.action }
86-
.filterIsInstance(RestCallAction::class.java)
87-
/*
88-
FIXME postLocationId() is not guaranteed to be unique...
89-
in fitness function it works because we handle it by taking last definition,
90-
but, here, if we refactor to declare it on its first use, we might end up with
91-
variable name clashes, unless we change the id to consider the action index, somehow
92-
*/
93-
.filter { it.saveCreatedResourceLocation }
94-
.map { it.creationLocationId() }
95-
// .filter { it.usePreviousLocationId != null }
96-
// .map { it.usePreviousLocationId }
97-
.distinct()
98-
.forEach { id ->
99-
val name = locationVar(id!!)
100-
when {
101-
format.isJava() -> lines.add("String $name = \"\";")
102-
format.isKotlin() -> lines.add("var $name : String? = \"\"")
103-
format.isJavaScript() -> lines.add("let $name = \"\";")
104-
format.isCsharp() -> lines.add("var $name = \"\";")
105-
format.isPython() -> {} // no need to declare variables
106-
// should never happen
107-
else -> throw IllegalStateException("Unsupported format $format")
108-
}
109-
}
110-
}
11170
}
11271

11372
override fun handleActionCalls(
@@ -149,9 +108,12 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
149108
}
150109

151110
protected fun locationVar(id: String): String {
111+
/*
112+
Ids are supposed to be unique, but might have invalid characters for a variable
113+
*/
152114
//TODO make sure name is syntactically valid
153-
//TODO use counters to make them unique
154-
return "location_${id.trim().replace(" ", "_")}"
115+
116+
return "location_${id.trim().replace(" ", "_").replace(Individual.LOCAL_ID_PREFIX_ACTION,"")}"
155117
}
156118

157119

@@ -421,12 +383,17 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
421383
when {
422384
format.isJavaOrKotlin() -> {
423385
val extract = "$resVarName.extract().header(\"location\")"
424-
lines.add("$location = $extract")
386+
if(format.isJava()){
387+
lines.add("String ")
388+
} else {
389+
lines.add("val ")
390+
}
391+
lines.append("$location = $extract")
425392
lines.appendSemicolon()
426393
lines.add("assertTrue(isValidURIorEmpty($location));")
427394
}
428395
format.isJavaScript() -> {
429-
lines.add("$location = $resVarName.header['location'];")
396+
lines.add("const $location = $resVarName.header['location'];")
430397
val validCheck = "${TestSuiteWriter.jsImport}.isValidURIorEmpty($location)"
431398
lines.add("expect($validCheck).toBe(true);")
432399
}
@@ -464,6 +431,12 @@ class RestTestCaseWriter : HttpWsTestCaseWriter {
464431
else -> throw IllegalStateException("Unhandled format: $format")
465432
}
466433

434+
when{
435+
format.isJavaScript() -> lines.add("const ")
436+
format.isJava() -> lines.add("String ")
437+
format.isKotlin() -> lines.add("val ")
438+
format.isPython() -> { /* nothing to do in Python */}
439+
}
467440
lines.add("${locationVar(call.creationLocationId())} = $baseUri + \"/\" + $extract")
468441
lines.appendSemicolon()
469442
}

core/src/main/kotlin/org/evomaster/core/search/Individual.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ abstract class Individual(
5757

5858
companion object{
5959
private val log = LoggerFactory.getLogger(Individual::class.java)
60+
61+
const val LOCAL_ID_PREFIX_ACTION = "ACTION_COMPONENT"
62+
const val LOCAL_ID_PREFIX_GENE = "GENE"
6063
}
6164

6265

@@ -99,9 +102,16 @@ abstract class Individual(
99102
/**
100103
* get local id based on the given counter
101104
*/
102-
private fun getLocalId(obj: StructuralElement, counter: Int) : String
103-
= "${if (obj is ActionComponent) "ACTION_COMPONENT" else if (obj is Gene) "GENE" else throw IllegalStateException("Only Generate local id for ActionComponent and Gene")}_$counter"
105+
private fun getLocalId(obj: StructuralElement, counter: Int) : String {
106+
107+
val prefix = when (obj) {
108+
is ActionComponent -> LOCAL_ID_PREFIX_ACTION
109+
is Gene -> LOCAL_ID_PREFIX_GENE
110+
else -> throw IllegalStateException("Only Generate local id for ActionComponent and Gene")
111+
}
104112

113+
return "${prefix}_$counter"
114+
}
105115
/**
106116
* Make a deep copy of this individual
107117
*/

0 commit comments

Comments
 (0)