|
| 1 | +package com.baeldung.netty; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.nio.charset.Charset; |
| 7 | + |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 12 | +import io.netty.handler.codec.http.DefaultFullHttpRequest; |
| 13 | +import io.netty.handler.codec.http.FullHttpRequest; |
| 14 | +import io.netty.handler.codec.http.FullHttpResponse; |
| 15 | +import io.netty.handler.codec.http.HttpMethod; |
| 16 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 17 | +import io.netty.handler.codec.http.HttpVersion; |
| 18 | + |
| 19 | +public class EmbeddedChannelUnitTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void givenTwoChannelHandlers_testPipeline() { |
| 23 | + |
| 24 | + final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5"); |
| 25 | + httpRequest.headers().add("Operator", "Add"); |
| 26 | + |
| 27 | + EmbeddedChannel channel = new EmbeddedChannel( |
| 28 | + new HttpMessageHandler(), new CalculatorOperationHandler()); |
| 29 | + |
| 30 | + channel.pipeline() |
| 31 | + .addFirst(new HttpMessageHandler()) |
| 32 | + .addLast(new CalculatorOperationHandler()); |
| 33 | + |
| 34 | + |
| 35 | + // send HTTP request to server and check that the message is on the inbound pipeline |
| 36 | + assertTrue(channel.writeInbound(httpRequest)); |
| 37 | + |
| 38 | + long inboundChannelResponse = channel.readInbound(); |
| 39 | + assertEquals(15, inboundChannelResponse); |
| 40 | + |
| 41 | + // we should have an outbound message in the form of a HTTP response |
| 42 | + assertEquals(1, channel.outboundMessages().size()); |
| 43 | + // Object response = channel.readOutbound(); |
| 44 | + |
| 45 | + FullHttpResponse httpResponse = channel.readOutbound(); |
| 46 | + String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset()); |
| 47 | + assertTrue("15".equalsIgnoreCase(httpResponseContent)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenTwoChannelHandlers_testExceptionHandlingInHttpMessageHandler() { |
| 52 | + |
| 53 | + EmbeddedChannel channel = new EmbeddedChannel( |
| 54 | + new HttpMessageHandler(), new CalculatorOperationHandler()); |
| 55 | + |
| 56 | + final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/calculate?a=10&b=5"); |
| 57 | + wrongHttpRequest.headers().add("Operator", "Add"); |
| 58 | + |
| 59 | + try { |
| 60 | + // send invalid HTTP request to server and expect and error |
| 61 | + channel.pipeline().fireChannelRead(wrongHttpRequest); |
| 62 | + channel.checkException(); |
| 63 | + // channel.writeInbound(wrongHttpRequest); |
| 64 | + Assert.fail(); |
| 65 | + |
| 66 | + } catch (Exception ex) { |
| 67 | + |
| 68 | + // the HttpMessageHandler does not handle the exception and throws it down the pipeline |
| 69 | + assertTrue(ex instanceof UnsupportedOperationException); |
| 70 | + assertTrue(ex.getMessage().equalsIgnoreCase("HTTP method not supported")); |
| 71 | + |
| 72 | + FullHttpResponse errorHttpResponse = channel.readOutbound(); |
| 73 | + String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset()); |
| 74 | + assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent)); |
| 75 | + assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Test |
| 81 | + public void givenTwoChannelHandlers_testExceptionHandlingInCalculatorOperationHandler() { |
| 82 | + EmbeddedChannel channel = new EmbeddedChannel( |
| 83 | + new HttpMessageHandler(), new CalculatorOperationHandler()); |
| 84 | + |
| 85 | + final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5"); |
| 86 | + wrongHttpRequest.headers().add("Operator", "Invalid_operation"); |
| 87 | + |
| 88 | + try { |
| 89 | + // send invalid HTTP request to server and expect and error |
| 90 | + channel.writeInbound(wrongHttpRequest); |
| 91 | + Assert.fail(); |
| 92 | + |
| 93 | + } catch (Exception ex) { |
| 94 | + |
| 95 | + // the HttpMessageHandler does not handle the exception and throws it down the pipeline |
| 96 | + assertTrue(ex instanceof IllegalArgumentException); |
| 97 | + assertTrue(ex.getMessage().equalsIgnoreCase("Operation not defined")); |
| 98 | + |
| 99 | + // the outbound message is a HTTP response with the status code 500 |
| 100 | + FullHttpResponse errorHttpResponse = channel.readOutbound(); |
| 101 | + String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset()); |
| 102 | + assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent)); |
| 103 | + assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments