本文整理汇总了Java中org.springframework.boot.test.TestRestTemplate类的典型用法代码示例。如果您正苦于以下问题:Java TestRestTemplate类的具体用法?Java TestRestTemplate怎么用?Java TestRestTemplate使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TestRestTemplate类属于org.springframework.boot.test包,在下文中一共展示了TestRestTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testHome
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testHome() throws Exception {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build();
TestRestTemplate testRestTemplate = new TestRestTemplate();
((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory())
.setHttpClient(httpClient);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertTrue( "Result is not Matched with Server Response",entity.getBody().contains("welcome to the application"));
}
示例2: envPostAvailable
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void envPostAvailable() {
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().postForEntity(
"http://localhost:" + port + "/admin/env", form, Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
}
示例3: testCompression
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testCompression() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new TestRestTemplate();
ResponseEntity<byte[]> entity = restTemplate.exchange(
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody()));
try {
assertEquals("welcome to the application "+System.getProperty("user.name"),
StreamUtils.copyToString(inflater, Charset.forName("UTF-8")));
}
finally {
inflater.close();
}
}
示例4: testCompression
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testCompression() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new TestRestTemplate();
ResponseEntity<byte[]> entity = restTemplate.exchange(
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody()));
try {
assertEquals("welcome to the application Adarsh",
StreamUtils.copyToString(inflater, Charset.forName("UTF-8")));
}
finally {
inflater.close();
}
}
示例5: testWelcome
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testWelcome() throws Exception {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build();
TestRestTemplate testRestTemplate = new TestRestTemplate();
((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory())
.setHttpClient(httpClient);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("welcome to the application "+System.getProperty("user.name"), entity.getBody());
}
示例6: testCompression
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testCompression() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new TestRestTemplate();
ResponseEntity<byte[]> entity = restTemplate.exchange(
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody()));
try {
assertEquals("Hello World",
StreamUtils.copyToString(inflater, Charset.forName("UTF-8")));
}
finally {
inflater.close();
}
}
示例7: testDenied
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testDenied() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
form.set("username", "user");
form.set("password", "user");
getCsrf(form, headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/login", HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers),
String.class);
assertEquals(HttpStatus.FOUND, entity.getStatusCode());
String cookie = entity.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie);
ResponseEntity<String> page = new TestRestTemplate().exchange(
entity.getHeaders().getLocation(), HttpMethod.GET,
new HttpEntity<Void>(headers), String.class);
assertEquals(HttpStatus.FORBIDDEN, page.getStatusCode());
assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(),
page.getBody().contains("Access denied"));
}
示例8: testHome
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testHome() throws Exception {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build();
TestRestTemplate testRestTemplate = new TestRestTemplate();
((HttpComponentsClientHttpRequestFactory) testRestTemplate.getRequestFactory())
.setHttpClient(httpClient);
ResponseEntity<String> entity = testRestTemplate
.getForEntity("https://localhost:" + this.port, String.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("Hello World", entity.getBody());
}
示例9: testCompression
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testCompression() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new TestRestTemplate();
ResponseEntity<byte[]> entity = restTemplate.exchange(
"http://localhost:" + this.port, HttpMethod.GET, requestEntity,
byte[].class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
GZIPInputStream inflater = new GZIPInputStream(
new ByteArrayInputStream(entity.getBody()));
try {
assertEquals("Hello World",
StreamUtils.copyToString(inflater, Charset.forName("UTF-8")));
}
finally {
inflater.close();
}
}
示例10: testMetricsIsSecure
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testMetricsIsSecure() throws Exception {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/metrics", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
entity = new TestRestTemplate()
.getForEntity("http://localhost:" + this.port + "/metrics/", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/metrics/foo", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
entity = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/metrics.json", Map.class);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
}
示例11: testDenied
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testDenied() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.set("username", "admin");
form.set("password", "admin");
getCsrf(form, headers);
ResponseEntity<String> entity = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/login", HttpMethod.POST,
new HttpEntity<>(form, headers),
String.class);
assertEquals(HttpStatus.FOUND, entity.getStatusCode());
String cookie = entity.getHeaders().getFirst("Set-Cookie");
headers.set("Cookie", cookie);
ResponseEntity<String> page = new TestRestTemplate().exchange(entity.getHeaders()
.getLocation(), HttpMethod.GET, new HttpEntity<Void>(headers),
String.class);
assertEquals(HttpStatus.OK, page.getStatusCode());
cookie = entity.getHeaders().getFirst("Set-Cookie");
assertTrue(cookie.contains("remember-me"));
assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(), page
.getBody().contains("Invalid username and password"));
}
示例12: testTrace
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void testTrace() throws Exception {
new TestRestTemplate().getForEntity("http://localhost:" + this.port + "/health",
String.class);
@SuppressWarnings("rawtypes")
ResponseEntity<List> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/trace", List.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked")
List<Map<String, Object>> list = entity.getBody();
Map<String, Object> trace = list.get(list.size() - 1);
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) trace
.get("info")).get("headers")).get("response");
assertEquals("200", map.get("status"));
}
示例13: configurationAvailable
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void configurationAvailable() {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
"http://localhost:" + port + "/app/cloud", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
}
示例14: test_authenticate_success
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void test_authenticate_success() throws JsonProcessingException {
// authenticate
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
final String json = new ObjectMapper().writeValueAsString(
new UsernamePasswordToken(USER_EMAIL, USER_PWD));
System.out.println(json);
final ResponseEntity<String> response = new TestRestTemplate(
HttpClientOption.ENABLE_COOKIES).exchange(BASE_URL.concat("/auth"),
HttpMethod.POST, new HttpEntity<>(json, headers), String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}
示例15: test_authenticate_failure
import org.springframework.boot.test.TestRestTemplate; //导入依赖的package包/类
@Test
public void test_authenticate_failure() throws JsonProcessingException {
// authenticate
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
final String json = new ObjectMapper().writeValueAsString(
new UsernamePasswordToken(USER_EMAIL, "wrong password"));
System.out.println(json);
final ResponseEntity<String> response = new TestRestTemplate(
HttpClientOption.ENABLE_COOKIES).exchange(BASE_URL.concat("/auth"),
HttpMethod.POST, new HttpEntity<>(json, headers), String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
}