Skip to content
Open
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
@@ -0,0 +1,55 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.base

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
@RequestMapping(path = ["/api/resources"])
@RestController
open class HttpInvalidLocationApplication {

companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(HttpInvalidLocationApplication::class.java, *args)
}

private val data = mutableMapOf<Int, String>()

fun reset(){
data.clear()
}
}


@GetMapping(path = ["/{id}"])
open fun get(@PathVariable("id") id: Int): ResponseEntity<String> {
val value = data[id] ?: return ResponseEntity.status(404).build()
return ResponseEntity.status(200).body(value)
}


@PutMapping(path = ["/{id}"])
open fun put(
@PathVariable("id") id: Int
): ResponseEntity<Any> {

val isNew = !data.containsKey(id)
data[id] = "$id"

// bug: Location header points to a different id than the one
// actually stored, so a follow-up GET on it will return 404

val status = if (isNew) 201 else 200
return ResponseEntity.status(status)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add test case for when the relative path is invalid, eg, /somePathThatDoesNotExist, and also full URL, eg, http://localhost:$port/api/resources/${id+1000}

.header("Location", "/api/resources/${id + 1000}")
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.fullpath

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.support.ServletUriComponentsBuilder

@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
@RequestMapping(path = ["/api/resources"])
@RestController
open class HttpInvalidLocationFullPathApplication {

companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(HttpInvalidLocationFullPathApplication::class.java, *args)
}

private val data = mutableMapOf<Int, String>()

fun reset(){
data.clear()
}
}


@GetMapping(path = ["/{id}"])
open fun get(@PathVariable("id") id: Int): ResponseEntity<String> {
val value = data[id] ?: return ResponseEntity.status(404).build()
return ResponseEntity.status(200).body(value)
}


@PutMapping(path = ["/{id}"])
open fun put(
@PathVariable("id") id: Int
): ResponseEntity<Any> {

val isNew = !data.containsKey(id)
data[id] = "$id"

val location = ServletUriComponentsBuilder
.fromCurrentContextPath()
.path("/api/resources/{id}")
.buildAndExpand(id + 1000)
.toUri()

val status = if (isNew) 201 else 200
return ResponseEntity.status(status)
.header("Location", location.toString())
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.locationget

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
@RequestMapping(path = ["/api/resources"])
@RestController
open class HttpInvalidLocationGetApplication {

companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(HttpInvalidLocationGetApplication::class.java, *args)
}

fun reset(){
}
}


@GetMapping(path = ["/{id}"])
open fun get(@PathVariable("id") id: Int): ResponseEntity<String> {
return ResponseEntity.status(200).header("Location", "/api/resources/${id + 1000}/notfound").body("Resource with id $id")
}

@GetMapping(path = ["/{id}/notfound"])
open fun getNotFound(@PathVariable("id") id: Int): ResponseEntity<String> {
return ResponseEntity.status(404).body("Not found")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.notvalidpath

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
@RequestMapping(path = ["/api/resources"])
@RestController
open class HttpInvalidLocationNotValidApplication {

companion object {
@JvmStatic
fun main(args: Array<String>) {
SpringApplication.run(HttpInvalidLocationNotValidApplication::class.java, *args)
}

private val data = mutableMapOf<Int, String>()

fun reset(){
data.clear()
}
}


@GetMapping(path = ["/{id}"])
open fun get(@PathVariable("id") id: Int): ResponseEntity<String> {
val value = data[id] ?: return ResponseEntity.status(404).build()
return ResponseEntity.status(200).body(value)
}


@PutMapping(path = ["/{id}"])
open fun put(
@PathVariable("id") id: Int
): ResponseEntity<Any> {

val isNew = !data.containsKey(id)
data[id] = "$id"

// bug: Location header points to a different id than the one
// actually stored, so a follow-up GET on it will return 404

val status = if (isNew) 201 else 200
return ResponseEntity.status(status)
.header("Location", "/somePathThatDoesNotExist")
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation

import com.foo.rest.examples.spring.openapi.v3.SpringController
import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.base.HttpInvalidLocationApplication


class HttpInvalidLocationController: SpringController(HttpInvalidLocationApplication::class.java){

override fun resetStateOfSUT() {
HttpInvalidLocationApplication.reset()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.fullpath

import com.foo.rest.examples.spring.openapi.v3.SpringController
import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.base.HttpInvalidLocationApplication
import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.notvalidpath.HttpInvalidLocationNotValidApplication


class HttpInvalidLocationFullPathController: SpringController(HttpInvalidLocationFullPathApplication::class.java){

override fun resetStateOfSUT() {
HttpInvalidLocationFullPathApplication.reset()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.locationget

import com.foo.rest.examples.spring.openapi.v3.SpringController
import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.base.HttpInvalidLocationApplication
import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.locationget.HttpInvalidLocationGetApplication


class HttpInvalidLocationGetController: SpringController(HttpInvalidLocationGetApplication::class.java){

override fun resetStateOfSUT() {
HttpInvalidLocationGetApplication.reset()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.notvalidpath

import com.foo.rest.examples.spring.openapi.v3.SpringController
import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.base.HttpInvalidLocationApplication
import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.notvalidpath.HttpInvalidLocationNotValidApplication


class HttpInvalidLocationNotValidController: SpringController(HttpInvalidLocationNotValidApplication::class.java){

override fun resetStateOfSUT() {
HttpInvalidLocationNotValidApplication.reset()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.evomaster.e2etests.spring.openapi.v3.httporacle.invalidlocation

import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.HttpInvalidLocationController
import org.evomaster.core.problem.enterprise.DetectedFaultUtils
import org.evomaster.core.problem.enterprise.ExperimentalFaultCategory
import org.evomaster.e2etests.spring.openapi.v3.SpringTestBase
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test

class HttpInvalidLocationEMTest : SpringTestBase(){

companion object {
@BeforeAll
@JvmStatic
fun init() {
initClass(HttpInvalidLocationController())
}
}


@Test
fun testRunEM() {

runTestHandlingFlakyAndCompilation(
"HttpInvalidLocationEM",
20
) { args: MutableList<String> ->

setOption(args, "security", "false")
setOption(args, "schemaOracles", "false")
setOption(args, "httpOracles", "true")
setOption(args, "useExperimentalOracles", "true")

val solution = initAndRun(args)

assertTrue(solution.individuals.size >= 1)

val faults = DetectedFaultUtils.getDetectedFaultCategories(solution)
assertTrue({ ExperimentalFaultCategory.HTTP_INVALID_LOCATION in faults })
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.evomaster.e2etests.spring.openapi.v3.httporacle.invalidlocation

import com.foo.rest.examples.spring.openapi.v3.httporacle.invalidlocation.fullpath.HttpInvalidLocationFullPathController
import org.evomaster.core.problem.enterprise.DetectedFaultUtils
import org.evomaster.core.problem.enterprise.ExperimentalFaultCategory
import org.evomaster.e2etests.spring.openapi.v3.SpringTestBase
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test

class HttpInvalidLocationFullPathEMTest : SpringTestBase(){

companion object {
@BeforeAll
@JvmStatic
fun init() {
initClass(HttpInvalidLocationFullPathController())
}
}


@Test
fun testRunEM() {

runTestHandlingFlakyAndCompilation(
"HttpInvalidLocationFullPathEM",
20
) { args: MutableList<String> ->

setOption(args, "security", "false")
setOption(args, "schemaOracles", "false")
setOption(args, "httpOracles", "true")
setOption(args, "useExperimentalOracles", "true")

val solution = initAndRun(args)

assertTrue(solution.individuals.size >= 1)

val faults = DetectedFaultUtils.getDetectedFaultCategories(solution)
assertTrue({ ExperimentalFaultCategory.HTTP_INVALID_LOCATION in faults })
}
}
}
Loading
Loading