Add preserveHistory option for mock

This commit is contained in:
Dominik Przybysz 2015-12-28 14:11:02 +01:00
parent 7a13cb2ce5
commit 3599300191
5 changed files with 71 additions and 38 deletions

View file

@ -23,6 +23,7 @@
<xs:element name="responseHeaders" type="xs:string" minOccurs="0"/>
<xs:element name="schema" type="xs:string" minOccurs="0"/>
<xs:element name="imports" type="common:importAlias" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="preserveHistory" type="xs:boolean" minOccurs="0"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>

View file

@ -110,6 +110,7 @@
<xs:element name="statusCode" type="xs:int"/>
<xs:element name="schema" type="xs:string" minOccurs="0"/>
<xs:element name="imports" type="common:importAlias" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="preserveHistory" type="xs:boolean" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

View file

@ -1071,7 +1071,8 @@ class MockServerIntegrationTest extends Specification {
imports: [
new ImportAlias(alias: 'aaa', fullClassName: 'bbb'),
new ImportAlias(alias: 'ccc', fullClassName: 'bla')
]
],
preserveHistory: true
))
remoteMockServer.removeMock('testRest5')
when:
@ -1082,7 +1083,7 @@ class MockServerIntegrationTest extends Specification {
then:
List<MockReport> mockReport = remoteMockServer.listMocks()
mockReport.size() == 5
assertMockReport(mockReport[0], [name: 'testRest', path: 'testEndpoint', port: 9999, predicate: '{ _ -> true }', response: '''{ _ -> '' }''', responseHeaders: '{ _ -> [:] }', soap: false, statusCode: 200, method: Method.POST, schema: 'schema2.xsd'])
assertMockReport(mockReport[0], [name: 'testRest', path: 'testEndpoint', port: 9999, predicate: '{ _ -> true }', response: '''{ _ -> '' }''', responseHeaders: '{ _ -> [:] }', soap: false, statusCode: 200, method: Method.POST, schema: 'schema2.xsd', preserveHistory: true])
assertMockReport(mockReport[1], [name: 'testRest2', path: 'testEndpoint', port: 9998, predicate: '''{ req -> req.xml.name() == 'request1'}''', response: '''{ req -> '<response/>' }''', responseHeaders: '{ _ -> [a: "b"] }', soap: false, statusCode: 200, method: Method.POST])
assertMockReport(mockReport[2], [name: 'testRest3', path: 'testEndpoint2', port: 9999, predicate: '{ _ -> true }', response: '''{ _ -> '' }''', responseHeaders: '{ _ -> [:] }', soap: false, statusCode: 200, method: Method.POST])
assertMockReport(mockReport[3], [name: 'testRest4', path: 'testEndpoint', port: 9999, predicate: '{ _ -> true }', response: '''{ _ -> '' }''', responseHeaders: '{ _ -> [:] }', soap: true, statusCode: 204, method: Method.PUT])
@ -1091,4 +1092,26 @@ class MockServerIntegrationTest extends Specification {
mockReport[0].imports.find { it.alias == 'ccc' }?.fullClassName == 'bla'
}
def "should add mock without history"() {
expect:
remoteMockServer.addMock(new AddMock(
name: 'testRest',
path: 'testEndpoint',
port: 9999,
predicate: '''{req -> req.xml.name() == 'request'}''',
response: '''{req -> "<goodResponseRest-${req.xml.name()}/>"}''',
soap: false,
preserveHistory: false
))
when:
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
restPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8"))
CloseableHttpResponse response = client.execute(restPost)
then:
GPathResult restPostResponse = Util.extractXmlResponse(response)
restPostResponse.name() == 'goodResponseRest-request'
expect:
remoteMockServer.removeMock('testRest')?.size() == 0
}
}

View file

@ -86,7 +86,8 @@ class HttpMockServer {
method: it.method,
statusCode: it.statusCode as int,
schema: it.schema,
imports: it.imports.collect { new ImportAlias(alias: it.key, fullClassName: it.value) }
imports: it.imports.collect { new ImportAlias(alias: it.key, fullClassName: it.value) },
preserveHistory: it.preserveHistory
)
}
)
@ -150,6 +151,7 @@ class HttpMockServer {
mock.method = request.method
mock.responseHeaders = request.responseHeaders
mock.schema = request.schema
mock.preserveHistory = request.preserveHistory != false
return mock
}
@ -163,6 +165,7 @@ class HttpMockServer {
mock.method = co.method ? Method.valueOf(co.method) : null
mock.responseHeaders = co.responseHeaders ?: null
mock.schema = co.schema ?: null
mock.preserveHistory = co.preserveHistory != false
return mock
}

View file

@ -34,6 +34,7 @@ class Mock implements Comparable<Mock> {
String schema
private Validator validator
Map<String, String> imports = [:]
boolean preserveHistory = true
Mock(String name, String path, int port) {
if (!(name)) {
@ -60,7 +61,9 @@ class Mock implements Comparable<Mock> {
}
} catch (Exception e) {
MockResponse response = new MockResponse(400, e.message, [:])
if(preserveHistory) {
history << new MockEvent(request, response)
}
return response
}
}
@ -69,7 +72,9 @@ class Mock implements Comparable<Mock> {
String response = soap ? wrapSoap(responseText) : responseText
Map<String, String> headers = responseHeaders(request)
MockResponse mockResponse = new MockResponse(statusCode, response, headers)
if(preserveHistory) {
history << new MockEvent(request, mockResponse)
}
return mockResponse
}