|
28 | 28 | import org.springframework.mock.web.MockHttpServletResponse; |
29 | 29 | import org.springframework.security.web.header.HeaderWriter; |
30 | 30 |
|
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
31 | 33 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
32 | 34 | import static org.assertj.core.api.Assertions.assertThatRuntimeException; |
33 | 35 | import static org.mockito.BDDMockito.then; |
@@ -68,6 +70,56 @@ void writeHeadersWhenSomeWriterThrowsThenOthersStillWrite() { |
68 | 70 | then(writer2).should().writeHeaders(request, response); |
69 | 71 | } |
70 | 72 |
|
| 73 | + @Test |
| 74 | + void writeHeadersWhenOneWriterThrowsThenThatExceptionIsPropagated() { |
| 75 | + HttpServletRequest request = new MockHttpServletRequest(); |
| 76 | + HttpServletResponse response = new MockHttpServletResponse(); |
| 77 | + HeaderWriter writer1 = mock(HeaderWriter.class); |
| 78 | + HeaderWriter writer2 = mock(HeaderWriter.class); |
| 79 | + CompositeHeaderWriter headerWriter = new CompositeHeaderWriter(List.of(writer1, writer2)); |
| 80 | + IllegalStateException failure = new IllegalStateException("failed"); |
| 81 | + willThrow(failure).given(writer1).writeHeaders(request, response); |
| 82 | + assertThatExceptionOfType(IllegalStateException.class) |
| 83 | + .isThrownBy(() -> headerWriter.writeHeaders(request, response)) |
| 84 | + .isSameAs(failure); |
| 85 | + assertThat(failure.getSuppressed()).isEmpty(); |
| 86 | + then(writer2).should().writeHeaders(request, response); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void writeHeadersWhenSeveralWritersThrowThenFirstIsPropagatedWithOthersSuppressed() { |
| 91 | + HttpServletRequest request = new MockHttpServletRequest(); |
| 92 | + HttpServletResponse response = new MockHttpServletResponse(); |
| 93 | + HeaderWriter writer1 = mock(HeaderWriter.class); |
| 94 | + HeaderWriter writer2 = mock(HeaderWriter.class); |
| 95 | + HeaderWriter writer3 = mock(HeaderWriter.class); |
| 96 | + CompositeHeaderWriter headerWriter = new CompositeHeaderWriter(List.of(writer1, writer2, writer3)); |
| 97 | + IllegalStateException first = new IllegalStateException("first"); |
| 98 | + IllegalArgumentException second = new IllegalArgumentException("second"); |
| 99 | + willThrow(first).given(writer1).writeHeaders(request, response); |
| 100 | + willThrow(second).given(writer2).writeHeaders(request, response); |
| 101 | + assertThatExceptionOfType(IllegalStateException.class) |
| 102 | + .isThrownBy(() -> headerWriter.writeHeaders(request, response)) |
| 103 | + .isSameAs(first); |
| 104 | + assertThat(first.getSuppressed()).containsExactly(second); |
| 105 | + then(writer3).should().writeHeaders(request, response); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void writeHeadersWhenSeveralWritersThrowSameExceptionThenItIsPropagated() { |
| 110 | + HttpServletRequest request = new MockHttpServletRequest(); |
| 111 | + HttpServletResponse response = new MockHttpServletResponse(); |
| 112 | + HeaderWriter writer1 = mock(HeaderWriter.class); |
| 113 | + HeaderWriter writer2 = mock(HeaderWriter.class); |
| 114 | + CompositeHeaderWriter headerWriter = new CompositeHeaderWriter(List.of(writer1, writer2)); |
| 115 | + IllegalStateException failure = new IllegalStateException("failed"); |
| 116 | + willThrow(failure).given(writer1).writeHeaders(request, response); |
| 117 | + willThrow(failure).given(writer2).writeHeaders(request, response); |
| 118 | + assertThatExceptionOfType(IllegalStateException.class) |
| 119 | + .isThrownBy(() -> headerWriter.writeHeaders(request, response)) |
| 120 | + .isSameAs(failure); |
| 121 | + } |
| 122 | + |
71 | 123 | @Test |
72 | 124 | public void constructorWhenPassingEmptyListThenThrowsException() { |
73 | 125 | assertThatIllegalArgumentException().isThrownBy(() -> new CompositeHeaderWriter(Collections.emptyList())); |
|
0 commit comments