Add async test for serverless-web
This commit is contained in:
@@ -134,4 +134,22 @@ public class RequestResponseTests {
|
||||
assertThat(pet.getName()).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validatePostAsyncWithBody() throws Exception {
|
||||
ProxyHttpServletRequest request = new ProxyHttpServletRequest(null, "POST", "/petsAsync/");
|
||||
String jsonPet = "{\n"
|
||||
+ " \"id\":\"1234\",\n"
|
||||
+ " \"breed\":\"Canish\",\n"
|
||||
+ " \"name\":\"Foo\",\n"
|
||||
+ " \"date\":\"2012-04-23T18:25:43.511Z\"\n"
|
||||
+ "}";
|
||||
request.setContent(jsonPet.getBytes());
|
||||
request.setContentType("application/json");
|
||||
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
|
||||
mvc.service(request, response);
|
||||
Pet pet = mapper.readValue(response.getContentAsByteArray(), Pet.class);
|
||||
assertThat(pet).isNotNull();
|
||||
assertThat(pet.getName()).isNotEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,12 +28,27 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.request.async.DeferredResult;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
|
||||
@RestController
|
||||
@EnableWebMvc
|
||||
public class PetsController {
|
||||
|
||||
@RequestMapping(path = "/petsAsync/", method = RequestMethod.POST)
|
||||
public DeferredResult<Pet> createPetAsync(@RequestBody Pet newPet) {
|
||||
if (newPet.getName() == null || newPet.getBreed() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Pet dbPet = newPet;
|
||||
dbPet.setId(UUID.randomUUID().toString());
|
||||
DeferredResult<Pet> result = new DeferredResult<Pet>();
|
||||
result.setResult(dbPet);
|
||||
return result;
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/pets/", method = RequestMethod.POST)
|
||||
public Pet createPet(@RequestBody Pet newPet) {
|
||||
if (newPet.getName() == null || newPet.getBreed() == null) {
|
||||
|
||||
Reference in New Issue
Block a user