Add support for each http method
This commit is contained in:
parent
b3e3dfb3f3
commit
1be072f440
6 changed files with 192 additions and 24 deletions
|
@ -18,10 +18,9 @@ class ContextExecutor {
|
||||||
HttpExchange ex ->
|
HttpExchange ex ->
|
||||||
String input = ex.requestBody.text
|
String input = ex.requestBody.text
|
||||||
println "Mock received input"
|
println "Mock received input"
|
||||||
GPathResult inputXml = new XmlSlurper().parseText(input)
|
|
||||||
for (Mock mock : mocks){
|
for (Mock mock : mocks){
|
||||||
GPathResult xml = inputXml
|
|
||||||
try {
|
try {
|
||||||
|
GPathResult xml = input ? new XmlSlurper().parseText(input) : null
|
||||||
if (mock.soap) {
|
if (mock.soap) {
|
||||||
if(xml.name() == 'Envelope' && xml.Body.size() > 0){
|
if(xml.name() == 'Envelope' && xml.Body.size() > 0){
|
||||||
xml = getSoapBodyContent(xml)
|
xml = getSoapBodyContent(xml)
|
||||||
|
@ -29,7 +28,7 @@ class ContextExecutor {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (xml != null && mock.predicate(xml)) {
|
if (ex.requestMethod == mock.method && mock.predicate(xml)) {
|
||||||
ex.sendResponseHeaders(mock.statusCode, 0)
|
ex.sendResponseHeaders(mock.statusCode, 0)
|
||||||
println "Mock ${mock.name} invoked"
|
println "Mock ${mock.name} invoked"
|
||||||
++mock.counter
|
++mock.counter
|
||||||
|
|
|
@ -57,6 +57,10 @@ class HttpMockServer {
|
||||||
if(statusCode){
|
if(statusCode){
|
||||||
mock.statusCode = Integer.valueOf(statusCode)
|
mock.statusCode = Integer.valueOf(statusCode)
|
||||||
}
|
}
|
||||||
|
String method = request.method
|
||||||
|
if(method){
|
||||||
|
mock.method = method
|
||||||
|
}
|
||||||
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)
|
||||||
|
|
|
@ -11,7 +11,7 @@ class Mock {
|
||||||
Closure responseOk = { xml -> '' }
|
Closure responseOk = { xml -> '' }
|
||||||
boolean soap = false
|
boolean soap = false
|
||||||
int statusCode = 200
|
int statusCode = 200
|
||||||
//TODO add http method - default POST
|
String method = 'POST'
|
||||||
//TODO add request headers - default [:]
|
//TODO add request headers - default [:]
|
||||||
//TODO add response headers - default [:]
|
//TODO add response headers - default [:]
|
||||||
int counter = 0
|
int counter = 0
|
||||||
|
|
|
@ -10,14 +10,24 @@ class AddMockRequestData {
|
||||||
String response
|
String response
|
||||||
Boolean soap
|
Boolean soap
|
||||||
Integer statusCode
|
Integer statusCode
|
||||||
|
Method method
|
||||||
|
|
||||||
void setPredicate(String predicate){
|
void setPredicate(String predicate) {
|
||||||
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
||||||
}
|
}
|
||||||
|
|
||||||
void setResponse(String response){
|
void setResponse(String response) {
|
||||||
this.response = StringEscapeUtils.escapeXml11(response)
|
this.response = StringEscapeUtils.escapeXml11(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Method {
|
||||||
|
POST,
|
||||||
|
GET,
|
||||||
|
DELETE,
|
||||||
|
PUT,
|
||||||
|
TRACE,
|
||||||
|
HEAD,
|
||||||
|
OPTIONS,
|
||||||
|
PATCH
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,10 +55,11 @@ class ControlServerClient {
|
||||||
<name>${data.name}</name>
|
<name>${data.name}</name>
|
||||||
<path>${data.path}</path>
|
<path>${data.path}</path>
|
||||||
<port>${data.port}</port>
|
<port>${data.port}</port>
|
||||||
${data.predicate != null ? "<predicate>${data.predicate}</predicate>" : ''}
|
${data.predicate ? "<predicate>${data.predicate}</predicate>" : ''}
|
||||||
${data.response != null ? "<response>${data.response}</response>" : ''}
|
${data.response ? "<response>${data.response}</response>" : ''}
|
||||||
${data.soap != null ? "<soap>${data.soap}</soap>" : ''}
|
${data.soap != null ? "<soap>${data.soap}</soap>" : ''}
|
||||||
${data.statusCode != null ? "<statusCode>${data.statusCode}</statusCode>" : ''}
|
${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''}
|
||||||
|
${data.method ? "<method>${data.method}</method>" : ''}
|
||||||
</addMock>
|
</addMock>
|
||||||
""", ContentType.create("text/xml", "UTF-8"))
|
""", ContentType.create("text/xml", "UTF-8"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
package pl.touk.mockserver.server
|
package pl.touk.mockserver.server
|
||||||
|
|
||||||
import groovy.util.slurpersupport.GPathResult
|
import groovy.util.slurpersupport.GPathResult
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse
|
import org.apache.http.client.methods.*
|
||||||
import org.apache.http.client.methods.HttpPost
|
|
||||||
import org.apache.http.entity.ContentType
|
import org.apache.http.entity.ContentType
|
||||||
import org.apache.http.entity.StringEntity
|
import org.apache.http.entity.StringEntity
|
||||||
import org.apache.http.impl.client.CloseableHttpClient
|
import org.apache.http.impl.client.CloseableHttpClient
|
||||||
|
@ -257,7 +256,7 @@ class MockServerIntegrationTest extends Specification {
|
||||||
secondXmlResponse.name() == 'invalidInput'
|
secondXmlResponse.name() == 'invalidInput'
|
||||||
}
|
}
|
||||||
|
|
||||||
def "should inform that there was problem during adding mock - invalid port"(){
|
def "should inform that there was problem during adding mock - invalid port"() {
|
||||||
when:
|
when:
|
||||||
controlServerClient.addMock(new AddMockRequestData(
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
name: 'testSoap',
|
name: 'testSoap',
|
||||||
|
@ -271,21 +270,176 @@ class MockServerIntegrationTest extends Specification {
|
||||||
thrown(InvalidMockDefinitionException)
|
thrown(InvalidMockDefinitionException)
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO def "should dispatch rest mock with post method"(){}
|
def "should dispatch rest mock with get method"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<defaultResponse/>"}'''
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<getResponse/>"}''',
|
||||||
|
method: AddMockRequestData.Method.GET
|
||||||
|
))
|
||||||
|
HttpGet restGet = new HttpGet('http://localhost:9999/testEndpoint')
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(restGet)
|
||||||
|
then:
|
||||||
|
GPathResult restPostResponse = Util.extractXmlResponse(response)
|
||||||
|
restPostResponse.name() == 'getResponse'
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should dispatch rest mock with trace method"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<defaultResponse/>"}'''
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<traceResponse/>"}''',
|
||||||
|
method: AddMockRequestData.Method.TRACE
|
||||||
|
))
|
||||||
|
HttpTrace restTrace = new HttpTrace('http://localhost:9999/testEndpoint')
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(restTrace)
|
||||||
|
then:
|
||||||
|
GPathResult restPostResponse = Util.extractXmlResponse(response)
|
||||||
|
restPostResponse.name() == 'traceResponse'
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should dispatch rest mock with head method"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<defaultResponse/>"}'''
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
method: AddMockRequestData.Method.HEAD
|
||||||
|
))
|
||||||
|
HttpHead restHead = new HttpHead('http://localhost:9999/testEndpoint')
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(restHead)
|
||||||
|
then:
|
||||||
|
response.statusLine.statusCode == 200
|
||||||
|
EntityUtils.consumeQuietly(response.entity)
|
||||||
|
//TODO check headers
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should dispatch rest mock with options method"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<defaultResponse/>"}'''
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
method: AddMockRequestData.Method.OPTIONS
|
||||||
|
))
|
||||||
|
HttpOptions restOptions = new HttpOptions('http://localhost:9999/testEndpoint')
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(restOptions)
|
||||||
|
then:
|
||||||
|
response.statusLine.statusCode == 200
|
||||||
|
EntityUtils.consumeQuietly(response.entity)
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should dispatch rest mock with put method"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<defaultResponse/>"}'''
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
predicate: '''{xml -> xml.name() == 'request1'}''',
|
||||||
|
response: '''{xml -> "<goodResponseRest1/>"}''',
|
||||||
|
method: AddMockRequestData.Method.PUT
|
||||||
|
))
|
||||||
|
HttpPut request = new HttpPut('http://localhost:9999/test1')
|
||||||
|
request.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8"))
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(request)
|
||||||
|
then:
|
||||||
|
GPathResult secondXmlResponse = Util.extractXmlResponse(response)
|
||||||
|
secondXmlResponse.name() == 'goodResponseRest1'
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should dispatch rest mock with delete method"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<defaultResponse/>"}'''
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<goodResponseRest1/>"}''',
|
||||||
|
method: AddMockRequestData.Method.DELETE
|
||||||
|
))
|
||||||
|
HttpDelete request = new HttpDelete('http://localhost:9999/test1')
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(request)
|
||||||
|
then:
|
||||||
|
GPathResult secondXmlResponse = Util.extractXmlResponse(response)
|
||||||
|
secondXmlResponse.name() == 'goodResponseRest1'
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should dispatch rest mock with patch method"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<defaultResponse/>"}'''
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
predicate: '''{xml -> xml.name() == 'request1'}''',
|
||||||
|
response: '''{xml -> "<goodResponseRest1/>"}''',
|
||||||
|
method: AddMockRequestData.Method.PATCH
|
||||||
|
))
|
||||||
|
HttpPatch request = new HttpPatch('http://localhost:9999/test1')
|
||||||
|
request.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8"))
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(request)
|
||||||
|
then:
|
||||||
|
GPathResult secondXmlResponse = Util.extractXmlResponse(response)
|
||||||
|
secondXmlResponse.name() == 'goodResponseRest1'
|
||||||
|
}
|
||||||
|
|
||||||
//TODO def "should dispatch rest mock with post method and request headers"(){}
|
//TODO def "should dispatch rest mock with post method and request headers"(){}
|
||||||
//TODO def "should dispatch rest mock with post method and response headers"(){}
|
//TODO def "should dispatch rest mock with post method and response headers"(){}
|
||||||
//TODO def "should dispatch rest mock with get method"(){}
|
|
||||||
//TODO def "should dispatch rest mock with get method and request headers"(){}
|
|
||||||
//TODO def "should dispatch rest mock with get method and response headers"(){}
|
|
||||||
//TODO def "should dispatch rest mock with put method"(){}
|
|
||||||
//TODO def "should dispatch rest mock with put method and request headers"(){}
|
|
||||||
//TODO def "should dispatch rest mock with put method and response headers"(){}
|
|
||||||
//TODO def "should dispatch rest mock with delete method"(){}
|
|
||||||
//TODO def "should dispatch rest mock with delete method and request headers"(){}
|
|
||||||
//TODO def "should dispatch rest mock with delete method and response headers"(){}
|
|
||||||
//TODO def "should dispatch rest mocks with all methods"(){}
|
|
||||||
|
|
||||||
//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"(){}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue