Add json support
This commit is contained in:
parent
bbbc4a7049
commit
0805de459d
8 changed files with 184 additions and 103 deletions
|
@ -16,32 +16,16 @@ class ContextExecutor {
|
|||
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
||||
httpServerWraper.createContext(path, {
|
||||
HttpExchange ex ->
|
||||
String input = ex.requestBody.text
|
||||
Map<String, String> queryParams = ex.requestURI.query?.split('&')?.collectEntries {
|
||||
String[] keyValue = it.split('='); [(keyValue[0]): keyValue[1]]
|
||||
} ?: [:]
|
||||
Map<String, String> headers = ex.requestHeaders.collectEntries {
|
||||
[it.key.toLowerCase(), it.value.join(',')]
|
||||
}
|
||||
Request request = new Request(ex.requestBody.text, ex.requestHeaders, ex.requestURI.query)
|
||||
println "Mock received input"
|
||||
for (Mock mock : mocks) {
|
||||
try {
|
||||
GPathResult xml = input ? new XmlSlurper().parseText(input) : null
|
||||
if (mock.soap) {
|
||||
if (xml.name() == 'Envelope' && xml.Body.size() > 0) {
|
||||
xml = getSoapBodyContent(xml)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if (ex.requestMethod == mock.method &&
|
||||
mock.predicate(xml) &&
|
||||
mock.requestHeaders(headers) &&
|
||||
mock.queryParams(queryParams)) {
|
||||
mock.predicate(request)) {
|
||||
println "Mock ${mock.name} invoked"
|
||||
++mock.counter
|
||||
String response = mock.responseOk(xml)
|
||||
mock.responseHeaders(xml).each {
|
||||
String response = mock.responseOk(request)
|
||||
mock.responseHeaders(request).each {
|
||||
ex.responseHeaders.add(it.key as String, it.value as String)
|
||||
}
|
||||
ex.sendResponseHeaders(mock.statusCode, response ? 0 : -1)
|
||||
|
@ -61,10 +45,6 @@ class ContextExecutor {
|
|||
})
|
||||
}
|
||||
|
||||
private static GPathResult getSoapBodyContent(GPathResult xml) {
|
||||
return xml.Body.'**'[1]
|
||||
}
|
||||
|
||||
int removeMock(String name) {
|
||||
Mock mock = mocks.find { it.name == name }
|
||||
if (mock) {
|
||||
|
|
|
@ -65,14 +65,6 @@ class HttpMockServer {
|
|||
if(responseHeaders){
|
||||
mock.responseHeaders = Eval.me(responseHeaders) as Closure
|
||||
}
|
||||
String requestHeaders = request.requestHeaders
|
||||
if(requestHeaders){
|
||||
mock.requestHeaders = Eval.me(requestHeaders) as Closure
|
||||
}
|
||||
String queryParams = request.queryParams
|
||||
if(queryParams){
|
||||
mock.queryParams = Eval.me(queryParams) as Closure
|
||||
}
|
||||
HttpServerWraper child = childServers.find { it.port == mockPort }
|
||||
if (!child) {
|
||||
child = new HttpServerWraper(mockPort)
|
||||
|
|
|
@ -7,14 +7,12 @@ class Mock {
|
|||
final String name
|
||||
final String path
|
||||
final int port
|
||||
Closure predicate = { xml -> true }
|
||||
Closure responseOk = { xml -> '' }
|
||||
Closure predicate = { _ -> true }
|
||||
Closure responseOk = { _ -> '' }
|
||||
boolean soap = false
|
||||
int statusCode = 200
|
||||
String method = 'POST'
|
||||
Closure requestHeaders = {hs -> true}
|
||||
Closure responseHeaders = {xml -> [:]}
|
||||
Closure queryParams = {qs -> true}
|
||||
Closure responseHeaders = {_ -> [:]}
|
||||
int counter = 0
|
||||
|
||||
Mock(String name, String path, int port) {
|
||||
|
|
68
src/main/groovy/pl/touk/mockserver/server/Request.groovy
Normal file
68
src/main/groovy/pl/touk/mockserver/server/Request.groovy
Normal file
|
@ -0,0 +1,68 @@
|
|||
package pl.touk.mockserver.server
|
||||
|
||||
import com.sun.net.httpserver.Headers
|
||||
import groovy.json.JsonSlurper
|
||||
import groovy.util.slurpersupport.GPathResult
|
||||
|
||||
class Request {
|
||||
final String text
|
||||
final Map<String,String> headers
|
||||
final Map<String,String> query
|
||||
final GPathResult xml
|
||||
final GPathResult soap
|
||||
final Object json
|
||||
|
||||
Request(String text, Headers headers, String query) {
|
||||
this.text = text
|
||||
this.headers = headersToMap(headers)
|
||||
this.query = queryParamsToMap(query)
|
||||
this.xml = inputToXml(text)
|
||||
this.soap = inputToSoap(xml)
|
||||
this.json= inputToJson(text)
|
||||
}
|
||||
|
||||
private static GPathResult inputToXml(String text) {
|
||||
try{
|
||||
return new XmlSlurper().parseText(text)
|
||||
}catch (Exception _){
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private static GPathResult inputToSoap(GPathResult xml) {
|
||||
try{
|
||||
if (xml.name() == 'Envelope' && xml.Body.size() > 0) {
|
||||
return getSoapBodyContent(xml)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}catch (Exception _){
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private static GPathResult getSoapBodyContent(GPathResult xml) {
|
||||
return xml.Body.'**'[1]
|
||||
}
|
||||
|
||||
private static Object inputToJson(String text) {
|
||||
try{
|
||||
return new JsonSlurper().parseText(text)
|
||||
}catch (Exception _){
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, String> queryParamsToMap(String query) {
|
||||
return query?.split('&')?.collectEntries {
|
||||
String[] keyValue = it.split('='); [(keyValue[0]): keyValue[1]]
|
||||
} ?: [:]
|
||||
}
|
||||
|
||||
private static Map<String, String> headersToMap(Headers headers) {
|
||||
return headers.collectEntries {
|
||||
[it.key.toLowerCase(), it.value.join(',')]
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue