Skip to content

Commit d86550f

Browse files
committed
Polish Tests and Error Messages
MockMvc matchers are best matched with the MockMvc execution API - it's a little odd to try and use them inside of an AssertJ assertion since they do their own asserting. It's more readable to place "this." in front of member variables. It's best to test just one class at a time in a unit test. Issue: spring-projectsgh-4187
1 parent 82d527e commit d86550f

File tree

4 files changed

+26
-71
lines changed

4 files changed

+26
-71
lines changed

web/src/main/java/org/springframework/security/web/authentication/logout/HeaderWriterLogoutHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class HeaderWriterLogoutHandler implements LogoutHandler {
3838
* @throws {@link IllegalArgumentException} if headerWriter is null.
3939
*/
4040
public HeaderWriterLogoutHandler(HeaderWriter headerWriter) {
41-
Assert.notNull(headerWriter, "headerWriter cannot be null.");
41+
Assert.notNull(headerWriter, "headerWriter cannot be null");
4242
this.headerWriter = headerWriter;
4343
}
4444

web/src/main/java/org/springframework/security/web/header/writers/ClearSiteDataHeaderWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final class ClearSiteDataHeaderWriter implements HeaderWriter {
6767
* @throws {@link IllegalArgumentException} if sources is null or empty.
6868
*/
6969
public ClearSiteDataHeaderWriter(String ...sources) {
70-
Assert.notEmpty(sources, "Sources cannot be empty or null.");
70+
Assert.notEmpty(sources, "sources cannot be empty or null");
7171
this.requestMatcher = new SecureRequestMatcher();
7272
this.headerValue = Stream.of(sources).map(this::quote).collect(Collectors.joining(", "));
7373
}

web/src/test/java/org/springframework/security/web/authentication/logout/HeaderWriterLogoutHandlerTests.java

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@
2020
import org.junit.Rule;
2121
import org.junit.Test;
2222
import org.junit.rules.ExpectedException;
23+
2324
import org.springframework.mock.web.MockHttpServletRequest;
2425
import org.springframework.mock.web.MockHttpServletResponse;
2526
import org.springframework.security.core.Authentication;
26-
import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter;
27+
import org.springframework.security.web.header.HeaderWriter;
2728

28-
import static org.assertj.core.api.Assertions.assertThat;
2929
import static org.mockito.Mockito.mock;
30-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
30+
import static org.mockito.Mockito.verify;
3131

3232
/**
3333
*
3434
* @author Rafiullah Hamedy
35+
* @author Josh Cummings
3536
*
3637
* @see {@link HeaderWriterLogoutHandler}
3738
*/
3839
public class HeaderWriterLogoutHandlerTests {
39-
private static final String HEADER_NAME = "Clear-Site-Data";
40-
4140
private MockHttpServletResponse response;
4241
private MockHttpServletRequest request;
4342

@@ -51,54 +50,19 @@ public void setup() {
5150
}
5251

5352
@Test
54-
public void createInstanceWhenHeaderWriterIsNullThenThrowsException() {
53+
public void constructorWhenHeaderWriterIsNullThenThrowsException() {
5554
this.thrown.expect(IllegalArgumentException.class);
56-
this.thrown.expectMessage("headerWriter cannot be null.");
55+
this.thrown.expectMessage("headerWriter cannot be null");
5756

5857
new HeaderWriterLogoutHandler(null);
5958
}
6059

6160
@Test
62-
public void createInstanceWhenSourceIsNullThenThrowsException() {
63-
this.thrown.expect(IllegalArgumentException.class);
64-
this.thrown.expectMessage("Sources cannot be empty or null.");
65-
66-
new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter());
67-
}
68-
69-
@Test
70-
public void logoutWhenRequestIsNotSecureThenHeaderIsNotPresent() {
71-
HeaderWriterLogoutHandler handler = new HeaderWriterLogoutHandler(
72-
new ClearSiteDataHeaderWriter("cache"));
73-
74-
handler.logout(request, response, mock(Authentication.class));
75-
76-
assertThat(header().doesNotExist(HEADER_NAME));
77-
}
78-
79-
@Test
80-
public void logoutWhenRequestIsSecureThenHeaderIsPresentMatchesWildCardSource() {
81-
HeaderWriterLogoutHandler handler = new HeaderWriterLogoutHandler(
82-
new ClearSiteDataHeaderWriter("*"));
83-
84-
this.request.setSecure(true);
85-
86-
handler.logout(request, response, mock(Authentication.class));
87-
88-
assertThat(header().stringValues(HEADER_NAME, "\"*\""));
89-
}
90-
91-
@Test
92-
public void logoutWhenRequestIsSecureThenHeaderValueMatchesSource() {
93-
HeaderWriterLogoutHandler handler = new HeaderWriterLogoutHandler(
94-
new ClearSiteDataHeaderWriter("cache", "cookies", "storage",
95-
"executionContexts"));
96-
97-
this.request.setSecure(true);
98-
99-
handler.logout(request, response, mock(Authentication.class));
61+
public void logoutWhenHasHeaderWriterThenInvoked() {
62+
HeaderWriter headerWriter = mock(HeaderWriter.class);
63+
HeaderWriterLogoutHandler handler = new HeaderWriterLogoutHandler(headerWriter);
64+
handler.logout(this.request, this.response, mock(Authentication.class));
10065

101-
assertThat(header().stringValues(HEADER_NAME, "\"cache\", \"cookies\", \"storage\", "
102-
+ "\"executionContexts\""));
66+
verify(headerWriter).writeHeaders(this.request, this.response);
10367
}
10468
}

web/src/test/java/org/springframework/security/web/header/writers/ClearSiteDataHeaderWriterTests.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
import org.junit.Rule;
2121
import org.junit.Test;
2222
import org.junit.rules.ExpectedException;
23+
2324
import org.springframework.mock.web.MockHttpServletRequest;
2425
import org.springframework.mock.web.MockHttpServletResponse;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
27-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
2828

2929
/**
3030
*
3131
* @author Rafiullah Hamedy
32+
* @author Josh Cummings
3233
*
3334
* @see {@link ClearSiteDataHeaderWriter}
3435
*/
@@ -43,53 +44,43 @@ public class ClearSiteDataHeaderWriterTests {
4344

4445
@Before
4546
public void setup() {
46-
request = new MockHttpServletRequest();
47-
request.setSecure(true);
48-
response = new MockHttpServletResponse();
47+
this.request = new MockHttpServletRequest();
48+
this.request.setSecure(true);
49+
this.response = new MockHttpServletResponse();
4950
}
5051

5152
@Test
5253
public void createInstanceWhenMissingSourceThenThrowsException() {
5354
this.thrown.expect(Exception.class);
54-
this.thrown.expectMessage("Sources cannot be empty or null.");
55+
this.thrown.expectMessage("sources cannot be empty or null");
5556

5657
new ClearSiteDataHeaderWriter();
5758
}
5859

59-
@Test
60-
public void createInstanceWhenEmptySourceThenThrowsException() {
61-
this.thrown.expect(Exception.class);
62-
this.thrown.expectMessage("Sources cannot be empty or null.");
63-
64-
new ClearSiteDataHeaderWriter(new String[] {});
65-
}
66-
6760
@Test
6861
public void writeHeaderWhenRequestNotSecureThenHeaderIsNotPresent() {
6962
this.request.setSecure(false);
70-
7163
ClearSiteDataHeaderWriter headerWriter = new ClearSiteDataHeaderWriter("cache");
72-
headerWriter.writeHeaders(request, response);
64+
headerWriter.writeHeaders(this.request, this.response);
7365

74-
assertThat(header().doesNotExist(HEADER_NAME));
66+
assertThat(this.response.getHeader(HEADER_NAME)).isNull();
7567
}
7668

7769
@Test
7870
public void writeHeaderWhenRequestIsSecureThenHeaderValueMatchesPassedSource() {
7971
ClearSiteDataHeaderWriter headerWriter = new ClearSiteDataHeaderWriter("storage");
80-
headerWriter.writeHeaders(request, response);
72+
headerWriter.writeHeaders(this.request, this.response);
8173

82-
assertThat(header().stringValues(HEADER_NAME, "\"storage\""));
74+
assertThat(this.response.getHeader(HEADER_NAME)).isEqualTo("\"storage\"");
8375
}
8476

8577
@Test
8678
public void writeHeaderWhenRequestIsSecureThenHeaderValueMatchesPassedSources() {
8779
ClearSiteDataHeaderWriter headerWriter =
8880
new ClearSiteDataHeaderWriter("cache", "cookies", "storage", "executionContexts");
81+
headerWriter.writeHeaders(this.request, this.response);
8982

90-
headerWriter.writeHeaders(request, response);
91-
92-
assertThat(header().stringValues(HEADER_NAME, "\"cache\", \"cookies\", \"storage\","
93-
+ " \"executionContexts\""));
83+
assertThat(this.response.getHeader(HEADER_NAME))
84+
.isEqualTo("\"cache\", \"cookies\", \"storage\", \"executionContexts\"");
9485
}
9586
}

0 commit comments

Comments
 (0)