mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Support conditional streaming with ResponseEntity<?>
Closes gh-35153
This commit is contained in:
+32
@@ -51,6 +51,7 @@ import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
|
||||
@@ -205,6 +206,21 @@ class RequestMappingHandlerAdapterTests {
|
||||
assertMethodProcessorCount(RESOLVER_COUNT, INIT_BINDER_RESOLVER_COUNT, 1);
|
||||
}
|
||||
|
||||
@Test // gh-35153
|
||||
void responseEntityWithWildCardAndConditionalStream() throws Exception {
|
||||
HandlerMethod handlerMethod = handlerMethod(new SseController(), "handle", String.class);
|
||||
this.handlerAdapter.afterPropertiesSet();
|
||||
|
||||
this.request.setAsyncSupported(true);
|
||||
this.request.addParameter("q", "sse");
|
||||
|
||||
this.handlerAdapter.handle(this.request, this.response, handlerMethod);
|
||||
|
||||
assertThat(this.response.getStatus()).isEqualTo(200);
|
||||
assertThat(this.response.getHeader("Content-Type")).isEqualTo("text/event-stream");
|
||||
assertThat(this.response.getContentAsString()).isEqualTo("data:event 1\n\ndata:event 2\n\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modelAttributeAdvice() throws Exception {
|
||||
this.webAppContext.registerSingleton("maa", ModelAttributeAdvice.class);
|
||||
@@ -377,6 +393,22 @@ class RequestMappingHandlerAdapterTests {
|
||||
}
|
||||
|
||||
|
||||
private static class SseController {
|
||||
|
||||
public ResponseEntity<?> handle(@RequestParam String q) throws IOException {
|
||||
if (q.equals("sse")) {
|
||||
SseEmitter emitter = new SseEmitter();
|
||||
emitter.send("event 1");
|
||||
emitter.send("event 2");
|
||||
emitter.complete();
|
||||
return ResponseEntity.ok().body(emitter);
|
||||
}
|
||||
return ResponseEntity.ok("text");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ControllerAdvice
|
||||
private static class ModelAttributeAdvice {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user