Search in sources :

Example 21 with TestRestTemplate

use of org.springframework.boot.test.web.client.TestRestTemplate in project spring-boot by spring-projects.

the class HalBrowserMvcEndpointServerContextPathIntegrationTests method actuatorBrowserEntryPoint.

@Test
public void actuatorBrowserEntryPoint() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/spring/actuator/browser.html", HttpMethod.GET, new HttpEntity<Void>(null, headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).contains("entryPoint: '/spring/actuator'");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 22 with TestRestTemplate

use of org.springframework.boot.test.web.client.TestRestTemplate in project spring-boot by spring-projects.

the class HalBrowserMvcEndpointServerContextPathIntegrationTests method actuatorBrowserRedirect.

@Test
public void actuatorBrowserRedirect() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/spring/actuator/", HttpMethod.GET, new HttpEntity<Void>(null, headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
    assertThat(entity.getHeaders().getLocation()).isEqualTo(URI.create("http://localhost:" + this.port + "/spring/actuator/browser.html"));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with TestRestTemplate

use of org.springframework.boot.test.web.client.TestRestTemplate in project spring-boot by spring-projects.

the class HalBrowserMvcEndpointServerPortIntegrationTests method links.

@Test
public void links() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/actuator", HttpMethod.GET, new HttpEntity<Void>(null, headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).contains("\"_links\":");
    assertThat(entity.getBody()).contains(":" + this.port);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 24 with TestRestTemplate

use of org.springframework.boot.test.web.client.TestRestTemplate in project spring-boot by spring-projects.

the class OAuth2AutoConfigurationTests method verifyAuthentication.

private void verifyAuthentication(ClientDetails config, HttpStatus finalStatus) {
    String baseUrl = "http://localhost:" + this.context.getWebServer().getPort();
    TestRestTemplate rest = new TestRestTemplate();
    // First, verify the web endpoint can't be reached
    assertEndpointUnauthorized(baseUrl, rest);
    // Since we can't reach it, need to collect an authorization token
    HttpHeaders headers = getHeaders(config);
    String url = baseUrl + "/oauth/token";
    JsonNode tokenResponse = rest.postForObject(url, new HttpEntity<>(getBody(), headers), JsonNode.class);
    String authorizationToken = tokenResponse.findValue("access_token").asText();
    String tokenType = tokenResponse.findValue("token_type").asText();
    String scope = tokenResponse.findValues("scope").get(0).toString();
    assertThat(tokenType).isEqualTo("bearer");
    assertThat(scope).isEqualTo("\"read\"");
    // Now we should be able to see that endpoint.
    headers.set("Authorization", "BEARER " + authorizationToken);
    ResponseEntity<String> securedResponse = rest.exchange(new RequestEntity<Void>(headers, HttpMethod.GET, URI.create(baseUrl + "/securedFind")), String.class);
    assertThat(securedResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(securedResponse.getBody()).isEqualTo("You reached an endpoint " + "secured by Spring Security OAuth2");
    ResponseEntity<String> entity = rest.exchange(new RequestEntity<Void>(headers, HttpMethod.POST, URI.create(baseUrl + "/securedSave")), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(finalStatus);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 25 with TestRestTemplate

use of org.springframework.boot.test.web.client.TestRestTemplate in project spring-boot by spring-projects.

the class MustacheAutoConfigurationIntegrationTests method testPartialPage.

@Test
public void testPartialPage() throws Exception {
    String body = new TestRestTemplate().getForObject("http://localhost:" + this.port + "/partial", String.class);
    assertThat(body.contains("Hello World")).isTrue();
}
Also used : TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)70 Test (org.junit.Test)67 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)53 Map (java.util.Map)23 HttpHeaders (org.springframework.http.HttpHeaders)15 RequestEntity (org.springframework.http.RequestEntity)2 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 File (java.io.File)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 JSONArray (net.minidev.json.JSONArray)1 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)1 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1