16
16
*/
17
17
package org .graylog2 .inputs .transports .netty ;
18
18
19
+ import io .netty .buffer .ByteBuf ;
20
+ import io .netty .channel .ChannelHandlerContext ;
21
+ import io .netty .channel .SimpleChannelInboundHandler ;
19
22
import io .netty .channel .embedded .EmbeddedChannel ;
20
23
import io .netty .handler .codec .http .DefaultFullHttpRequest ;
21
24
import io .netty .handler .codec .http .DefaultHttpRequest ;
@@ -235,7 +238,8 @@ private void testAuthentication(String expectedAuthHeader, String expectedAuthHe
235
238
236
239
httpRequest .content ().writeBytes (GELF_MESSAGE );
237
240
238
- channel = new EmbeddedChannel (new HttpHandler (true , expectedAuthHeader , expectedAuthHeaderValue , "/gelf" ));
241
+ final DownstreamHandler downstreamHandler = new DownstreamHandler ();
242
+ channel = new EmbeddedChannel (new HttpHandler (true , expectedAuthHeader , expectedAuthHeaderValue , "/gelf" ), downstreamHandler );
239
243
channel .writeInbound (httpRequest );
240
244
channel .finish ();
241
245
@@ -248,5 +252,25 @@ private void testAuthentication(String expectedAuthHeader, String expectedAuthHe
248
252
assertThat (headers .get (ACCESS_CONTROL_ALLOW_CREDENTIALS )).isEqualTo ("true" );
249
253
assertThat (headers .get (ACCESS_CONTROL_ALLOW_HEADERS )).isEqualTo ("Authorization, Content-Type" );
250
254
assertThat (headers .get (CONNECTION )).isEqualTo (HttpHeaderValues .CLOSE .toString ());
255
+ if (expectedStatus == HttpResponseStatus .ACCEPTED ) {
256
+ assertThat (downstreamHandler .received ).isTrue ();
257
+ }else if (expectedStatus == HttpResponseStatus .UNAUTHORIZED ) {
258
+ assertThat (downstreamHandler .received ).isFalse ();
259
+ } else {
260
+ throw new AssertionError ("Unexpected status: " + expectedStatus );
261
+ }
262
+ }
263
+
264
+ /**
265
+ * Downstream handler for confirming that authorization failures halt message flow, and that message flow continues
266
+ * for authentication successes.
267
+ */
268
+ private class DownstreamHandler extends SimpleChannelInboundHandler <ByteBuf > {
269
+ public boolean received = false ;
270
+
271
+ @ Override
272
+ protected void channelRead0 (ChannelHandlerContext channelHandlerContext , io .netty .buffer .ByteBuf httpRequest ) throws Exception {
273
+ this .received = true ;
274
+ }
251
275
}
252
276
}
0 commit comments