BAEL-89 Removing spring from integration test and converting to unit test.

This commit is contained in:
tschiman 2016-12-07 21:27:57 -07:00
parent 35020fc085
commit f3b79ff214
1 changed files with 21 additions and 43 deletions

View File

@ -1,86 +1,64 @@
package com.baeldung.spring.session; package com.baeldung.spring.session;
import org.apache.tomcat.util.codec.binary.Base64;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner; import redis.clients.jedis.Jedis;
import java.util.Set; import java.util.Set;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SessionControllerTest { public class SessionControllerTest {
@Autowired private Jedis jedis;
private TestRestTemplate restTemplate; private TestRestTemplate testRestTemplate;
@Autowired private TestRestTemplate testRestTemplateWithAuth;
private JedisConnectionFactory jedisConnectionFactory; private String testUrl = "http://localhost:8080/";
private RedisConnection connection;
@Before @Before
public void clearRedisData() { public void clearRedisData() {
connection = jedisConnectionFactory.getConnection(); testRestTemplate = new TestRestTemplate();
connection.flushAll(); testRestTemplateWithAuth = new TestRestTemplate("admin", "password", null);
jedis = new Jedis("localhost", 6379);
jedis.flushAll();
} }
@Test @Test
public void testRedisIsEmpty() { public void testRedisIsEmpty() {
Set<byte[]> result = connection.keys("*".getBytes()); Set<String> result = jedis.keys("*");
assertEquals(0, result.size()); assertEquals(0, result.size());
} }
@Test @Test
public void testUnauthenticatedCantAccess() { public void testUnauthenticatedCantAccess() {
ResponseEntity<String> result = restTemplate.getForEntity("/", String.class); ResponseEntity<String> result = testRestTemplate.getForEntity(testUrl, String.class);
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode()); assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
} }
@Test @Test
public void testRedisControlsSession() { public void testRedisControlsSession() {
ResponseEntity<String> result = restTemplate.exchange("/", HttpMethod.GET, makeAuthRequest(), String.class); ResponseEntity<String> result = testRestTemplateWithAuth.getForEntity(testUrl, String.class);
assertEquals("hello admin", result.getBody()); //login worked assertEquals("hello admin", result.getBody()); //login worked
Set<byte[]> redisResult = connection.keys("*".getBytes()); Set<String> redisResult = jedis.keys("*");
assertTrue(redisResult.size() > 0); //redis is populated with session data assertTrue(redisResult.size() > 0); //redis is populated with session data
String sessionCookie = result.getHeaders().get("Set-Cookie").get(0).split(";")[0]; String sessionCookie = result.getHeaders().get("Set-Cookie").get(0).split(";")[0];
result = restTemplate.exchange("/", HttpMethod.GET, makeRequestWithCookie(sessionCookie), String.class); HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", sessionCookie);
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
result = testRestTemplate.exchange(testUrl, HttpMethod.GET, httpEntity, String.class);
assertEquals("hello admin", result.getBody()); //access with session works worked assertEquals("hello admin", result.getBody()); //access with session works worked
connection.flushAll(); //clear all keys in redis jedis.flushAll(); //clear all keys in redis
result = restTemplate.exchange("/", HttpMethod.GET, makeRequestWithCookie(sessionCookie), String.class); result = testRestTemplate.exchange(testUrl, HttpMethod.GET, httpEntity, String.class);
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());//access denied after sessions are removed in redis assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());//access denied after sessions are removed in redis
} }
private HttpEntity<String> makeRequestWithCookie(String sessionCookie) {
HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", sessionCookie);
return new HttpEntity<>(headers);
}
private HttpEntity<String> makeAuthRequest() {
String plainCreds = "admin:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
return new HttpEntity<>(headers);
}
} }