|
| 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