Skip to content

Commit 3592255

Browse files
authored
Merge pull request #6538 from thc202/zest/client-errors
zest: fail fast on client errors
2 parents a288c1c + 271c585 commit 3592255

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

addOns/zest/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
8-
8+
### Changed
9+
- Fail fast on client errors.
910

1011
## [48.7.0] - 2025-06-10
1112
### Changed

addOns/zest/src/main/java/org/zaproxy/zap/extension/zest/ZestZapRunner.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,10 @@ public String handleClient(ZestScript script, ZestClient client)
401401
}
402402
}
403403
return super.handleClient(script, client);
404+
} catch (ZestClientFailException e) {
405+
throw e;
404406
} catch (Exception e) {
405-
LOGGER.error(e.getMessage(), e);
406-
return null;
407+
throw new ZestClientFailException(client, e);
407408
}
408409
}
409410

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Zed Attack Proxy (ZAP) and its related class files.
3+
*
4+
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
5+
*
6+
* Copyright 2025 The ZAP Development Team
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package org.zaproxy.zap.extension.zest;
21+
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.is;
24+
import static org.hamcrest.Matchers.sameInstance;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.BDDMockito.given;
28+
import static org.mockito.Mockito.mock;
29+
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
32+
import org.zaproxy.addon.network.ExtensionNetwork;
33+
import org.zaproxy.zap.extension.script.ExtensionScript;
34+
import org.zaproxy.zest.core.v1.ZestClient;
35+
import org.zaproxy.zest.core.v1.ZestClientFailException;
36+
import org.zaproxy.zest.core.v1.ZestScript;
37+
38+
/** Unit test for {@link ZestZapRunner}. */
39+
class ZestZapRunnerUnitTest {
40+
41+
private ExtensionZest extensionZest;
42+
private ExtensionScript extensionScript;
43+
private ExtensionNetwork extensionNetwork;
44+
private ZestScriptWrapper scriptWrapper;
45+
46+
private ZestZapRunner runner;
47+
48+
@BeforeEach
49+
void setup() {
50+
extensionZest = mock();
51+
extensionScript = mock();
52+
given(extensionZest.getExtScript()).willReturn(extensionScript);
53+
extensionNetwork = mock();
54+
given(extensionNetwork.getMainProxyServerInfo()).willReturn(mock());
55+
scriptWrapper = mock();
56+
57+
runner = new ZestZapRunner(extensionZest, extensionNetwork, scriptWrapper);
58+
}
59+
60+
@Test
61+
void shouldRethrowClientZestClientFailExceptions() throws ZestClientFailException {
62+
// Given
63+
ZestScript script = mock();
64+
ZestClient client = mock();
65+
ZestClientFailException exception = new ZestClientFailException(null);
66+
given(client.invoke(any())).willThrow(exception);
67+
// When
68+
Exception thrown =
69+
assertThrows(
70+
ZestClientFailException.class, () -> runner.handleClient(script, client));
71+
// Then
72+
assertThat(thrown, is(sameInstance(exception)));
73+
}
74+
75+
@Test
76+
void shouldThrowClientExceptions() throws ZestClientFailException {
77+
// Given
78+
ZestScript script = mock();
79+
ZestClient client = mock();
80+
RuntimeException exception = new RuntimeException();
81+
given(client.invoke(any())).willThrow(exception);
82+
// When
83+
ZestClientFailException thrown =
84+
assertThrows(
85+
ZestClientFailException.class, () -> runner.handleClient(script, client));
86+
// Then
87+
assertThat(thrown.getElement(), is(sameInstance(client)));
88+
assertThat(thrown.getCause(), is(sameInstance(exception)));
89+
}
90+
}

0 commit comments

Comments
 (0)