|
22 | 22 | import static org.hamcrest.CoreMatchers.equalTo; |
23 | 23 | import static org.hamcrest.CoreMatchers.is; |
24 | 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | +import static org.hamcrest.Matchers.containsString; |
25 | 26 | import static org.hamcrest.Matchers.hasSize; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
26 | 28 | import static org.mockito.ArgumentMatchers.anyInt; |
27 | 29 | import static org.mockito.BDDMockito.given; |
28 | 30 | import static org.mockito.Mockito.doNothing; |
29 | 31 | import static org.mockito.Mockito.mock; |
| 32 | +import static org.mockito.Mockito.verify; |
30 | 33 | import static org.mockito.Mockito.when; |
31 | 34 | import static org.mockito.Mockito.withSettings; |
32 | 35 |
|
|
35 | 38 | import java.util.HashMap; |
36 | 39 | import java.util.List; |
37 | 40 | import java.util.Map; |
| 41 | +import net.sf.json.JSONObject; |
38 | 42 | import org.apache.commons.httpclient.Cookie; |
39 | 43 | import org.junit.jupiter.api.BeforeEach; |
40 | 44 | import org.junit.jupiter.api.Test; |
|
55 | 59 | import org.zaproxy.zap.model.Context; |
56 | 60 | import org.zaproxy.zap.network.HttpRequestBody; |
57 | 61 | import org.zaproxy.zap.network.HttpResponseBody; |
| 62 | +import org.zaproxy.zap.session.SessionManagementMethod; |
58 | 63 | import org.zaproxy.zap.testutils.TestUtils; |
59 | 64 | import org.zaproxy.zap.utils.Pair; |
60 | 65 | import org.zaproxy.zap.utils.ZapXmlConfiguration; |
@@ -389,4 +394,84 @@ void shouldParseHeaderValuesProperly(String entry, String expectedFirst, String |
389 | 394 | assertThat(header.first, is(equalTo(expectedFirst))); |
390 | 395 | assertThat(header.second, is(equalTo(expectedSecond))); |
391 | 396 | } |
| 397 | + |
| 398 | + @ParameterizedTest |
| 399 | + @CsvSource(value = {"'',0", "Header1:Value1,1", "Header:, 1"}) |
| 400 | + void shouldSetHeaderConfigsFromApi(String headers, int expectedSize) throws ApiException { |
| 401 | + // Given |
| 402 | + HeaderBasedSessionManagementMethodType type = new HeaderBasedSessionManagementMethodType(); |
| 403 | + Context context = mock(Context.class); |
| 404 | + JSONObject params = new JSONObject(); |
| 405 | + params.put("contextId", 1); |
| 406 | + params.put("headers", headers); |
| 407 | + |
| 408 | + Model model = mock(Model.class, withSettings().strictness(Strictness.LENIENT)); |
| 409 | + Model.setSingletonForTesting(model); |
| 410 | + Session session = mock(Session.class); |
| 411 | + given(model.getSession()).willReturn(session); |
| 412 | + given(session.getContext(1)).willReturn(context); |
| 413 | + |
| 414 | + // When |
| 415 | + type.getSetMethodForContextApiAction().handleAction(params); |
| 416 | + |
| 417 | + // Then |
| 418 | + ArgumentCaptor<SessionManagementMethod> captor = |
| 419 | + ArgumentCaptor.forClass(SessionManagementMethod.class); |
| 420 | + verify(context).setSessionManagementMethod(captor.capture()); |
| 421 | + HeaderBasedSessionManagementMethod savedMethod = |
| 422 | + (HeaderBasedSessionManagementMethod) captor.getValue(); |
| 423 | + assertThat(savedMethod.getHeaderConfigs(), hasSize(expectedSize)); |
| 424 | + } |
| 425 | + |
| 426 | + @ParameterizedTest |
| 427 | + @CsvSource(value = {"' \\t\\n '", "Header"}) |
| 428 | + void shouldRejectInvalidHeaderConfigsFromApi(String headers) { |
| 429 | + // Given |
| 430 | + HeaderBasedSessionManagementMethodType type = new HeaderBasedSessionManagementMethodType(); |
| 431 | + Context context = mock(Context.class); |
| 432 | + JSONObject params = new JSONObject(); |
| 433 | + params.put("contextId", 1); |
| 434 | + params.put("headers", headers); |
| 435 | + |
| 436 | + Model model = mock(Model.class, withSettings().strictness(Strictness.LENIENT)); |
| 437 | + Model.setSingletonForTesting(model); |
| 438 | + Session session = mock(Session.class); |
| 439 | + given(model.getSession()).willReturn(session); |
| 440 | + given(session.getContext(1)).willReturn(context); |
| 441 | + |
| 442 | + // When / Then |
| 443 | + ApiException e = |
| 444 | + assertThrows( |
| 445 | + ApiException.class, |
| 446 | + () -> type.getSetMethodForContextApiAction().handleAction(params)); |
| 447 | + |
| 448 | + assertThat(e.getType(), is(equalTo(ApiException.Type.ILLEGAL_PARAMETER))); |
| 449 | + assertThat(e.getMessage(), is(containsString("headers"))); |
| 450 | + } |
| 451 | + |
| 452 | + @Test |
| 453 | + void shouldSetHeaderConfigsFromApiWhenParamMissing() throws ApiException { |
| 454 | + // Given |
| 455 | + HeaderBasedSessionManagementMethodType type = new HeaderBasedSessionManagementMethodType(); |
| 456 | + Context context = mock(Context.class); |
| 457 | + JSONObject params = new JSONObject(); |
| 458 | + params.put("contextId", 1); |
| 459 | + |
| 460 | + Model model = mock(Model.class, withSettings().strictness(Strictness.LENIENT)); |
| 461 | + Model.setSingletonForTesting(model); |
| 462 | + Session session = mock(Session.class); |
| 463 | + given(model.getSession()).willReturn(session); |
| 464 | + given(session.getContext(1)).willReturn(context); |
| 465 | + |
| 466 | + // When |
| 467 | + type.getSetMethodForContextApiAction().handleAction(params); |
| 468 | + |
| 469 | + // Then |
| 470 | + ArgumentCaptor<SessionManagementMethod> captor = |
| 471 | + ArgumentCaptor.forClass(SessionManagementMethod.class); |
| 472 | + verify(context).setSessionManagementMethod(captor.capture()); |
| 473 | + HeaderBasedSessionManagementMethod savedMethod = |
| 474 | + (HeaderBasedSessionManagementMethod) captor.getValue(); |
| 475 | + assertThat(savedMethod.getHeaderConfigs(), hasSize(0)); |
| 476 | + } |
392 | 477 | } |
0 commit comments