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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,21 +74,22 @@ class HttpMockServer {
|
||||||
|
|
||||||
void listMocks(HttpExchange ex) {
|
void listMocks(HttpExchange ex) {
|
||||||
Mocks mockListing = new Mocks(
|
Mocks mockListing = new Mocks(
|
||||||
mocks: listMocks().collect {
|
mocks: listMocks().collect {
|
||||||
new MockReport(
|
new MockReport(
|
||||||
name: it.name,
|
name: it.name,
|
||||||
path: it.path,
|
path: it.path,
|
||||||
port: it.port,
|
port: it.port,
|
||||||
predicate: it.predicateClosureText,
|
predicate: it.predicateClosureText,
|
||||||
response: it.responseClosureText,
|
response: it.responseClosureText,
|
||||||
responseHeaders: it.responseHeadersClosureText,
|
responseHeaders: it.responseHeadersClosureText,
|
||||||
soap: it.soap,
|
soap: it.soap,
|
||||||
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
|
||||||
}
|
)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
createResponse(ex, mockListing, 200)
|
createResponse(ex, mockListing, 200)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,7 +191,7 @@ class HttpMockServer {
|
||||||
mockNames.remove(name)
|
mockNames.remove(name)
|
||||||
configuration.remove(name)
|
configuration.remove(name)
|
||||||
MockRemoved mockRemoved = new MockRemoved(
|
MockRemoved mockRemoved = new MockRemoved(
|
||||||
mockEvents: createMockEventReports(mockEvents)
|
mockEvents: createMockEventReports(mockEvents)
|
||||||
)
|
)
|
||||||
createResponse(ex, mockRemoved, 200)
|
createResponse(ex, mockRemoved, 200)
|
||||||
}
|
}
|
||||||
|
@ -196,23 +199,23 @@ class HttpMockServer {
|
||||||
private static List<MockEventReport> createMockEventReports(List<MockEvent> mockEvents) {
|
private static List<MockEventReport> createMockEventReports(List<MockEvent> mockEvents) {
|
||||||
return mockEvents.collect {
|
return mockEvents.collect {
|
||||||
new MockEventReport(
|
new MockEventReport(
|
||||||
request: new MockRequestReport(
|
request: new MockRequestReport(
|
||||||
text: it.request.text,
|
text: it.request.text,
|
||||||
headers: new MockRequestReport.Headers(headers: it.request.headers.collect {
|
headers: new MockRequestReport.Headers(headers: it.request.headers.collect {
|
||||||
new Parameter(name: it.key, value: it.value)
|
new Parameter(name: it.key, value: it.value)
|
||||||
}),
|
}),
|
||||||
queryParams: new MockRequestReport.QueryParams(queryParams: it.request.query.collect {
|
queryParams: new MockRequestReport.QueryParams(queryParams: it.request.query.collect {
|
||||||
new Parameter(name: it.key, value: it.value)
|
new Parameter(name: it.key, value: it.value)
|
||||||
}),
|
}),
|
||||||
path: new MockRequestReport.Path(pathParts: it.request.path)
|
path: new MockRequestReport.Path(pathParts: it.request.path)
|
||||||
),
|
),
|
||||||
response: new MockResponseReport(
|
response: new MockResponseReport(
|
||||||
statusCode: it.response.statusCode,
|
statusCode: it.response.statusCode,
|
||||||
text: it.response.text,
|
text: it.response.text,
|
||||||
headers: new MockResponseReport.Headers(headers: it.response.headers.collect {
|
headers: new MockResponseReport.Headers(headers: it.response.headers.collect {
|
||||||
new Parameter(name: it.key, value: it.value)
|
new Parameter(name: it.key, value: it.value)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -225,7 +228,7 @@ class HttpMockServer {
|
||||||
log.trace("Peeking mock $name")
|
log.trace("Peeking mock $name")
|
||||||
List<MockEvent> mockEvents = childServers.values().collect { it.peekMock(name) }.flatten() as List<MockEvent>
|
List<MockEvent> mockEvents = childServers.values().collect { it.peekMock(name) }.flatten() as List<MockEvent>
|
||||||
MockPeeked mockPeeked = new MockPeeked(
|
MockPeeked mockPeeked = new MockPeeked(
|
||||||
mockEvents: createMockEventReports(mockEvents)
|
mockEvents: createMockEventReports(mockEvents)
|
||||||
)
|
)
|
||||||
createResponse(ex, mockPeeked, 200)
|
createResponse(ex, mockPeeked, 200)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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, [:])
|
||||||
history << new MockEvent(request, response)
|
if(preserveHistory) {
|
||||||
|
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)
|
||||||
history << new MockEvent(request, mockResponse)
|
if(preserveHistory) {
|
||||||
|
history << new MockEvent(request, mockResponse)
|
||||||
|
}
|
||||||
return mockResponse
|
return mockResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue