Add limited mock uses
This commit is contained in:
parent
fcd99cf61f
commit
79622a4177
7 changed files with 369 additions and 51 deletions
|
@ -39,6 +39,7 @@ class ContextExecutor {
|
|||
try {
|
||||
if (mock.match(Method.valueOf(ex.requestMethod), request)) {
|
||||
log.debug("Mock ${mock.name} match request ${request.text}")
|
||||
handleMaxUses(mock)
|
||||
MockResponse httpResponse = mock.apply(request)
|
||||
fillExchange(ex, httpResponse)
|
||||
log.trace("Mock ${mock.name} response with body ${httpResponse.text}")
|
||||
|
@ -92,4 +93,22 @@ class ContextExecutor {
|
|||
List<Mock> getMocks() {
|
||||
return mocks
|
||||
}
|
||||
|
||||
private synchronized void handleMaxUses(Mock mock) {
|
||||
if (mock.hasLimitedUses()) {
|
||||
mock.decrementUses()
|
||||
removeAndResetIfNeeded(mock)
|
||||
log.debug("Uses left ${mock.usesLeft} of ${mock.maxUses} (is cyclic: ${mock.cyclic})")
|
||||
}
|
||||
}
|
||||
|
||||
private void removeAndResetIfNeeded(Mock mock) {
|
||||
if (mock.shouldBeRemoved()) {
|
||||
mocks.remove(mock)
|
||||
}
|
||||
if (mock.shouldUsesBeReset()) {
|
||||
mock.resetUses()
|
||||
mocks.add(mock)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,6 +108,9 @@ class HttpMockServer {
|
|||
if (name in mockNames) {
|
||||
throw new RuntimeException('mock already registered')
|
||||
}
|
||||
if (request.maxUses == 0) {
|
||||
throw new RuntimeException('cannot set maxUses to 0')
|
||||
}
|
||||
Mock mock = mockFromRequest(request)
|
||||
HttpServerWrapper child = getOrCreateChildServer(mock.port, mock.https)
|
||||
child.addMock(mock)
|
||||
|
@ -121,6 +124,9 @@ class HttpMockServer {
|
|||
if (name in mockNames) {
|
||||
throw new RuntimeException('mock already registered')
|
||||
}
|
||||
if (co.maxUses == 0) {
|
||||
throw new RuntimeException('cannot set maxUses to 0')
|
||||
}
|
||||
Mock mock = mockFromConfig(co)
|
||||
HttpServerWrapper child = getOrCreateChildServer(mock.port, mock.https)
|
||||
child.addMock(mock)
|
||||
|
@ -158,6 +164,8 @@ class HttpMockServer {
|
|||
mock.schema = request.schema
|
||||
mock.preserveHistory = request.preserveHistory != false
|
||||
mock.https = request.https
|
||||
mock.maxUses = request.maxUses
|
||||
mock.cyclic = request.cyclic
|
||||
return mock
|
||||
}
|
||||
|
||||
|
@ -182,6 +190,8 @@ class HttpMockServer {
|
|||
requireClientAuth: co.https?.requireClientAuth?.asBoolean() ?: false
|
||||
)
|
||||
}
|
||||
mock.maxUses = co.maxUses ?: null
|
||||
mock.cyclic = co.cyclic ?: null
|
||||
return mock
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,9 @@ class Mock implements Comparable<Mock> {
|
|||
Map<String, String> imports = [:]
|
||||
boolean preserveHistory = true
|
||||
Https https
|
||||
int maxUses = -1
|
||||
int usesLeft
|
||||
boolean cyclic
|
||||
|
||||
Mock(String name, String path, int port) {
|
||||
if (!(name)) {
|
||||
|
@ -148,6 +151,17 @@ class Mock implements Comparable<Mock> {
|
|||
}
|
||||
}
|
||||
|
||||
void setMaxUses(Integer maxUses) {
|
||||
if (maxUses > 0) {
|
||||
this.maxUses = maxUses
|
||||
this.usesLeft = maxUses
|
||||
}
|
||||
}
|
||||
|
||||
void setCyclic(Boolean cyclic) {
|
||||
this.cyclic = cyclic ?: false
|
||||
}
|
||||
|
||||
@Override
|
||||
int compareTo(Mock o) {
|
||||
return name.compareTo(o.name)
|
||||
|
@ -165,4 +179,24 @@ class Mock implements Comparable<Mock> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean hasLimitedUses() {
|
||||
return maxUses > 0
|
||||
}
|
||||
|
||||
void decrementUses() {
|
||||
usesLeft--
|
||||
}
|
||||
|
||||
boolean shouldBeRemoved() {
|
||||
return hasLimitedUses() && usesLeft <= 0
|
||||
}
|
||||
|
||||
boolean shouldUsesBeReset() {
|
||||
return shouldBeRemoved() && cyclic
|
||||
}
|
||||
|
||||
void resetUses() {
|
||||
setMaxUses(maxUses)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue