Skip to content

Commit

Permalink
You should still be able to open the input stream after XML/JSON pars…
Browse files Browse the repository at this point in the history
…ing fails.
  • Loading branch information
Jorge L. Williams committed Jun 7, 2016
1 parent 36af5bd commit 673e9d6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class CheckerServletRequest(val request : HttpServletRequest) extends HttpServle
Array[String]()
}

private var clearInput = false

def clearInputStream : Unit = {
clearInput = true
}

def pathToSegment(uriLevel : Int) : String = {
"/" + URISegment.slice(0, uriLevel).reduceLeft( _ + "/" +_ )
}
Expand Down Expand Up @@ -154,7 +160,9 @@ class CheckerServletRequest(val request : HttpServletRequest) extends HttpServle
}

override def getInputStream : ServletInputStream = {
if (parsedXML != null) {
if (clearInput) {
new ByteArrayServletInputStream(Array[Byte]())
} else if (parsedXML != null) {
var transformer : Transformer = null
val bout = new ByteArrayOutputStream()
try {
Expand Down Expand Up @@ -185,7 +193,9 @@ class CheckerServletRequest(val request : HttpServletRequest) extends HttpServle
}

override def getReader : BufferedReader = {
if (parsedXML != null) {
if (clearInput) {
new BufferedReader(new InputStreamReader (getInputStream, "UTF-8"))
} else if (parsedXML != null) {
new BufferedReader(new InputStreamReader (getInputStream, parsedXML.getInputEncoding))
} else if (parsedJSON != null) {
new BufferedReader(new InputStreamReader (getInputStream, "UTF-8"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class WellFormedJSON(id : String, label : String, val priority : Long, next : Ar
} catch {
case e : Exception => req.contentError = e
req.contentErrorPriority = priority
req.clearInputStream
} finally {
if (parser != null) returnParser(parser)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class WellFormedXML(id : String, label : String, val priority : Long, next : Arr
}catch {
case e : Exception => req.contentError = e
req.contentErrorPriority = priority
req.clearInputStream
}
finally {
if (parser != null) returnParser(parser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,21 @@ class StepSuite extends BaseStepSuite with MockitoSugar {
assert (req1.contentErrorPriority == 150)
}

test("In a WellFormedXML step, if the content is not well formed XML, you should still be able to open the input stream") {
val wfx = new WellFormedXML("WFXML", "WFXML", 150, Array[Step]())
val req1 = request("PUT", "/a/b", "application/xml", """<validXML xmlns='http://valid'>""")

wfx.checkStep (req1, response, chain, 0)
assert (req1.getInputStream.available == 0)
assert (req1.getInputStream.read == -1)

val req2 = request("PUT", "/a/b", "application/xml", """{ \"bla\" : 55 }""")

wfx.checkStep (req2, response, chain, 1)
assert (req2.getInputStream.available == 0)
assert (req2.getInputStream.read == -1)
}

test("In a WellFormedXML step, XML in the same request should not be parsed twice") {
val wfx = new WellFormedXML("WFXML", "WFXML", 10, Array[Step]())
val req1 = request("PUT", "/a/b", "application/xml", <validXML xmlns="http://valid"/>)
Expand Down Expand Up @@ -793,6 +808,20 @@ class StepSuite extends BaseStepSuite with MockitoSugar {
assert (req2.contentError.isInstanceOf[JsonParseException])
}

test("In a WellFormedJSON step, if the content contains JSON that is not well-formed, you should still be able to access the input stream") {
val wfj = new WellFormedJSON("WFJSON", "WFJSON", 10, Array[Step]())
val req1 = request("PUT", "/a/b", "application/json", """ { "valid" : ture } """)
wfj.checkStep (req1, response, chain, 0)
assert (req1.getInputStream.available == 0)
assert (req1.getInputStream.read == -1)

val req2 = request("PUT", "/a/b", "application/json", """ <json /> """)
wfj.checkStep (req2, response, chain, 1)
assert (req2.getInputStream.available == 0)
assert (req2.getInputStream.read == -1)
}


test("In a WellFormedJSON step, if the content contains JSON that is not well-formed, then the request should contain the correct contentErrorPriority") {
val wfj = new WellFormedJSON("WFJSON", "WFJSON", 1000, Array[Step]())
val req1 = request("PUT", "/a/b", "application/json", """ { "valid" : ture } """)
Expand Down

0 comments on commit 673e9d6

Please sign in to comment.