### Question:
NettyToStyxRequestDecoder extends MessageToMessageDecoder<HttpObject> { @Override protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception { if (msg.getDecoderResult().isFailure()) { String formattedHttpObject = httpMessageFormatter.formatNettyMessage(msg); throw new BadRequestException("Error while decoding request: " + formattedHttpObject, httpMessageFormatter.wrap(msg.getDecoderResult().cause())); } try { if (msg instanceof HttpRequest) { ctx.channel().config().setAutoRead(false); ctx.channel().read(); HttpRequest nettyRequest = (HttpRequest) msg; this.producer = Optional.of(createProducer(ctx, nettyRequest.uri())); Publisher<Buffer> contentPublisher = new FlowControllingPublisher(queueDrainingExecutor, this.producer.get()); LiveHttpRequest styxRequest = toStyxRequest(nettyRequest, contentPublisher); out.add(styxRequest); } if (msg instanceof HttpContent) { assert this.producer.isPresent(); ByteBuf content = ((ByteBufHolder) msg).content(); if (content.isReadable()) { ByteBuf byteBuf = retain(content); queueDrainingExecutor.execute(() -> producer.get().newChunk(byteBuf)); } if (msg instanceof LastHttpContent) { queueDrainingExecutor.execute(() -> producer.get().lastHttpContent()); } } } catch (BadRequestException ex) { throw ex; } catch (Exception ex) { throw new BadRequestException(ex.getMessage() + " in " + msg, ex); } } private  NettyToStyxRequestDecoder(Builder builder); @Override void channelInactive(ChannelHandlerContext ctx); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause);  }### Answer:
@Test public void sanitisesHeadersInBadRequestException() throws Throwable { FullHttpRequest originalRequest = newHttpRequest("/uri"); HttpHeaders originalRequestHeaders = originalRequest.headers(); originalRequestHeaders.add("Foo", "foo"); originalRequestHeaders.add("secret-header", "secret"); originalRequestHeaders.add("Cookie", "Bar=bar;secret-cookie=secret"); originalRequest.setDecoderResult(DecoderResult.failure(new Exception("It just didn't work"))); Exception e = assertThrows(DecoderException.class, () -> decode(originalRequest)); assertEquals(BadRequestException.class, e.getCause().getClass()); String breMsg = e.getCause().getMessage(); assertThat(breMsg, containsString("Foo=foo")); assertThat(breMsg, containsString("secret-header=****")); assertThat(breMsg, containsString("Bar=bar")); assertThat(breMsg, containsString("secret-cookie=****")); assertThat(breMsg, not(containsString("secret-header=secret"))); assertThat(breMsg, not(containsString("secret-cookie=secret"))); }
@Test public void decodesNettyInternalRequestToStyxRequest() throws Exception { FullHttpRequest originalRequest = newHttpRequest("/uri"); HttpHeaders originalRequestHeaders = originalRequest.headers(); originalRequestHeaders.add("Foo", "Bar"); originalRequestHeaders.add("Bar", "Bar"); originalRequestHeaders.add("Host", "foo.com"); LiveHttpRequest styxRequest = decode(originalRequest); assertThat(styxRequest.id().toString(), is("1")); assertThat(styxRequest.url().encodedUri(), is(originalRequest.getUri())); assertThat(styxRequest.method(), is(HttpMethod.GET)); assertThatHttpHeadersAreSame(styxRequest.headers(), originalRequestHeaders); }
@Test public void removesExpectHeaderBeforePassingThroughTheRequest() throws Exception { FullHttpRequest originalRequest = newHttpRequest("/uri"); originalRequest.headers().set(EXPECT, CONTINUE); originalRequest.headers().set(HOST, "foo.com"); LiveHttpRequest styxRequest = decode(originalRequest); assertThat(styxRequest.header(EXPECT).isPresent(), is(false)); }
@Test public void overridesTheHostHeaderWithTheHostAndPortInTheAbsoluteURI() { HttpRequest request = newHttpRequest(URI.create("http: request.headers().set(HOST, "www.example.com:8000"); LiveHttpRequest styxRequest = decode(request); assertThat(styxRequest.headers().get(HOST).get(), is("example.net")); }