20
20
import java .nio .charset .StandardCharsets ;
21
21
import java .util .ArrayList ;
22
22
import java .util .List ;
23
+ import java .util .function .Consumer ;
24
+ import java .util .function .Supplier ;
23
25
24
26
import org .junit .jupiter .api .Test ;
25
27
import org .reactivestreams .Publisher ;
26
28
import reactor .core .publisher .Flux ;
27
29
import reactor .core .publisher .Mono ;
30
+ import reactor .test .StepVerifier ;
28
31
29
32
import org .springframework .core .io .buffer .DataBuffer ;
30
33
import org .springframework .core .io .buffer .DefaultDataBuffer ;
36
39
import static org .assertj .core .api .Assertions .assertThat ;
37
40
38
41
/**
42
+ * Unit tests for {@link AbstractServerHttpRequest}.
43
+ *
39
44
* @author Rossen Stoyanchev
40
45
* @author Sebastien Deleuze
41
46
* @author Brian Clozel
42
47
*/
43
48
public class ServerHttpResponseTests {
44
49
45
50
@ Test
46
- void writeWith () throws Exception {
51
+ void writeWith () {
47
52
TestServerHttpResponse response = new TestServerHttpResponse ();
48
53
response .writeWith (Flux .just (wrap ("a" ), wrap ("b" ), wrap ("c" ))).block ();
49
54
@@ -58,7 +63,7 @@ void writeWith() throws Exception {
58
63
}
59
64
60
65
@ Test // SPR-14952
61
- void writeAndFlushWithFluxOfDefaultDataBuffer () throws Exception {
66
+ void writeAndFlushWithFluxOfDefaultDataBuffer () {
62
67
TestServerHttpResponse response = new TestServerHttpResponse ();
63
68
Flux <Flux <DefaultDataBuffer >> flux = Flux .just (Flux .just (wrap ("foo" )));
64
69
response .writeAndFlushWith (flux ).block ();
@@ -72,18 +77,18 @@ void writeAndFlushWithFluxOfDefaultDataBuffer() throws Exception {
72
77
}
73
78
74
79
@ Test
75
- void writeWithFluxError () throws Exception {
80
+ void writeWithFluxError () {
76
81
IllegalStateException error = new IllegalStateException ("boo" );
77
82
writeWithError (Flux .error (error ));
78
83
}
79
84
80
85
@ Test
81
- void writeWithMonoError () throws Exception {
86
+ void writeWithMonoError () {
82
87
IllegalStateException error = new IllegalStateException ("boo" );
83
88
writeWithError (Mono .error (error ));
84
89
}
85
90
86
- void writeWithError (Publisher <DataBuffer > body ) throws Exception {
91
+ void writeWithError (Publisher <DataBuffer > body ) {
87
92
TestServerHttpResponse response = new TestServerHttpResponse ();
88
93
HttpHeaders headers = response .getHeaders ();
89
94
headers .setContentType (MediaType .APPLICATION_JSON );
@@ -100,7 +105,7 @@ void writeWithError(Publisher<DataBuffer> body) throws Exception {
100
105
}
101
106
102
107
@ Test
103
- void setComplete () throws Exception {
108
+ void setComplete () {
104
109
TestServerHttpResponse response = new TestServerHttpResponse ();
105
110
response .setComplete ().block ();
106
111
@@ -111,7 +116,7 @@ void setComplete() throws Exception {
111
116
}
112
117
113
118
@ Test
114
- void beforeCommitWithComplete () throws Exception {
119
+ void beforeCommitWithComplete () {
115
120
ResponseCookie cookie = ResponseCookie .from ("ID" , "123" ).build ();
116
121
TestServerHttpResponse response = new TestServerHttpResponse ();
117
122
response .beforeCommit (() -> Mono .fromRunnable (() -> response .getCookies ().add (cookie .getName (), cookie )));
@@ -129,7 +134,7 @@ void beforeCommitWithComplete() throws Exception {
129
134
}
130
135
131
136
@ Test
132
- void beforeCommitActionWithSetComplete () throws Exception {
137
+ void beforeCommitActionWithSetComplete () {
133
138
ResponseCookie cookie = ResponseCookie .from ("ID" , "123" ).build ();
134
139
TestServerHttpResponse response = new TestServerHttpResponse ();
135
140
response .beforeCommit (() -> {
@@ -145,6 +150,32 @@ void beforeCommitActionWithSetComplete() throws Exception {
145
150
assertThat (response .getCookies ().getFirst ("ID" )).isSameAs (cookie );
146
151
}
147
152
153
+ @ Test // gh-24186
154
+ void beforeCommitErrorShouldLeaveResponseNotCommitted () {
155
+
156
+ Consumer <Supplier <Mono <Void >>> tester = preCommitAction -> {
157
+ TestServerHttpResponse response = new TestServerHttpResponse ();
158
+ response .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
159
+ response .getHeaders ().setContentLength (3 );
160
+ response .beforeCommit (preCommitAction );
161
+
162
+ StepVerifier .create (response .writeWith (Flux .just (wrap ("body" ))))
163
+ .expectErrorMessage ("Max sessions" )
164
+ .verify ();
165
+
166
+ assertThat (response .statusCodeWritten ).isFalse ();
167
+ assertThat (response .headersWritten ).isFalse ();
168
+ assertThat (response .cookiesWritten ).isFalse ();
169
+ assertThat (response .isCommitted ()).isFalse ();
170
+ assertThat (response .getHeaders ()).isEmpty ();
171
+ };
172
+
173
+ tester .accept (() -> Mono .error (new IllegalStateException ("Max sessions" )));
174
+ tester .accept (() -> {
175
+ throw new IllegalStateException ("Max sessions" );
176
+ });
177
+ }
178
+
148
179
149
180
private DefaultDataBuffer wrap (String a ) {
150
181
return new DefaultDataBufferFactory ().wrap (ByteBuffer .wrap (a .getBytes (StandardCharsets .UTF_8 )));
0 commit comments