17
17
package org .springframework .cloud .gateway .tests .grpc ;
18
18
19
19
import java .security .cert .X509Certificate ;
20
+ import java .util .Iterator ;
20
21
21
22
import javax .net .ssl .SSLException ;
22
23
import javax .net .ssl .TrustManager ;
@@ -72,6 +73,18 @@ public void gRPCUnaryCallShouldReturnResponse() throws SSLException {
72
73
Assertions .assertThat (response .getGreeting ()).isEqualTo ("Hello, Sir FromClient" );
73
74
}
74
75
76
+ @ Test
77
+ public void gRPCStreamingCallShouldReturnResponse () throws SSLException {
78
+ ManagedChannel channel = createSecuredChannel (gatewayPort );
79
+
80
+ final Iterator <HelloResponse > response = StreamServiceGrpc .newBlockingStub (channel )
81
+ .more (HelloRequest .newBuilder ().setFirstName ("Sir" ).setLastName ("FromClient" ).build ());
82
+
83
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(0) ==> Sir" );
84
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(1) ==> Sir" );
85
+ Assertions .assertThat (response .next ().getGreeting ()).isEqualTo ("Hello(2) ==> Sir" );
86
+ }
87
+
75
88
private ManagedChannel createSecuredChannel (int port ) throws SSLException {
76
89
TrustManager [] trustAllCerts = createTrustAllTrustManager ();
77
90
@@ -97,13 +110,38 @@ public void gRPCUnaryCallShouldHandleRuntimeException() throws SSLException {
97
110
}
98
111
99
112
@ Test
100
- public void gRPCUnaryCallShouldHandleRuntimeException2 () throws SSLException {
113
+ public void gRPCUnaryCallShouldHandleRuntimeExceptionAfterData () throws SSLException {
101
114
ManagedChannel channel = createSecuredChannel (gatewayPort );
102
115
boolean thrown = false ;
103
116
try {
104
117
HelloServiceGrpc .newBlockingStub (channel )
105
- .hello (HelloRequest .newBuilder ().setFirstName ("failWithRuntimeExceptionAfterData!" ).build ())
106
- .getGreeting ();
118
+ .hello (HelloRequest .newBuilder ().setFirstName ("failWithRuntimeExceptionAfterData!" ).build ())
119
+ .getGreeting ();
120
+ }
121
+ catch (StatusRuntimeException e ) {
122
+ thrown = true ;
123
+ Assertions .assertThat (e .getStatus ().getCode ()).isEqualTo (RESOURCE_EXHAUSTED .getCode ());
124
+ Assertions .assertThat (e .getStatus ().getDescription ()).isEqualTo ("Too long firstNames?" );
125
+ }
126
+ Assertions .assertThat (thrown ).withFailMessage ("Expected exception not thrown!" ).isTrue ();
127
+ }
128
+
129
+ @ Test
130
+ public void gRPCStreamingCallShouldHandleRuntimeExceptionAfterData () throws SSLException {
131
+ ManagedChannel channel = createSecuredChannel (gatewayPort );
132
+ boolean thrown = false ;
133
+ final Iterator <HelloResponse > response = StreamServiceGrpc .newBlockingStub (channel )
134
+ .more (HelloRequest .newBuilder ()
135
+ .setFirstName ("failWithRuntimeExceptionAfterData!" )
136
+ .setLastName ("FromClient" )
137
+ .build ());
138
+ Assertions .assertThat (response .next ().getGreeting ())
139
+ .isEqualTo ("Hello(0) ==> failWithRuntimeExceptionAfterData!" );
140
+ Assertions .assertThat (response .next ().getGreeting ())
141
+ .isEqualTo ("Hello(1) ==> failWithRuntimeExceptionAfterData!" );
142
+ try {
143
+ Assertions .assertThat (response .next ().getGreeting ())
144
+ .isEqualTo ("Hello(2) ==> failWithRuntimeExceptionAfterData!" );
107
145
}
108
146
catch (StatusRuntimeException e ) {
109
147
thrown = true ;
0 commit comments