Add setting status code in mock
This commit is contained in:
parent
a7c6050e09
commit
b3e3dfb3f3
7 changed files with 118 additions and 35 deletions
|
@ -16,7 +16,6 @@ class ContextExecutor {
|
||||||
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
||||||
httpServerWraper.createContext(path,{
|
httpServerWraper.createContext(path,{
|
||||||
HttpExchange ex ->
|
HttpExchange ex ->
|
||||||
ex.sendResponseHeaders(200, 0)
|
|
||||||
String input = ex.requestBody.text
|
String input = ex.requestBody.text
|
||||||
println "Mock received input"
|
println "Mock received input"
|
||||||
GPathResult inputXml = new XmlSlurper().parseText(input)
|
GPathResult inputXml = new XmlSlurper().parseText(input)
|
||||||
|
@ -31,6 +30,7 @@ class ContextExecutor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (xml != null && mock.predicate(xml)) {
|
if (xml != null && mock.predicate(xml)) {
|
||||||
|
ex.sendResponseHeaders(mock.statusCode, 0)
|
||||||
println "Mock ${mock.name} invoked"
|
println "Mock ${mock.name} invoked"
|
||||||
++mock.counter
|
++mock.counter
|
||||||
String response = mock.responseOk(xml)
|
String response = mock.responseOk(xml)
|
||||||
|
@ -42,6 +42,7 @@ class ContextExecutor {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ex.sendResponseHeaders(404, 0)
|
||||||
ex.responseBody << "<invalidInput/>"
|
ex.responseBody << "<invalidInput/>"
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
})
|
})
|
||||||
|
|
|
@ -17,7 +17,6 @@ class HttpMockServer {
|
||||||
|
|
||||||
httpServerWraper.createContext('/serverControl', {
|
httpServerWraper.createContext('/serverControl', {
|
||||||
HttpExchange ex ->
|
HttpExchange ex ->
|
||||||
ex.sendResponseHeaders(200, 0)
|
|
||||||
try{
|
try{
|
||||||
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
||||||
if(ex.requestMethod== 'POST' && request.name() == 'addMock'){
|
if(ex.requestMethod== 'POST' && request.name() == 'addMock'){
|
||||||
|
@ -41,10 +40,23 @@ class HttpMockServer {
|
||||||
println "Adding $name"
|
println "Adding $name"
|
||||||
String mockPath = request.path
|
String mockPath = request.path
|
||||||
int mockPort = Integer.valueOf(request.port as String)
|
int mockPort = Integer.valueOf(request.port as String)
|
||||||
Closure predicate = Eval.me(request.predicate as String) as Closure
|
Mock mock = new Mock(name, mockPath, mockPort)
|
||||||
Closure okResponse = Eval.me(request.response as String) as Closure
|
String predicate = request.predicate
|
||||||
boolean soap = Boolean.valueOf(request.soap as String)
|
if(predicate){
|
||||||
Mock mock = new Mock(name, mockPath, mockPort, predicate, okResponse, soap)
|
mock.predicate = Eval.me(predicate) as Closure
|
||||||
|
}
|
||||||
|
String okResponse = request.response
|
||||||
|
if(okResponse){
|
||||||
|
mock.responseOk = Eval.me(okResponse) as Closure
|
||||||
|
}
|
||||||
|
String soap = request.soap
|
||||||
|
if(soap){
|
||||||
|
mock.soap = Boolean.valueOf(soap)
|
||||||
|
}
|
||||||
|
String statusCode = request.statusCode
|
||||||
|
if(statusCode){
|
||||||
|
mock.statusCode = Integer.valueOf(statusCode)
|
||||||
|
}
|
||||||
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)
|
||||||
|
@ -52,6 +64,7 @@ class HttpMockServer {
|
||||||
}
|
}
|
||||||
child.addMock(mockPath, mock)
|
child.addMock(mockPath, mock)
|
||||||
mockNames << name
|
mockNames << name
|
||||||
|
ex.sendResponseHeaders(200, 0)
|
||||||
ex.responseBody << '<mockAdded/>'
|
ex.responseBody << '<mockAdded/>'
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
}
|
}
|
||||||
|
@ -64,11 +77,13 @@ class HttpMockServer {
|
||||||
println "Removing $name"
|
println "Removing $name"
|
||||||
int used = childServers.inject(0) { int res, HttpServerWraper server-> server.removeMock(name) + res}
|
int used = childServers.inject(0) { int res, HttpServerWraper server-> server.removeMock(name) + res}
|
||||||
mockNames.remove(name)
|
mockNames.remove(name)
|
||||||
|
ex.sendResponseHeaders(200, 0)
|
||||||
ex.responseBody << "<mockRemoved>$used</mockRemoved>"
|
ex.responseBody << "<mockRemoved>$used</mockRemoved>"
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void createErrorResponse(HttpExchange ex, Exception e) {
|
private static void createErrorResponse(HttpExchange ex, Exception e) {
|
||||||
|
ex.sendResponseHeaders(400, 0)
|
||||||
ex.responseBody << """<exceptionOccured>${e.message}</exceptionOccured>"""
|
ex.responseBody << """<exceptionOccured>${e.message}</exceptionOccured>"""
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,21 +7,18 @@ class Mock {
|
||||||
final String name
|
final String name
|
||||||
final String path
|
final String path
|
||||||
final int port
|
final int port
|
||||||
final Closure predicate
|
Closure predicate = { xml -> true }
|
||||||
final Closure responseOk
|
Closure responseOk = { xml -> '' }
|
||||||
final boolean soap
|
boolean soap = false
|
||||||
//TODO add http method
|
int statusCode = 200
|
||||||
//TODO add http code
|
//TODO add http method - default POST
|
||||||
//TODO add request headers
|
//TODO add request headers - default [:]
|
||||||
//TODO add response headers
|
//TODO add response headers - default [:]
|
||||||
int counter = 0
|
int counter = 0
|
||||||
//TODO add historical invocations
|
//TODO add historical invocations
|
||||||
|
|
||||||
Mock(String name, String path, int port, Closure predicate, Closure responseOk, boolean soap) {
|
Mock(String name, String path, int port) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.predicate = predicate
|
|
||||||
this.responseOk = responseOk
|
|
||||||
this.soap = soap
|
|
||||||
this.path = path
|
this.path = path
|
||||||
this.port = port
|
this.port = port
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ class AddMockRequestData {
|
||||||
String predicate
|
String predicate
|
||||||
String response
|
String response
|
||||||
Boolean soap
|
Boolean soap
|
||||||
|
Integer statusCode
|
||||||
|
|
||||||
void setPredicate(String predicate){
|
void setPredicate(String predicate){
|
||||||
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
||||||
|
|
|
@ -16,46 +16,50 @@ class ControlServerClient {
|
||||||
address = "http://$host:$port/serverControl"
|
address = "http://$host:$port/serverControl"
|
||||||
}
|
}
|
||||||
|
|
||||||
void addMock(AddMockRequestData addMockRequestData){
|
void addMock(AddMockRequestData addMockRequestData) {
|
||||||
HttpPost addMockPost = new HttpPost(address)
|
HttpPost addMockPost = new HttpPost(address)
|
||||||
addMockPost.entity = buildAddMockRequest(addMockRequestData)
|
addMockPost.entity = buildAddMockRequest(addMockRequestData)
|
||||||
CloseableHttpResponse response = client.execute(addMockPost)
|
CloseableHttpResponse response = client.execute(addMockPost)
|
||||||
GPathResult responseXml = Util.extractXmlResponse(response)
|
GPathResult responseXml = Util.extractXmlResponse(response)
|
||||||
if(responseXml.name() != 'mockAdded'){
|
if (responseXml.name() != 'mockAdded') {
|
||||||
throw new MockAlreadyExists()
|
if (responseXml.text() == 'mock already registered') {
|
||||||
|
throw new MockAlreadyExists()
|
||||||
|
|
||||||
|
}
|
||||||
|
throw new InvalidMockDefinitionException(responseXml.text())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int removeMock(String name){
|
int removeMock(String name) {
|
||||||
HttpPost removeMockPost = new HttpPost(address)
|
HttpPost removeMockPost = new HttpPost(address)
|
||||||
removeMockPost.entity = buildRemoveMockRequest(new RemoveMockRequestData(name:name))
|
removeMockPost.entity = buildRemoveMockRequest(new RemoveMockRequestData(name: name))
|
||||||
CloseableHttpResponse response = client.execute(removeMockPost)
|
CloseableHttpResponse response = client.execute(removeMockPost)
|
||||||
GPathResult responseXml = Util.extractXmlResponse(response)
|
GPathResult responseXml = Util.extractXmlResponse(response)
|
||||||
if(responseXml.name() == 'mockRemoved'){
|
if (responseXml.name() == 'mockRemoved') {
|
||||||
return responseXml.text() as int
|
return responseXml.text() as int
|
||||||
}
|
}
|
||||||
throw new MockDoesNotExist()
|
throw new MockDoesNotExist()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static StringEntity buildRemoveMockRequest(RemoveMockRequestData data) {
|
||||||
private StringEntity buildRemoveMockRequest(RemoveMockRequestData data){
|
|
||||||
return new StringEntity("""\
|
return new StringEntity("""\
|
||||||
<removeMock>
|
<removeMock>
|
||||||
<name>${data.name}</name>
|
<name>${data.name}</name>
|
||||||
</removeMock>
|
</removeMock>
|
||||||
""",ContentType.create("text/xml", "UTF-8"))
|
""", ContentType.create("text/xml", "UTF-8"))
|
||||||
}
|
}
|
||||||
|
|
||||||
private StringEntity buildAddMockRequest(AddMockRequestData data){
|
private static StringEntity buildAddMockRequest(AddMockRequestData data) {
|
||||||
return new StringEntity("""\
|
return new StringEntity("""\
|
||||||
<addMock>
|
<addMock>
|
||||||
<name>${data.name}</name>
|
<name>${data.name}</name>
|
||||||
<path>${data.path}</path>
|
<path>${data.path}</path>
|
||||||
<port>${data.port}</port>
|
<port>${data.port}</port>
|
||||||
<predicate>${data.predicate}</predicate>
|
${data.predicate != null ? "<predicate>${data.predicate}</predicate>" : ''}
|
||||||
<response>${data.response}</response>
|
${data.response != null ? "<response>${data.response}</response>" : ''}
|
||||||
<soap>${data.soap}</soap>
|
${data.soap != null ? "<soap>${data.soap}</soap>" : ''}
|
||||||
|
${data.statusCode != null ? "<statusCode>${data.statusCode}</statusCode>" : ''}
|
||||||
</addMock>
|
</addMock>
|
||||||
""",ContentType.create("text/xml", "UTF-8"))
|
""", ContentType.create("text/xml", "UTF-8"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package pl.touk.mockserver.client
|
||||||
|
|
||||||
|
class InvalidMockDefinitionException extends RuntimeException{
|
||||||
|
InvalidMockDefinitionException(String s) {
|
||||||
|
super(s)
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ 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
|
||||||
import org.apache.http.impl.client.HttpClients
|
import org.apache.http.impl.client.HttpClients
|
||||||
|
import org.apache.http.util.EntityUtils
|
||||||
import pl.touk.mockserver.client.*
|
import pl.touk.mockserver.client.*
|
||||||
import spock.lang.Shared
|
import spock.lang.Shared
|
||||||
import spock.lang.Specification
|
import spock.lang.Specification
|
||||||
|
@ -73,7 +74,7 @@ class MockServerIntegrationTest extends Specification {
|
||||||
controlServerClient.removeMock('testSoap') == 1
|
controlServerClient.removeMock('testSoap') == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
def "should not remove when it does not exist"() {
|
def "should not remove mock when it does not exist"() {
|
||||||
when:
|
when:
|
||||||
controlServerClient.removeMock('testSoap')
|
controlServerClient.removeMock('testSoap')
|
||||||
then:
|
then:
|
||||||
|
@ -214,9 +215,62 @@ class MockServerIntegrationTest extends Specification {
|
||||||
9998 | '/test2' | 'another port and path'
|
9998 | '/test2' | 'another port and path'
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO def "should get mock report"(){}
|
@Unroll
|
||||||
//TODO def "should get list mocks"(){}
|
def "should dispatch rest mock with response code"() {
|
||||||
//TODO def "should dispatch rest mock with response code"(){}
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest1',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
statusCode: statusCode
|
||||||
|
))
|
||||||
|
HttpPost request = new HttpPost('http://localhost:9999/test1')
|
||||||
|
request.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8"))
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(request)
|
||||||
|
then:
|
||||||
|
response.statusLine.statusCode == expectedStatusCode
|
||||||
|
EntityUtils.consumeQuietly(response.entity)
|
||||||
|
where:
|
||||||
|
statusCode | expectedStatusCode
|
||||||
|
null | 200
|
||||||
|
300 | 300
|
||||||
|
204 | 204
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should return response code 404 and error body when mocks does not apply"() {
|
||||||
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest1',
|
||||||
|
path: '/test1',
|
||||||
|
port: 9999,
|
||||||
|
predicate: '''{xml -> xml.name() == 'request2'}''',
|
||||||
|
response: '''{xml -> "<goodResponseRest2/>"}'''
|
||||||
|
))
|
||||||
|
HttpPost request = new HttpPost('http://localhost:9999/test1')
|
||||||
|
request.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8"))
|
||||||
|
when:
|
||||||
|
CloseableHttpResponse response = client.execute(request)
|
||||||
|
then:
|
||||||
|
response.statusLine.statusCode == 404
|
||||||
|
GPathResult secondXmlResponse = Util.extractXmlResponse(response)
|
||||||
|
secondXmlResponse.name() == 'invalidInput'
|
||||||
|
}
|
||||||
|
|
||||||
|
def "should inform that there was problem during adding mock - invalid port"(){
|
||||||
|
when:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testSoap',
|
||||||
|
path: '/testEndpoint2',
|
||||||
|
port: -1,
|
||||||
|
predicate: '''{xml -> true}''',
|
||||||
|
response: '''{xml -> "<goodResponseSoap-${xml.name()}/>"}''',
|
||||||
|
soap: true
|
||||||
|
))
|
||||||
|
then:
|
||||||
|
thrown(InvalidMockDefinitionException)
|
||||||
|
}
|
||||||
|
|
||||||
//TODO def "should dispatch rest mock with post method"(){}
|
//TODO def "should dispatch rest mock with post method"(){}
|
||||||
//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"(){}
|
||||||
|
@ -230,4 +284,8 @@ class MockServerIntegrationTest extends Specification {
|
||||||
//TODO def "should dispatch rest mock with delete method and request headers"(){}
|
//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 mock with delete method and response headers"(){}
|
||||||
//TODO def "should dispatch rest mocks with all methods"(){}
|
//TODO def "should dispatch rest mocks with all methods"(){}
|
||||||
|
|
||||||
|
//TODO def "should get mock report"(){}
|
||||||
|
//TODO def "should get list mocks"(){}
|
||||||
|
//TODO def "should validate mock when creating"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue