Add json support

This commit is contained in:
Dominik Adam Przybysz 2014-12-13 17:44:53 +01:00
parent bbbc4a7049
commit 0805de459d
8 changed files with 184 additions and 103 deletions

View file

@ -16,32 +16,16 @@ class ContextExecutor {
this.mocks = new CopyOnWriteArrayList<>([initialMock]) this.mocks = new CopyOnWriteArrayList<>([initialMock])
httpServerWraper.createContext(path, { httpServerWraper.createContext(path, {
HttpExchange ex -> HttpExchange ex ->
String input = ex.requestBody.text Request request = new Request(ex.requestBody.text, ex.requestHeaders, ex.requestURI.query)
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(',')]
}
println "Mock received input" println "Mock received input"
for (Mock mock : mocks) { for (Mock mock : mocks) {
try { 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 && if (ex.requestMethod == mock.method &&
mock.predicate(xml) && mock.predicate(request)) {
mock.requestHeaders(headers) &&
mock.queryParams(queryParams)) {
println "Mock ${mock.name} invoked" println "Mock ${mock.name} invoked"
++mock.counter ++mock.counter
String response = mock.responseOk(xml) String response = mock.responseOk(request)
mock.responseHeaders(xml).each { mock.responseHeaders(request).each {
ex.responseHeaders.add(it.key as String, it.value as String) ex.responseHeaders.add(it.key as String, it.value as String)
} }
ex.sendResponseHeaders(mock.statusCode, response ? 0 : -1) 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) { int removeMock(String name) {
Mock mock = mocks.find { it.name == name } Mock mock = mocks.find { it.name == name }
if (mock) { if (mock) {

View file

@ -65,14 +65,6 @@ class HttpMockServer {
if(responseHeaders){ if(responseHeaders){
mock.responseHeaders = Eval.me(responseHeaders) as Closure 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 } HttpServerWraper child = childServers.find { it.port == mockPort }
if (!child) { if (!child) {
child = new HttpServerWraper(mockPort) child = new HttpServerWraper(mockPort)

View file

@ -7,14 +7,12 @@ class Mock {
final String name final String name
final String path final String path
final int port final int port
Closure predicate = { xml -> true } Closure predicate = { _ -> true }
Closure responseOk = { xml -> '' } Closure responseOk = { _ -> '' }
boolean soap = false boolean soap = false
int statusCode = 200 int statusCode = 200
String method = 'POST' String method = 'POST'
Closure requestHeaders = {hs -> true} Closure responseHeaders = {_ -> [:]}
Closure responseHeaders = {xml -> [:]}
Closure queryParams = {qs -> true}
int counter = 0 int counter = 0
Mock(String name, String path, int port) { Mock(String name, String path, int port) {

View 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(',')]
}
}
}

View file

@ -1,7 +1,11 @@
package pl.touk.mockserver.client package pl.touk.mockserver.client
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.apache.commons.lang3.StringEscapeUtils import org.apache.commons.lang3.StringEscapeUtils
@CompileStatic
@TypeChecked
class AddMockRequestData { class AddMockRequestData {
String name String name
String path String path
@ -12,8 +16,6 @@ class AddMockRequestData {
Integer statusCode Integer statusCode
Method method Method method
String responseHeaders String responseHeaders
String requestHeaders
String queryParams
void setPredicate(String predicate) { void setPredicate(String predicate) {
this.predicate = StringEscapeUtils.escapeXml11(predicate) this.predicate = StringEscapeUtils.escapeXml11(predicate)
@ -27,14 +29,6 @@ class AddMockRequestData {
this.responseHeaders = StringEscapeUtils.escapeXml11(responseHeaders) this.responseHeaders = StringEscapeUtils.escapeXml11(responseHeaders)
} }
void setRequestHeaders(String requestHeaders) {
this.requestHeaders = StringEscapeUtils.escapeXml11(requestHeaders)
}
void setQueryParams(String queryParams) {
this.queryParams = StringEscapeUtils.escapeXml11(queryParams)
}
enum Method { enum Method {
POST, POST,
GET, GET,

View file

@ -61,8 +61,6 @@ class ControlServerClient {
${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''} ${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''}
${data.method ? "<method>${data.method}</method>" : ''} ${data.method ? "<method>${data.method}</method>" : ''}
${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''} ${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''}
${data.requestHeaders ? "<requestHeaders>${data.requestHeaders}</requestHeaders>" : ''}
${data.queryParams ? "<queryParams>${data.queryParams}</queryParams>" : ''}
</addMock> </addMock>
""", ContentType.create("text/xml", "UTF-8")) """, ContentType.create("text/xml", "UTF-8"))
} }

View file

@ -1,5 +1,6 @@
package pl.touk.mockserver.client package pl.touk.mockserver.client
import groovy.json.JsonSlurper
import groovy.transform.PackageScope import groovy.transform.PackageScope
import groovy.util.slurpersupport.GPathResult import groovy.util.slurpersupport.GPathResult
import org.apache.http.HttpEntity import org.apache.http.HttpEntity
@ -7,7 +8,6 @@ import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.util.EntityUtils import org.apache.http.util.EntityUtils
class Util { class Util {
@PackageScope
static GPathResult extractXmlResponse(CloseableHttpResponse response){ static GPathResult extractXmlResponse(CloseableHttpResponse response){
HttpEntity entity = response.entity HttpEntity entity = response.entity
GPathResult xml = new XmlSlurper().parseText(EntityUtils.toString(entity)) GPathResult xml = new XmlSlurper().parseText(EntityUtils.toString(entity))
@ -22,4 +22,10 @@ class Util {
</soap-env:Envelope>""" </soap-env:Envelope>"""
} }
static Object extractJsonResponse(CloseableHttpResponse response) {
HttpEntity entity = response.entity
Object json = new JsonSlurper().parseText(EntityUtils.toString(entity))
EntityUtils.consumeQuietly(entity)
return json
}
} }

View file

@ -36,8 +36,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{xml -> "<goodResponseRest-${xml.name()}/>"}''', response: '''{req -> "<goodResponseRest-${req.xml.name()}/>"}''',
soap: false soap: false
)) ))
when: when:
@ -57,8 +57,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.soap.name() == 'request'}''',
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap-${req.soap.name()}/>"}''',
soap: true soap: true
)) ))
when: when:
@ -83,8 +83,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
soap: true soap: true
)) ))
and: and:
@ -101,8 +101,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
soap: true soap: true
)) ))
when: when:
@ -110,8 +110,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint2', path: '/testEndpoint2',
port: 9998, port: 9998,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
soap: true soap: true
)) ))
then: then:
@ -124,8 +124,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap-${req.name()}/>"}''',
soap: true soap: true
)) ))
and: and:
@ -135,8 +135,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request2'}''', predicate: '''{req -> req.xml.name() == 'request2'}''',
response: '''{xml -> "<goodResponseSoap2-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap2-${req.xml.name()}/>"}''',
soap: true soap: true
)) ))
} }
@ -147,15 +147,15 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{xml -> "<goodResponseRest-${xml.name()}/>"}''' response: '''{req -> "<goodResponseRest-${req.xml.name()}/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.soap.name() == 'request'}''',
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap-${req.soap.name()}/>"}''',
soap: true soap: true
)) ))
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint') HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
@ -182,15 +182,15 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest1', name: 'testRest1',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request1'}''', predicate: '''{req -> req.xml.name() == 'request1'}''',
response: '''{xml -> "<goodResponseRest1/>"}''' response: '''{req -> "<goodResponseRest1/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
path: secondPath, path: secondPath,
port: secondPort, port: secondPort,
predicate: '''{xml -> xml.name() == 'request2'}''', predicate: '''{req -> req.xml.name() == 'request2'}''',
response: '''{xml -> "<goodResponseRest2/>"}''' response: '''{req -> "<goodResponseRest2/>"}'''
)) ))
HttpPost firstRequest = new HttpPost('http://localhost:9999/test1') HttpPost firstRequest = new HttpPost('http://localhost:9999/test1')
firstRequest.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8")) firstRequest.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8"))
@ -243,8 +243,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest1', name: 'testRest1',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request2'}''', predicate: '''{req -> req.xml.name() == 'request2'}''',
response: '''{xml -> "<goodResponseRest2/>"}''' response: '''{req -> "<goodResponseRest2/>"}'''
)) ))
HttpPost request = new HttpPost('http://localhost:9999/test1') HttpPost request = new HttpPost('http://localhost:9999/test1')
request.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8")) request.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8"))
@ -262,8 +262,8 @@ class MockServerIntegrationTest extends Specification {
name: 'testSoap', name: 'testSoap',
path: '/testEndpoint2', path: '/testEndpoint2',
port: -1, port: -1,
predicate: '''{xml -> true}''', predicate: '''{_ -> true}''',
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''', response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
soap: true soap: true
)) ))
then: then:
@ -276,13 +276,13 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<defaultResponse/>"}''' response: '''{_ -> "<defaultResponse/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<getResponse/>"}''', response: '''{_ -> "<getResponse/>"}''',
method: AddMockRequestData.Method.GET method: AddMockRequestData.Method.GET
)) ))
HttpGet restGet = new HttpGet('http://localhost:9999/testEndpoint') HttpGet restGet = new HttpGet('http://localhost:9999/testEndpoint')
@ -299,13 +299,13 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<defaultResponse/>"}''' response: '''{_ -> "<defaultResponse/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<traceResponse/>"}''', response: '''{_ -> "<traceResponse/>"}''',
method: AddMockRequestData.Method.TRACE method: AddMockRequestData.Method.TRACE
)) ))
HttpTrace restTrace = new HttpTrace('http://localhost:9999/testEndpoint') HttpTrace restTrace = new HttpTrace('http://localhost:9999/testEndpoint')
@ -322,7 +322,7 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<defaultResponse/>"}''' response: '''{_ -> "<defaultResponse/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
@ -344,7 +344,7 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<defaultResponse/>"}''' response: '''{_ -> "<defaultResponse/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
@ -366,14 +366,14 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
response: '''{xml -> "<defaultResponse/>"}''' response: '''{_ -> "<defaultResponse/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request1'}''', predicate: '''{req -> req.xml.name() == 'request1'}''',
response: '''{xml -> "<goodResponseRest1/>"}''', response: '''{_ -> "<goodResponseRest1/>"}''',
method: AddMockRequestData.Method.PUT method: AddMockRequestData.Method.PUT
)) ))
HttpPut request = new HttpPut('http://localhost:9999/test1') HttpPut request = new HttpPut('http://localhost:9999/test1')
@ -391,13 +391,13 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
response: '''{xml -> "<defaultResponse/>"}''' response: '''{_ -> "<defaultResponse/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
response: '''{xml -> "<goodResponseRest1/>"}''', response: '''{_ -> "<goodResponseRest1/>"}''',
method: AddMockRequestData.Method.DELETE method: AddMockRequestData.Method.DELETE
)) ))
HttpDelete request = new HttpDelete('http://localhost:9999/test1') HttpDelete request = new HttpDelete('http://localhost:9999/test1')
@ -414,14 +414,14 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
response: '''{xml -> "<defaultResponse/>"}''' response: '''{_ -> "<defaultResponse/>"}'''
)) ))
controlServerClient.addMock(new AddMockRequestData( controlServerClient.addMock(new AddMockRequestData(
name: 'testRest2', name: 'testRest2',
path: '/test1', path: '/test1',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request1'}''', predicate: '''{req -> req.xml.name() == 'request1'}''',
response: '''{xml -> "<goodResponseRest1/>"}''', response: '''{_ -> "<goodResponseRest1/>"}''',
method: AddMockRequestData.Method.PATCH method: AddMockRequestData.Method.PATCH
)) ))
HttpPatch request = new HttpPatch('http://localhost:9999/test1') HttpPatch request = new HttpPatch('http://localhost:9999/test1')
@ -439,9 +439,9 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''', predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{xml -> "<goodResponse/>"}''', response: '''{_ -> "<goodResponse/>"}''',
responseHeaders: '''{ xml -> ['Input-Name':"${xml.name()}"]}''' responseHeaders: '''{ req -> ['Input-Name':"${req.xml.name()}"]}'''
)) ))
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint') HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
restPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8")) restPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8"))
@ -459,9 +459,9 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<goodResponse/>"}''', predicate: '''{ req -> req.headers['user-agent']?.startsWith('Mozilla') &&
requestHeaders: '''{ hs -> hs['user-agent']?.startsWith('Mozilla') && req.headers.pragma == 'no-cache'}''',
hs.pragma == 'no-cache'}''' response: '''{_ -> "<goodResponse/>"}'''
)) ))
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint') HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
restPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8")) restPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8"))
@ -488,9 +488,9 @@ class MockServerIntegrationTest extends Specification {
name: 'testRest', name: 'testRest',
path: '/testEndpoint', path: '/testEndpoint',
port: 9999, port: 9999,
response: '''{xml -> "<goodResponse/>"}''', predicate: '''{ req -> req.query['q'] == '15' &&
queryParams: '''{ qp -> qp['q'] == '15' && req.query.id == '1'}''',
qp.id == '1'}''' response: '''{_ -> "<goodResponse/>"}'''
)) ))
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint?q=15&id=1') HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint?q=15&id=1')
HttpPost badRestPost = new HttpPost('http://localhost:9999/testEndpoint?q=15&id=2') HttpPost badRestPost = new HttpPost('http://localhost:9999/testEndpoint?q=15&id=2')
@ -506,12 +506,57 @@ class MockServerIntegrationTest extends Specification {
restPostResponse.name() == 'goodResponse' restPostResponse.name() == 'goodResponse'
} }
//TODO def "should dispatch rest mock with post method and parameters"(){} def "should add mock that accepts only when request has specific body"() {
//TODO def "should dispatch rest mock with post method, response headers and request headers"(){} given:
controlServerClient.addMock(new AddMockRequestData(
name: 'testRest',
path: '/testEndpoint',
port: 9999,
predicate: '''{req -> req.text == 'hello=world&id=3'}''',
response: '''{_ -> "<goodResponse/>"}'''
))
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
restPost.entity = new StringEntity('hello=world&id=3', ContentType.create("text/plain", "UTF-8"))
HttpPost badRestPost = new HttpPost('http://localhost:9999/testEndpoint')
badRestPost.entity = new StringEntity('hello=world&id=2', ContentType.create("text/plain", "UTF-8"))
when:
CloseableHttpResponse badResponse = client.execute(badRestPost)
then:
GPathResult badRestPostResponse = Util.extractXmlResponse(badResponse)
badRestPostResponse.name() == 'invalidInput'
when:
CloseableHttpResponse response = client.execute(restPost)
then:
GPathResult restPostResponse = Util.extractXmlResponse(response)
restPostResponse.name() == 'goodResponse'
}
def "should add mock which response json to json"() {
given:
controlServerClient.addMock(new AddMockRequestData(
name: 'testRest',
path: '/testEndpoint',
port: 9999,
predicate: '''{req -> req.json.id == 1 && req.json.ar == ["a", true]}''',
response: '''{req -> """{"name":"goodResponse-${req.json.id}"}"""}'''
))
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
restPost.entity = new StringEntity('{"id":1, "ar":["a", true]}', ContentType.create("text/json", "UTF-8"))
HttpPost badRestPost = new HttpPost('http://localhost:9999/testEndpoint')
badRestPost.entity = new StringEntity('{"id":1, "ar":["a", false]}', ContentType.create("text/json", "UTF-8"))
when:
CloseableHttpResponse badResponse = client.execute(badRestPost)
then:
GPathResult badRestPostResponse = Util.extractXmlResponse(badResponse)
badRestPostResponse.name() == 'invalidInput'
when:
CloseableHttpResponse response = client.execute(restPost)
then:
Object restPostResponse = Util.extractJsonResponse(response)
restPostResponse.name == 'goodResponse-1'
}
//TODO def "should get mock report"(){} //TODO def "should get mock report"(){}
//TODO def "should get list mocks"(){} //TODO def "should get list mocks"(){}
//TODO def "should validate mock when creating" //TODO def "should validate mock when creating"
//TODO def "should handle json input and output"(){}
} }