org.springframework.boot.test.TestRestTemplate源码实例Demo

类org.springframework.boot.test.TestRestTemplate源码实例Demo

下面列出了org.springframework.boot.test.TestRestTemplate 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


@Test
public void testLogin() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
    MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
    form.set("username", "user");
    form.set("password", "password");
    form.set("remember-me", "true");
    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());
    List<String> cookies = entity.getHeaders().get("Set-Cookie");
    assertTrue(cookies.toString().contains("remember-me"));
    assertEquals("http://localhost:" + this.port + "/", entity.getHeaders()
            .getLocation().toString());
}
 

@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"));
}
 

@Test
public void testTestEndpointWithQueryParams() throws Exception {
    TestRestTemplate testRestTemplate = new TestRestTemplate();
    ResponseEntity<String> response = testRestTemplate
            .getForEntity("http://localhost:" + this.port + "/api/tasks?filter[tasks][name]=John", String.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertThatJson(response.getBody()).node("data[0].attributes.name").isStringEqualTo("John");
    assertThatJson(response.getBody()).node("data[0].links.self").isStringEqualTo("http://localhost:8080/api/tasks/1");
    assertThatJson(response.getBody()).node("meta.name").isStringEqualTo("meta information");
}
 

@Test
public void testTestCustomEndpoint() throws Exception {
    TestRestTemplate testRestTemplate = new TestRestTemplate();
    ResponseEntity<String> response = testRestTemplate
            .getForEntity("http://localhost:" + this.port + "/api/custom", String.class);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(response.getBody(), "hello");
}
 

@Test
public void everythingIsSecuredByDefault() throws Exception {
	TestRestTemplate restTemplate = new TestRestTemplate();
	ResponseEntity<Void> entity = restTemplate
			.getForEntity("http://localhost:" + this.port, Void.class);
	assertThat(entity.getStatusCode(), is(HttpStatus.FOUND));
	assertThat(entity.getHeaders().getLocation(),
			is(equalTo(URI.create("http://localhost:" + this.port + "/login"))));
}
 

@Test
public void loginRedirectsToGithub() throws Exception {
	TestRestTemplate restTemplate = new TestRestTemplate();
	ResponseEntity<Void> entity = restTemplate
			.getForEntity("http://localhost:" + this.port + "/login", Void.class);
	assertThat(entity.getStatusCode(), is(HttpStatus.FOUND));
	assertThat(entity.getHeaders().getLocation().toString(),
			startsWith("https://github.com/login/oauth"));
}
 

@Test
public void testHome() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange(
            "http://localhost:" + this.port, HttpMethod.GET, new HttpEntity<Void>(
                    headers), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body (title doesn't match):\n" + entity.getBody(), entity
            .getBody().contains("<title>Spring"));
}
 

@Test
public void testProtected() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    ResponseEntity<String> entity = new TestRestTemplate().exchange(
            "http://localhost:" + this.port + "/api/health", HttpMethod.GET,
           new HttpEntity<>(headers),
           String.class);
    assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
}
 

private void getCsrf(MultiValueMap<String, String> form, HttpHeaders headers) {
    ResponseEntity<? extends CsrfToken> page = new TestRestTemplate().getForEntity(
            "http://localhost:" + this.port + "/api/csrf", CsrfToken.class);
    String cookie = page.getHeaders().getFirst("Set-Cookie");
    headers.set("Cookie", cookie);
    CsrfToken token = page.getBody();
    form.set(token.parameterName, token.token);
}
 
源代码10 项目: mule-spring-boot-starter   文件: HttpTest.java

@Test
public void testHttpRequest() {
	// Given
	// When
	ResponseEntity<String> response = new TestRestTemplate().getForEntity("http://localhost:8081/echo", String.class);
	// Then
	assertEquals(HttpStatus.OK, response.getStatusCode());
	assertEquals("/echo", response.getBody());
}
 

@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));
}
 

@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.UNAUTHORIZED));
}
 
源代码13 项目: enhanced-pet-clinic   文件: BaseTests.java

