HostInterceptor implements Interceptor { @Override public Event intercept(Event event) { Map<String, String> headers = event.getHeaders(); if (preserveExisting && headers.containsKey(header)) { return event; } if (host != null) { headers.put(header, host); } return event; } private  HostInterceptor(boolean preserveExisting,
      boolean useIP, String header); @Override void initialize(); @Override Event intercept(Event event); @Override List<Event> intercept(List<Event> events); @Override void close();  }
@Test public void testBasic() throws Exception { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.HOST.toString()); Interceptor interceptor = builder.build(); Event eventBeforeIntercept = EventBuilder.withBody("test event", Charsets.UTF_8); Assert.assertNull(eventBeforeIntercept.getHeaders().get(Constants.HOST)); Event eventAfterIntercept = interceptor.intercept(eventBeforeIntercept); String actualHost = eventAfterIntercept.getHeaders().get(Constants.HOST); Assert.assertNotNull(actualHost); }
@Test public void testCustomHeader() throws Exception { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.HOST.toString()); Context ctx = new Context(); ctx.put("preserveExisting", "false"); ctx.put("hostHeader", "hostname"); builder.configure(ctx); Interceptor interceptor = builder.build(); Event eventBeforeIntercept = EventBuilder.withBody("test event", Charsets.UTF_8); Assert.assertNull(eventBeforeIntercept.getHeaders().get("hostname")); Event eventAfterIntercept = interceptor.intercept(eventBeforeIntercept); String actualHost = eventAfterIntercept.getHeaders().get("hostname"); Assert.assertNotNull(actualHost); Assert.assertEquals(InetAddress.getLocalHost().getHostAddress(), actualHost); }
@Test public void testPreserve() throws Exception { Context ctx = new Context(); ctx.put("preserveExisting", "true"); Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.HOST.toString()); builder.configure(ctx); Interceptor interceptor = builder.build(); final String ORIGINAL_HOST = "originalhost"; Event eventBeforeIntercept = EventBuilder.withBody("test event", Charsets.UTF_8); eventBeforeIntercept.getHeaders().put(Constants.HOST, ORIGINAL_HOST); Assert.assertEquals(ORIGINAL_HOST, eventBeforeIntercept.getHeaders().get(Constants.HOST)); String expectedHost = ORIGINAL_HOST; Event eventAfterIntercept = interceptor.intercept(eventBeforeIntercept); String actualHost = eventAfterIntercept.getHeaders().get(Constants.HOST); Assert.assertNotNull(actualHost); Assert.assertEquals(expectedHost, actualHost); }
@Test public void testClobber() throws Exception { Context ctx = new Context(); ctx.put("preserveExisting", "false"); Interceptor.Builder builder = InterceptorBuilderFactory .newInstance(InterceptorType.HOST.toString()); builder.configure(ctx); Interceptor interceptor = builder.build(); final String ORIGINAL_HOST = "originalhost"; Event eventBeforeIntercept = EventBuilder.withBody("test event", Charsets.UTF_8); eventBeforeIntercept.getHeaders().put(Constants.HOST, ORIGINAL_HOST); Assert.assertEquals(ORIGINAL_HOST, eventBeforeIntercept.getHeaders() .get(Constants.HOST)); String expectedHost = InetAddress.getLocalHost().getHostAddress(); Event eventAfterIntercept = interceptor.intercept(eventBeforeIntercept); String actualHost = eventAfterIntercept.getHeaders().get(Constants.HOST); Assert.assertNotNull(actualHost); Assert.assertEquals(expectedHost, actualHost); }
@Test public void testUseIP() throws Exception { Context ctx = new Context(); ctx.put("useIP", "true"); Interceptor.Builder builder = InterceptorBuilderFactory .newInstance(InterceptorType.HOST.toString()); builder.configure(ctx); Interceptor interceptor = builder.build(); final String ORIGINAL_HOST = "originalhost"; Event eventBeforeIntercept = EventBuilder.withBody("test event", Charsets.UTF_8); eventBeforeIntercept.getHeaders().put(Constants.HOST, ORIGINAL_HOST); Assert.assertEquals(ORIGINAL_HOST, eventBeforeIntercept.getHeaders() .get(Constants.HOST)); String expectedHost = InetAddress.getLocalHost().getHostAddress(); Event eventAfterIntercept = interceptor.intercept(eventBeforeIntercept); String actualHost = eventAfterIntercept.getHeaders().get(Constants.HOST); Assert.assertNotNull(actualHost); Assert.assertEquals(expectedHost, actualHost); }
@Test public void testUseHostname() throws Exception { Context ctx = new Context(); ctx.put("useIP", "false"); Interceptor.Builder builder = InterceptorBuilderFactory .newInstance(InterceptorType.HOST.toString()); builder.configure(ctx); Interceptor interceptor = builder.build(); final String ORIGINAL_HOST = "originalhost"; Event eventBeforeIntercept = EventBuilder.withBody("test event", Charsets.UTF_8); eventBeforeIntercept.getHeaders().put(Constants.HOST, ORIGINAL_HOST); Assert.assertEquals(ORIGINAL_HOST, eventBeforeIntercept.getHeaders() .get(Constants.HOST)); String expectedHost = InetAddress.getLocalHost().getCanonicalHostName(); Event eventAfterIntercept = interceptor.intercept(eventBeforeIntercept); String actualHost = eventAfterIntercept.getHeaders().get(Constants.HOST); Assert.assertNotNull(actualHost); Assert.assertEquals(expectedHost, actualHost); }