当前位置: 首页>>代码示例>>Java>>正文


Java TestRestTemplate类代码示例

本文整理汇总了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"));
}
 
开发者ID:adarshkumarsingh83,项目名称:spring_boot,代码行数:18,代码来源:TomcatSSLApplicationTests.java

示例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());
}
 
开发者ID:zhaoqilong3031,项目名称:spring-cloud-samples,代码行数:9,代码来源:ApplicationTests.java

示例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();
	}
}
 
开发者ID:adarshkumarsingh83,项目名称:spring_boot,代码行数:25,代码来源:JettyApplicationTests.java

示例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();
	}
}
 
开发者ID:adarshkumarsingh83,项目名称:spring_boot,代码行数:25,代码来源:TomcatApplicationTests.java

示例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());
}
 
开发者ID:adarshkumarsingh83,项目名称:spring_boot,代码行数:18,代码来源:JettyApplicationTests.java

示例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();
	}
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:25,代码来源:SampleJettyApplicationTests.java

示例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"));
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:23,代码来源:SampleMethodSecurityApplicationTests.java

示例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());
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:18,代码来源:SampleJettySslApplicationTests.java

示例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();
	}
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:21,代码来源:SampleJetty93ApplicationTests.java

示例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());
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:17,代码来源:SampleActuatorApplicationTests.java

示例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"));
}
 
开发者ID:mraible,项目名称:java-webapp-security-examples,代码行数:25,代码来源:WebSecurityTests.java

示例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"));
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:17,代码来源:SampleActuatorApplicationTests.java

示例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());
}
 
开发者ID:zhaoqilong3031,项目名称:spring-cloud-samples,代码行数:8,代码来源:ApplicationTests.java

示例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));
}
 
开发者ID:auslides,项目名称:stateless-shiro,代码行数:15,代码来源:UserControllerTest.java

示例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));
}
 
开发者ID:auslides,项目名称:stateless-shiro,代码行数:15,代码来源:UserControllerTest.java


注:本文中的org.springframework.boot.test.TestRestTemplate类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。