Java Code Examples for org.springframework.boot.test.web.client.TestRestTemplate#postForEntity()

The following examples show how to use org.springframework.boot.test.web.client.TestRestTemplate#postForEntity() . 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: LocalServerTestSupport.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
/**
 * Starts up the Cloud Function Server and executes the test.
 */
public static <I, O> void verify(Class<?> mainClass, String function, I input, O expectedOutput) {
	try (ServerProcess serverProcess = LocalServerTestSupport.startServer(mainClass, function)) {
		TestRestTemplate testRestTemplate = new TestRestTemplate();

		HttpHeaders headers = new HttpHeaders();

		ResponseEntity<String> response = testRestTemplate.postForEntity(
				"http://localhost:" + serverProcess.getPort(), new HttpEntity<>(gson.toJson(input), headers),
				String.class);

		assertThat(response.getBody()).isEqualTo(gson.toJson(expectedOutput));
	}
	catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 2
Source File: FunctionInvokerIntegrationTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorResponse() {
	try (LocalServerTestSupport.ServerProcess serverProcess =
					LocalServerTestSupport.startServer(ErrorFunction.class, "errorFunction")) {

		TestRestTemplate testRestTemplate = new TestRestTemplate();

		HttpHeaders headers = new HttpHeaders();
		ResponseEntity<String> response = testRestTemplate.postForEntity(
				"http://localhost:" + serverProcess.getPort(), new HttpEntity<>("test", headers),
				String.class);

		assertThat(response.getStatusCode().is5xxServerError()).isTrue();
	}
	catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 3
Source File: FormIntegrationTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
public void multipartFormDataWorksRestTemplate() {
	MultiValueMap<String, HttpEntity<?>> formData = createMultipartData();
	TestRestTemplate rest = new TestRestTemplate();

	ResponseEntity<Map> response = rest.postForEntity(baseUri + "/post", formData,
			Map.class);

	assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
	assertMultipartData(response.getBody());
}
 
Example 4
Source File: UserSubmittedTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testIssue274() throws Exception {
	SpringApplication.run(Issue274Configuration.class);
	TestRestTemplate testRestTemplate = new TestRestTemplate();
	String port = System.getProperty("server.port");
	Thread.sleep(200);
	ResponseEntity<String> response = testRestTemplate
			.postForEntity(new URI("http://localhost:" + port + "/echo"), "", String.class);
	assertThat(response.getBody()).isNull();
	assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
 
Example 5
Source File: FunctionEndpointInitializerMVCTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFunctionMapping() throws Exception {
	SpringApplication.run(ApplicationConfiguration.class);
	TestRestTemplate testRestTemplate = new TestRestTemplate();
	String port = System.getProperty("server.port");
	ResponseEntity<String> response = testRestTemplate
			.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
	assertThat(response.getBody()).isEqualTo("STRESSED");
	response = testRestTemplate.postForEntity(new URI("http://localhost:" + port + "/reverse"), "stressed", String.class);
	assertThat(response.getBody()).isEqualTo("desserts");
}
 
Example 6
Source File: FunctionEndpointInitializerMVCTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompositionFunctionMapping() throws Exception {
	SpringApplication.run(ApplicationConfiguration.class);
	TestRestTemplate testRestTemplate = new TestRestTemplate();
	String port = System.getProperty("server.port");
	ResponseEntity<String> response = testRestTemplate
			.postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class);
	assertThat(response.getBody()).isEqualTo("desserts");
}
 
Example 7
Source File: FunctionEndpointInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonExistingFunction() throws Exception {
	FunctionalSpringApplication.run(ApplicationConfiguration.class);
	TestRestTemplate testRestTemplate = new TestRestTemplate();
	String port = System.getProperty("server.port");
	Thread.sleep(200);
	ResponseEntity<String> response = testRestTemplate
			.postForEntity(new URI("http://localhost:" + port + "/foo"), "stressed", String.class);
	assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
 
Example 8
Source File: FunctionEndpointInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleFunctionMapping() throws Exception {
	FunctionalSpringApplication.run(ApplicationConfiguration.class);
	TestRestTemplate testRestTemplate = new TestRestTemplate();
	String port = System.getProperty("server.port");
	Thread.sleep(200);
	ResponseEntity<String> response = testRestTemplate
			.postForEntity(new URI("http://localhost:" + port + "/uppercase"), "stressed", String.class);
	assertThat(response.getBody()).isEqualTo("STRESSED");
	response = testRestTemplate.postForEntity(new URI("http://localhost:" + port + "/reverse"), "stressed", String.class);
	assertThat(response.getBody()).isEqualTo("desserts");
}
 
Example 9
Source File: FunctionEndpointInitializerTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompositionFunctionMapping() throws Exception {
	FunctionalSpringApplication.run(ApplicationConfiguration.class);
	TestRestTemplate testRestTemplate = new TestRestTemplate();
	String port = System.getProperty("server.port");
	Thread.sleep(200);
	ResponseEntity<String> response = testRestTemplate
			.postForEntity(new URI("http://localhost:" + port + "/uppercase,lowercase,reverse"), "stressed", String.class);
	assertThat(response.getBody()).isEqualTo("desserts");
}
 
Example 10
Source File: RefreshEndpointIntegrationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void webAccess() throws Exception {
	TestRestTemplate template = new TestRestTemplate();
	ResponseEntity<String> entity = template.postForEntity(
			"http://localhost:" + this.port + BASE_PATH + "/refresh", null,
			String.class);
	assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
}
 
Example 11
Source File: BasicAuthConfigurationIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenCorrectCredentials_whenLogin_ThenSuccess() throws IllegalStateException, IOException {
	restTemplate = new TestRestTemplate();
	User user = new User();
	user.setUserName("user");
	user.setPassword("password");
    ResponseEntity<String> response = restTemplate.postForEntity(base.toString()+"/login",user, String.class);

    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertTrue(response
      .getBody()
      .contains("true"));
}
 
Example 12
Source File: BasicAuthConfigurationIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenWrongCredentials_whenLogin_ThenReturnFalse() throws IllegalStateException, IOException {
	restTemplate = new TestRestTemplate();
	User user = new User();
	user.setUserName("user");
	user.setPassword("wrongpassword");
    ResponseEntity<String> response = restTemplate.postForEntity(base.toString()+"/login",user, String.class);

    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertTrue(response
      .getBody()
      .contains("false"));
}