Add counter for mock invocations
This commit is contained in:
parent
876c29e57d
commit
15ed01cc0a
5 changed files with 131 additions and 112 deletions
|
@ -0,0 +1,44 @@
|
|||
package com.blogspot.przybyszd.mockserver
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange
|
||||
import groovy.util.slurpersupport.GPathResult
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
class ContextExecutor {
|
||||
private final HttpServerWraper httpServerWraper
|
||||
private final String path
|
||||
private final List<Mock> mocks
|
||||
|
||||
ContextExecutor(HttpServerWraper httpServerWraper, String path, Mock initialMock) {
|
||||
this.httpServerWraper = httpServerWraper
|
||||
this.path = path
|
||||
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
||||
httpServerWraper.createContext(path,{
|
||||
HttpExchange ex ->
|
||||
ex.sendResponseHeaders(200, 0)
|
||||
String input = ex.requestBody.text
|
||||
println "Mock received input"
|
||||
GPathResult xml = new XmlSlurper().parseText(input)
|
||||
for (Mock mock : mocks){
|
||||
if(mock.predicate(xml)){
|
||||
println "Mock ${mock.name} invoked"
|
||||
++mock.counter
|
||||
ex.responseBody << mock.responseOk(xml)
|
||||
ex.responseBody.close()
|
||||
return
|
||||
}
|
||||
}
|
||||
ex.responseBody << "<invalidInput/>"
|
||||
ex.responseBody.close()
|
||||
})
|
||||
}
|
||||
|
||||
int removeMock(String name) {
|
||||
Mock mock = mocks.find {it.name == name}
|
||||
if(mock){
|
||||
mocks.remove(mock)
|
||||
}
|
||||
return mock.counter
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue