Skip to content

Commit bc85f38

Browse files
author
Dave Syer
committed
Add test for gh-1058
1 parent f47b40f commit bc85f38

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package sample.actuator;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import java.util.Map;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.boot.test.IntegrationTest;
29+
import org.springframework.boot.test.SpringApplicationConfiguration;
30+
import org.springframework.boot.test.TestRestTemplate;
31+
import org.springframework.http.HttpStatus;
32+
import org.springframework.http.ResponseEntity;
33+
import org.springframework.test.annotation.DirtiesContext;
34+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35+
import org.springframework.test.context.web.WebAppConfiguration;
36+
37+
/**
38+
* Integration tests for endpoints configuration.
39+
*
40+
* @author Dave Syer
41+
*/
42+
@RunWith(SpringJUnit4ClassRunner.class)
43+
@SpringApplicationConfiguration(classes = SampleActuatorApplication.class)
44+
@WebAppConfiguration
45+
@IntegrationTest({"server.port=0", "management.context_path=/admin"})
46+
@DirtiesContext
47+
public class ManagementPathSampleActuatorApplicationTests {
48+
49+
@Value("${local.server.port}")
50+
private int port;
51+
52+
@Test
53+
public void testHealth() throws Exception {
54+
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
55+
"http://localhost:" + this.port + "/admin/health", String.class);
56+
assertEquals(HttpStatus.OK, entity.getStatusCode());
57+
assertTrue("Wrong body: " + entity.getBody(),
58+
entity.getBody().contains("\"status\":\"UP\""));
59+
}
60+
61+
@Test
62+
public void testHomeIsSecure() throws Exception {
63+
@SuppressWarnings("rawtypes")
64+
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
65+
"http://localhost:" + this.port, Map.class);
66+
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
67+
@SuppressWarnings("unchecked")
68+
Map<String, Object> body = entity.getBody();
69+
assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
70+
assertFalse("Wrong headers: " + entity.getHeaders(), entity.getHeaders()
71+
.containsKey("Set-Cookie"));
72+
}
73+
74+
}

0 commit comments

Comments
 (0)