protected ResponseEntity<String> sendRequest(URI uri, HttpMethod method) {
	ResponseEntity<String> page = new TestRestTemplate().exchange(uri, method, new HttpEntity<Void>(httpHeaders),
			String.class);
	saveCSRFAndCookieValues(page);

	return page;
}
 
源代码14 项目: enhanced-pet-clinic   文件: BaseTests.java

protected ResponseEntity<String> sendRequest(String url, HttpMethod method) {
	ResponseEntity<String> page = new TestRestTemplate().exchange(url, method, new HttpEntity<Void>(httpHeaders),
			String.class);
	saveCSRFAndCookieValues(page);

	return page;
}
 
源代码15 项目: enhanced-pet-clinic   文件: BaseTests.java

protected ResponseEntity<String> sendRequest(String url, HttpMethod method, MultiValueMap<String, String> form) {
	ResponseEntity<String> page = new TestRestTemplate().exchange(url, method,
			new HttpEntity<MultiValueMap<String, String>>(form, httpHeaders), String.class);
	saveCSRFAndCookieValues(page);

	return page;
}
 

@Test
public void testCss() throws Exception {
	ResponseEntity<String> entity = new TestRestTemplate()
			.getForEntity("http://localhost:" + this.port + "/css/main.css", String.class);
	assertEquals(HttpStatus.OK, entity.getStatusCode());
	assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
}
 

@Test
public void testAuthorizedAccess() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate("user", "password")
            .getForEntity("http://localhost:" + this.port + "/api/health", String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
}
 

@Test
public void testUnauthorizedAccess() throws Exception {
    ResponseEntity<String> entity = new TestRestTemplate("admin", "admin")
            .getForEntity("http://localhost:" + this.port + "/api/health", String.class);
    assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
}
 

private ClientHttpRequestFactory getClientRequestFactory() {
	String username = latticeProperties.getReceptor().getUsername();
	String password = latticeProperties.getReceptor().getPassword();
	return new TestRestTemplate(username, password).getRequestFactory();
}
 

@Test
public void catalogLoads() {
	@SuppressWarnings("rawtypes")
	ResponseEntity<Map> entity = new TestRestTemplate("user", "password").getForEntity("http://localhost:" + port + "/eureka/apps", Map.class);
	assertEquals(HttpStatus.OK, entity.getStatusCode());
}
 

@Test
public void adminLoads() {
	@SuppressWarnings("rawtypes")
	ResponseEntity<Map> entity = new TestRestTemplate("user", "password").getForEntity("http://localhost:" + port + "/env", Map.class);
	assertEquals(HttpStatus.OK, entity.getStatusCode());
}
 

@BeforeMethod
public void setUp() throws Exception {
  this.root_url = "http://127.0.0.1:" + localServerPort;
  restTemplate = new TestRestTemplate();
}
 
源代码23 项目: weslang   文件: DetectionTest.java

@Before
public void testSetup() {
  this.rest = new TestRestTemplate();
  this.baseUrl = "http://localhost:" + this.port;
}
 
源代码24 项目: enhanced-pet-clinic   文件: BaseTests.java

protected ResponseEntity<String> getPage(String url) {
	ResponseEntity<String> page = new TestRestTemplate().exchange(url, HttpMethod.GET,
			new HttpEntity<Void>(httpHeaders), String.class);
	saveCSRFAndCookieValues(page);
	return page;
}
 
源代码25 项目: enhanced-pet-clinic   文件: BaseTests.java

protected ResponseEntity<String> getPage(URI uri) {
	ResponseEntity<String> page = new TestRestTemplate().exchange(uri, HttpMethod.GET,
			new HttpEntity<Void>(httpHeaders), String.class);
	saveCSRFAndCookieValues(page);
	return page;
}
 
源代码26 项目: enhanced-pet-clinic   文件: BaseTests.java

protected ResponseEntity<String> postPage(String formAction, MultiValueMap<String, String> form) {
	ResponseEntity<String> page = new TestRestTemplate().exchange("http://localhost:" + this.port + formAction,
			HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, httpHeaders), String.class);
	saveCSRFAndCookieValues(page);
	return page;
}
 
如果文章对您有帮助,请点击页面广告。以鼓励作者做出的努力。
 类所在包
 类方法
 同包方法