1+ package com .baeldung .spring .session .tomcatex ;
2+
3+ import org .apache .tomcat .util .codec .binary .Base64 ;
4+ import org .junit .Before ;
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+ import org .springframework .boot .context .embedded .LocalServerPort ;
9+ import org .springframework .boot .test .context .SpringBootTest ;
10+ import org .springframework .boot .test .web .client .TestRestTemplate ;
11+ import org .springframework .data .redis .connection .RedisConnection ;
12+ import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
13+ import org .springframework .http .*;
14+ import org .springframework .test .context .junit4 .SpringRunner ;
15+
16+ import java .util .Set ;
17+
18+ import static org .junit .Assert .assertEquals ;
19+ import static org .junit .Assert .assertTrue ;
20+
21+ @ RunWith (SpringRunner .class )
22+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
23+ public class TomcatControllerTest {
24+
25+ @ Autowired
26+ private TestRestTemplate restTemplate ;
27+ @ LocalServerPort
28+ private int port ;
29+ @ Autowired
30+ private JedisConnectionFactory jedisConnectionFactory ;
31+ private RedisConnection connection ;
32+
33+ @ Before
34+ public void clearRedisData () {
35+ connection = jedisConnectionFactory .getConnection ();
36+ connection .flushAll ();
37+ }
38+
39+ @ Test
40+ public void testRedisIsEmpty () {
41+ Set <byte []> result = connection .keys ("*" .getBytes ());
42+ assertEquals (0 , result .size ());
43+ }
44+
45+ @ Test
46+ public void testForbiddenToProtectedEndpoint () {
47+ ResponseEntity <String > result = restTemplate .getForEntity ("/tomcat/admin" , String .class );
48+ assertEquals (HttpStatus .UNAUTHORIZED , result .getStatusCode ());
49+ }
50+
51+ @ Test
52+ public void testLoginAddsRedisKey () {
53+ ResponseEntity <String > result = makeRequest ();
54+ assertEquals ("hello tomcat admin" , result .getBody ()); //login worked
55+
56+ Set <byte []> redisResult = connection .keys ("*" .getBytes ());
57+ assertTrue (redisResult .size () > 0 ); //redis was populated with data
58+ }
59+
60+ @ Test //requires that the jetty service is running on port 8081
61+ public void testFailureAccessingJettyResourceWithTomcatSessionToken () {
62+ //call the jetty server with the token
63+ ResponseEntity <String > jettyResult = restTemplate .getForEntity ("http://localhost:8081" , String .class );
64+ assertEquals (HttpStatus .UNAUTHORIZED , jettyResult .getStatusCode ()); //login worked
65+ }
66+
67+ @ Test //requires that the jetty service is running on port 8081
68+ public void testAccessingJettyResourceWithTomcatSessionToken () {
69+ //login to get a session token
70+ ResponseEntity <String > result = makeRequest ();
71+ assertEquals ("hello tomcat admin" , result .getBody ()); //login worked
72+
73+ assertTrue (result .getHeaders ().containsKey ("Set-Cookie" ));
74+
75+ String setCookieValue = result .getHeaders ().get ("Set-Cookie" ).get (0 );
76+ String sessionCookie = setCookieValue .split (";" )[0 ];
77+ String sessionValue = sessionCookie .split ("=" )[1 ];
78+
79+ //Add session token to headers
80+ HttpHeaders headers = new HttpHeaders ();
81+ headers .add ("x-auth-token" , sessionValue );
82+
83+ //call the jetty server with the token
84+ HttpEntity <String > request = new HttpEntity <>(headers );
85+ ResponseEntity <String > jettyResult = restTemplate .exchange ("http://localhost:8081" , HttpMethod .GET , request , String .class );
86+ assertEquals ("hello Jetty" , jettyResult .getBody ()); //login worked
87+
88+ }
89+
90+ private ResponseEntity <String > makeRequest () {
91+ String plainCreds = "admin:password" ;
92+ byte [] plainCredsBytes = plainCreds .getBytes ();
93+ byte [] base64CredsBytes = Base64 .encodeBase64 (plainCredsBytes );
94+ String base64Creds = new String (base64CredsBytes );
95+
96+ HttpHeaders headers = new HttpHeaders ();
97+ headers .add ("Authorization" , "Basic " + base64Creds );
98+
99+ HttpEntity <String > request = new HttpEntity <>(headers );
100+ return restTemplate .exchange ("http://localhost:" + port + "/tomcat/admin" , HttpMethod .GET , request , String .class );
101+ }
102+
103+ }
0 commit comments