File tree 3 files changed +51
-16
lines changed
main/java/org/springframework/web/servlet/function
test/java/org/springframework/web/servlet/function
3 files changed +51
-16
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -570,6 +570,15 @@ interface SseBuilder {
570
570
*/
571
571
void send (Object object ) throws IOException ;
572
572
573
+ /**
574
+ * Sends the buffered content as a server-sent event, without data.
575
+ * Only the {@link #event(String) events} and {@link #comment(String) comments}
576
+ * will be sent.
577
+ * @throws IOException in case of I/O errors
578
+ * @since 6.1.4
579
+ */
580
+ void send () throws IOException ;
581
+
573
582
/**
574
583
* Add an SSE "id" line.
575
584
* @param id the event identifier
Original file line number Diff line number Diff line change @@ -135,6 +135,23 @@ public void send(Object object) throws IOException {
135
135
data (object );
136
136
}
137
137
138
+ @ Override
139
+ public void send () throws IOException {
140
+ this .builder .append ('\n' );
141
+ try {
142
+ OutputStream body = this .outputMessage .getBody ();
143
+ body .write (builderBytes ());
144
+ body .flush ();
145
+ }
146
+ catch (IOException ex ) {
147
+ this .sendFailed = true ;
148
+ throw ex ;
149
+ }
150
+ finally {
151
+ this .builder .setLength (0 );
152
+ }
153
+ }
154
+
138
155
@ Override
139
156
public SseBuilder id (String id ) {
140
157
Assert .hasLength (id , "Id must not be empty" );
@@ -186,20 +203,7 @@ private void writeString(String string) throws IOException {
186
203
for (String line : lines ) {
187
204
field ("data" , line );
188
205
}
189
- this .builder .append ('\n' );
190
-
191
- try {
192
- OutputStream body = this .outputMessage .getBody ();
193
- body .write (builderBytes ());
194
- body .flush ();
195
- }
196
- catch (IOException ex ) {
197
- this .sendFailed = true ;
198
- throw ex ;
199
- }
200
- finally {
201
- this .builder .setLength (0 );
202
- }
206
+ this .send ();
203
207
}
204
208
205
209
@ SuppressWarnings ("unchecked" )
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
32
32
import static org .assertj .core .api .Assertions .assertThat ;
33
33
34
34
/**
35
+ * Tests for {@link ServerResponse.SseBuilder}.
35
36
* @author Arjen Poutsma
36
37
* @author Sebastien Deleuze
38
+ * @author Brian Clozel
37
39
*/
38
40
class SseServerResponseTests {
39
41
@@ -151,6 +153,26 @@ void builder() throws Exception {
151
153
assertThat (this .mockResponse .getContentAsString ()).isEqualTo (expected );
152
154
}
153
155
156
+ @ Test
157
+ void sendWithoutData () throws Exception {
158
+ ServerResponse response = ServerResponse .sse (sse -> {
159
+ try {
160
+ sse .event ("custom" ).send ();
161
+ }
162
+ catch (IOException ex ) {
163
+ throw new UncheckedIOException (ex );
164
+ }
165
+ });
166
+
167
+ ServerResponse .Context context = Collections ::emptyList ;
168
+
169
+ ModelAndView mav = response .writeTo (this .mockRequest , this .mockResponse , context );
170
+ assertThat (mav ).isNull ();
171
+
172
+ String expected = "event:custom\n \n " ;
173
+ assertThat (this .mockResponse .getContentAsString ()).isEqualTo (expected );
174
+ }
175
+
154
176
155
177
private static final class Person {
156
178
You can’t perform that action at this time.
0 commit comments