Add preserveHistory option for mock
This commit is contained in:
parent
7a13cb2ce5
commit
3599300191
5 changed files with 71 additions and 38 deletions
|
@ -23,6 +23,7 @@
|
||||||
<xs:element name="responseHeaders" type="xs:string" minOccurs="0"/>
|
<xs:element name="responseHeaders" type="xs:string" minOccurs="0"/>
|
||||||
<xs:element name="schema" 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="imports" type="common:importAlias" minOccurs="0" maxOccurs="unbounded"/>
|
||||||
|
<xs:element name="preserveHistory" type="xs:boolean" minOccurs="0"/>
|
||||||
</xs:sequence>
|
</xs:sequence>
|
||||||
</xs:extension>
|
</xs:extension>
|
||||||
</xs:complexContent>
|
</xs:complexContent>
|
||||||
|
|
|
@ -110,6 +110,7 @@
|
||||||
<xs:element name="statusCode" type="xs:int"/>
|
<xs:element name="statusCode" type="xs:int"/>
|
||||||
<xs:element name="schema" 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="imports" type="common:importAlias" minOccurs="0" maxOccurs="unbounded"/>
|
||||||
|
<xs:element name="preserveHistory" type="xs:boolean" minOccurs="0"/>
|
||||||
</xs:sequence>
|
</xs:sequence>
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
|
|
||||||
|
|
|
@ -1071,7 +1071,8 @@ class MockServerIntegrationTest extends Specification {
|
||||||
imports: [
|
imports: [
|
||||||
new ImportAlias(alias: 'aaa', fullClassName: 'bbb'),
|
new ImportAlias(alias: 'aaa', fullClassName: 'bbb'),
|
||||||
new ImportAlias(alias: 'ccc', fullClassName: 'bla')
|
new ImportAlias(alias: 'ccc', fullClassName: 'bla')
|
||||||
]
|
],
|
||||||
|
preserveHistory: true
|
||||||
))
|
))
|
||||||
remoteMockServer.removeMock('testRest5')
|
remoteMockServer.removeMock('testRest5')
|
||||||
when:
|
when:
|
||||||
|
@ -1082,7 +1083,7 @@ class MockServerIntegrationTest extends Specification {
|
||||||
then:
|
then:
|
||||||
List<MockReport> mockReport = remoteMockServer.listMocks()
|
List<MockReport> mockReport = remoteMockServer.listMocks()
|
||||||
mockReport.size() == 5
|
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[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[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])
|
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'
|
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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,7 +86,8 @@ class HttpMockServer {
|
||||||
method: it.method,
|
method: it.method,
|
||||||
statusCode: it.statusCode as int,
|
statusCode: it.statusCode as int,
|
||||||
schema: it.schema,
|
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.method = request.method
|
||||||
mock.responseHeaders = request.responseHeaders
|
mock.responseHeaders = request.responseHeaders
|
||||||
mock.schema = request.schema
|
mock.schema = request.schema
|
||||||
|
mock.preserveHistory = request.preserveHistory != false
|
||||||
return mock
|
return mock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,6 +165,7 @@ class HttpMockServer {
|
||||||
mock.method = co.method ? Method.valueOf(co.method) : null
|
mock.method = co.method ? Method.valueOf(co.method) : null
|
||||||
mock.responseHeaders = co.responseHeaders ?: null
|
mock.responseHeaders = co.responseHeaders ?: null
|
||||||
mock.schema = co.schema ?: null
|
mock.schema = co.schema ?: null
|
||||||
|
mock.preserveHistory = co.preserveHistory != false
|
||||||
return mock
|
return mock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ class Mock implements Comparable<Mock> {
|
||||||
String schema
|
String schema
|
||||||
private Validator validator
|
private Validator validator
|
||||||
Map<String, String> imports = [:]
|
Map<String, String> imports = [:]
|
||||||
|
boolean preserveHistory = true
|
||||||
|
|
||||||
Mock(String name, String path, int port) {
|
Mock(String name, String path, int port) {
|
||||||
if (!(name)) {
|
if (!(name)) {
|
||||||
|
@ -60,7 +61,9 @@ class Mock implements Comparable<Mock> {
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
MockResponse response = new MockResponse(400, e.message, [:])
|
MockResponse response = new MockResponse(400, e.message, [:])
|
||||||
|
if(preserveHistory) {
|
||||||
history << new MockEvent(request, response)
|
history << new MockEvent(request, response)
|
||||||
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,7 +72,9 @@ class Mock implements Comparable<Mock> {
|
||||||
String response = soap ? wrapSoap(responseText) : responseText
|
String response = soap ? wrapSoap(responseText) : responseText
|
||||||
Map<String, String> headers = responseHeaders(request)
|
Map<String, String> headers = responseHeaders(request)
|
||||||
MockResponse mockResponse = new MockResponse(statusCode, response, headers)
|
MockResponse mockResponse = new MockResponse(statusCode, response, headers)
|
||||||
|
if(preserveHistory) {
|
||||||
history << new MockEvent(request, mockResponse)
|
history << new MockEvent(request, mockResponse)
|
||||||
|
}
|
||||||
return mockResponse
|
return mockResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue