Skip to content

Commit f53b49e

Browse files
committed
Add async test for serverless-web
1 parent f13feb1 commit f53b49e

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/main/java/org/springframework/cloud/function/serverless/web/ProxyMvc.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public final class ProxyMvc {
6565

6666
private static Log LOG = LogFactory.getLog(ProxyMvc.class);
6767

68-
static final String MVC_RESULT_ATTRIBUTE = ProxyMvc.class.getName().concat(".MVC_RESULT_ATTRIBUTE");
69-
7068
private final DispatcherServlet dispatcher;
7169

7270
private final ConfigurableWebApplicationContext applicationContext;
@@ -142,8 +140,6 @@ public void service(HttpServletRequest request, HttpServletResponse response) th
142140

143141

144142
public void service(HttpServletRequest request, HttpServletResponse response, CountDownLatch latch) throws Exception {
145-
((ProxyHttpServletRequest) request).setAsyncStarted(true);
146-
147143
ProxyFilterChain filterChain = new ProxyFilterChain(this.dispatcher);
148144
filterChain.doFilter(request, response);
149145

@@ -190,7 +186,6 @@ private static class ProxyFilterChain implements FilterChain {
190186
ProxyFilterChain(DispatcherServlet servlet) {
191187
List<Filter> filters = new ArrayList<>();
192188
servlet.getServletContext().getFilterRegistrations().values().forEach(fr -> filters.add(((ProxyFilterRegistration) fr).getFilter()));
193-
//servlet.getWebApplicationContext().getBeansOfType(Filter.class).values().forEach(f -> filters.add(f));
194189
Assert.notNull(filters, "filters cannot be null");
195190
Assert.noNullElements(filters, "filters cannot contain null values");
196191
this.filters = initFilterList(servlet, filters.toArray(new Filter[] {}));

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/serverless/web/RequestResponseTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,22 @@ public void validatePostWithBody() throws Exception {
134134
assertThat(pet.getName()).isNotEmpty();
135135
}
136136

137+
@Test
138+
public void validatePostAsyncWithBody() throws Exception {
139+
ProxyHttpServletRequest request = new ProxyHttpServletRequest(null, "POST", "/petsAsync/");
140+
String jsonPet = "{\n"
141+
+ " \"id\":\"1234\",\n"
142+
+ " \"breed\":\"Canish\",\n"
143+
+ " \"name\":\"Foo\",\n"
144+
+ " \"date\":\"2012-04-23T18:25:43.511Z\"\n"
145+
+ "}";
146+
request.setContent(jsonPet.getBytes());
147+
request.setContentType("application/json");
148+
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
149+
mvc.service(request, response);
150+
Pet pet = mapper.readValue(response.getContentAsByteArray(), Pet.class);
151+
assertThat(pet).isNotNull();
152+
assertThat(pet.getName()).isNotEmpty();
153+
}
154+
137155
}

spring-cloud-function-adapters/spring-cloud-function-serverless-web/src/test/java/org/springframework/cloud/function/test/app/PetsController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,27 @@
2828
import org.springframework.web.bind.annotation.RequestParam;
2929
import org.springframework.web.bind.annotation.ResponseStatus;
3030
import org.springframework.web.bind.annotation.RestController;
31+
import org.springframework.web.context.request.async.DeferredResult;
3132
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
3233

3334

3435
@RestController
3536
@EnableWebMvc
3637
public class PetsController {
38+
39+
@RequestMapping(path = "/petsAsync/", method = RequestMethod.POST)
40+
public DeferredResult<Pet> createPetAsync(@RequestBody Pet newPet) {
41+
if (newPet.getName() == null || newPet.getBreed() == null) {
42+
return null;
43+
}
44+
45+
Pet dbPet = newPet;
46+
dbPet.setId(UUID.randomUUID().toString());
47+
DeferredResult<Pet> result = new DeferredResult<Pet>();
48+
result.setResult(dbPet);
49+
return result;
50+
}
51+
3752
@RequestMapping(path = "/pets/", method = RequestMethod.POST)
3853
public Pet createPet(@RequestBody Pet newPet) {
3954
if (newPet.getName() == null || newPet.getBreed() == null) {

0 commit comments

Comments
 (0)