Skip to content

Commit 7d6b72a

Browse files
committed
BAEL-315 Adding test class for demonstrating features of Spring Session and Spring Cloud
1 parent df3eebe commit 7d6b72a

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.spring.cloud.bootstrap.gateway;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.springframework.boot.test.web.client.TestRestTemplate;
6+
import org.springframework.http.*;
7+
import org.springframework.util.LinkedMultiValueMap;
8+
import org.springframework.util.MultiValueMap;
9+
10+
public class GatewayApplicationTest {
11+
12+
@Test
13+
public void testAccess() throws Exception {
14+
TestRestTemplate testRestTemplate = new TestRestTemplate();
15+
String testUrl = "http://localhost:8080";
16+
17+
ResponseEntity<String> response = testRestTemplate.getForEntity(testUrl + "/resource/hello/cloud", String.class);
18+
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
19+
Assert.assertEquals("hello cloud", response.getBody());
20+
21+
//try the protected resource and confirm the redirect to login
22+
response = testRestTemplate.getForEntity(testUrl + "/resource/hello/user", String.class);
23+
Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode());
24+
Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0));
25+
26+
//login as user/password
27+
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
28+
form.add("username", "user");
29+
form.add("password", "password");
30+
response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
31+
32+
//extract the session from the cookie and propagate it to the next request
33+
String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
34+
HttpHeaders headers = new HttpHeaders();
35+
headers.add("Cookie", sessionCookie);
36+
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
37+
38+
//request the protected resource
39+
response = testRestTemplate.exchange(testUrl + "/resource/hello/user", HttpMethod.GET, httpEntity, String.class);
40+
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
41+
Assert.assertEquals("hello cloud user", response.getBody());
42+
43+
//request the admin protected resource to determine it is still protected
44+
response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class);
45+
Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
46+
47+
//login as the admin
48+
form.clear();
49+
form.add("username", "admin");
50+
form.add("password", "admin");
51+
response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class);
52+
53+
//extract the session from the cookie and propagate it to the next request
54+
sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0];
55+
headers = new HttpHeaders();
56+
headers.add("Cookie", sessionCookie);
57+
httpEntity = new HttpEntity<>(headers);
58+
59+
//request the protected resource
60+
response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class);
61+
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
62+
Assert.assertEquals("hello cloud admin", response.getBody());
63+
64+
//request the discovery resources as the admin
65+
response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class);
66+
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
67+
}
68+
69+
}

0 commit comments

Comments
 (0)