下面列出了org.springframework.boot.test.web.client.TestRestTemplate#postForEntity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
@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();
}
}
@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());
}
@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);
}
@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");
}
@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");
}
@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);
}
@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");
}
@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");
}
@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);
}
@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"));
}
@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"));
}