org.springframework.boot.test.TestRestTemplate Java Examples

The following examples show how to use org.springframework.boot.test.TestRestTemplate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebSecurityTests.java    From java-webapp-security-examples with Apache License 2.0 7 votes vote down vote up
@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());
}
 
Example #2
Source File: WebSecurityTests.java    From java-webapp-security-examples with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #3
Source File: SpringBootSimpleExampleApplicationTests.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example #4
Source File: SampleWebUiApplicationTests.java    From enhanced-pet-clinic with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #5
Source File: BaseTests.java    From enhanced-pet-clinic with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #6
Source File: BaseTests.java    From enhanced-pet-clinic with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #7
Source File: BaseTests.java    From enhanced-pet-clinic with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: UserControllerTest.java    From spring-boot-shiro-orientdb with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #9
Source File: UserControllerTest.java    From spring-boot-shiro-orientdb with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #10
Source File: HttpTest.java    From mule-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #11
Source File: WebSecurityTests.java    From java-webapp-security-examples with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #12
Source File: WebSecurityTests.java    From java-webapp-security-examples with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #13
Source File: WebSecurityTests.java    From java-webapp-security-examples with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #14
Source File: GoogleApplicationTests.java    From spring-boot-google-auth with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #15
Source File: GoogleApplicationTests.java    From spring-boot-google-auth with Apache License 2.0 5 votes vote down vote up
@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"))));
}
 
Example #16
Source File: SpringBootSimpleExampleApplicationTests.java    From katharsis-framework with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example #17
Source File: LatticeDiscoveryClientConfiguration.java    From spring-cloud-lattice with Apache License 2.0 4 votes vote down vote up
private ClientHttpRequestFactory getClientRequestFactory() {
	String username = latticeProperties.getReceptor().getUsername();
	String password = latticeProperties.getReceptor().getPassword();
	return new TestRestTemplate(username, password).getRequestFactory();
}
 
Example #18
Source File: WebSecurityTests.java    From java-webapp-security-examples with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example #19
Source File: ApplicationTests.java    From blog-microservices with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example #20
Source File: ApplicationTests.java    From blog-microservices with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example #21
Source File: HelloBrotliHttpControllerTest.java    From jbrotli with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
  this.root_url = "http://127.0.0.1:" + localServerPort;
  restTemplate = new TestRestTemplate();
}
 
Example #22
Source File: WebSecurityTests.java    From java-webapp-security-examples with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example #23
Source File: DetectionTest.java    From weslang with Apache License 2.0 4 votes vote down vote up
@Before
public void testSetup() {
  this.rest = new TestRestTemplate();
  this.baseUrl = "http://localhost:" + this.port;
}
 
Example #24
Source File: BaseTests.java    From enhanced-pet-clinic with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #25
Source File: BaseTests.java    From enhanced-pet-clinic with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #26
Source File: BaseTests.java    From enhanced-pet-clinic with Apache License 2.0 4 votes vote down vote up
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;
}