Package org.springframework.boot.test

Examples of org.springframework.boot.test.TestRestTemplate


  @Test
  public void testError() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange(
        "http://localhost:" + this.port + "/error", HttpMethod.GET,
        new HttpEntity<Void>(headers), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody()
        .contains("<html>"));
View Full Code Here


  }

  @Test
  public void testHomeIsSecure() throws Exception {
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> entity = new TestRestTemplate().getForEntity(
        "http://localhost:" + this.port, Map.class);
    assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
    @SuppressWarnings("unchecked")
    Map<String, Object> body = entity.getBody();
    assertEquals("Wrong body: " + body, "Unauthorized", body.get("error"));
View Full Code Here

  @Test
  public void testHome() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange(
        "http://localhost:" + this.port, HttpMethod.GET, new HttpEntity<Void>(
            headers), String.class);
    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
    assertTrue("Wrong location:\n" + entity.getHeaders(), entity.getHeaders()
        .getLocation().toString().endsWith(port + "/login"));
View Full Code Here

  @Test
  public void testLoginPage() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = new TestRestTemplate().exchange(
        "http://localhost:" + this.port + "/login", HttpMethod.GET,
        new HttpEntity<Void>(headers), String.class);
    assertEquals(HttpStatus.OK, entity.getStatusCode());
    assertTrue("Wrong content:\n" + entity.getBody(),
        entity.getBody().contains("_csrf"));
View Full Code Here

    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", "user");
    form.set("password", "user");
    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());
    assertTrue("Wrong location:\n" + entity.getHeaders(), entity.getHeaders()
View Full Code Here

        entity.getHeaders().get("Set-Cookie"));
  }

  private HttpHeaders getHeaders() {
    HttpHeaders headers = new HttpHeaders();
    ResponseEntity<String> page = new TestRestTemplate().getForEntity(
        "http://localhost:" + this.port + "/login", String.class);
    assertEquals(HttpStatus.OK, page.getStatusCode());
    String cookie = page.getHeaders().getFirst("Set-Cookie");
    headers.set("Cookie", cookie);
    Matcher matcher = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*")
 
View Full Code Here

    return headers;
  }

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

  @Test
  public void testHome() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(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>Login"));
View Full Code Here

    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    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<MultiValueMap<String, String>>(form, headers),
        String.class);
    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
    assertEquals("http://localhost:" + this.port + "/", entity.getHeaders()
View Full Code Here

    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"));
View Full Code Here

TOP

Related Classes of org.springframework.boot.test.TestRestTemplate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.