UriBuilderRequestFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext ctx) { UriBuilder baseBuilder = ctx.getUriInfo().getBaseUriBuilder(); UriBuilder requestBuilder = ctx.getUriInfo().getRequestUriBuilder(); processProtocolHeader(ctx, baseBuilder, requestBuilder); processHostHeader(ctx, baseBuilder, requestBuilder); processPortHeader(ctx, baseBuilder, requestBuilder); }  @Override void filter(ContainerRequestContext ctx);  }
@Test public void noForwardedHeadersCausesNoBuilderInvocations() { givenHeaders(); filter.filter(containerRequestContext); verifyUriBuildersKeptOriginalScheme(); verifyUriBuildersKeptOriginalHost(); verifyUriBuildersKeptOriginalPort(); }
@Test public void protoHeaderCausesUriBuildersSchemeSet() { givenHeaders( "X-Forwarded-Proto", "https" ); filter.filter(containerRequestContext); verifyUriBuildersChangedSchemeTo("https"); verifyUriBuildersKeptOriginalHost(); verifyUriBuildersKeptOriginalPort(); }
@Test public void hostHeaderWithoutPortCausesUriBuildersHostSetAndPortReset() { givenHeaders( "X-Forwarded-Host", "gravitee.io" ); filter.filter(containerRequestContext); verifyUriBuildersKeptOriginalScheme(); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(PROTOCOL_DEFAULT_PORT); }
@Test public void portHeaderCausesUriBuildersPortSet() { givenHeaders( "X-Forwarded-Port", "1234" ); filter.filter(containerRequestContext); verifyUriBuildersKeptOriginalScheme(); verifyUriBuildersKeptOriginalHost(); verifyUriBuildersChangedPortTo(1234); }
@Test public void hostHeaderWithPortCausesUriBuildersHostSetAndPortSet() { givenHeaders( "X-Forwarded-Host", "gravitee.io:4321" ); filter.filter(containerRequestContext); verifyUriBuildersKeptOriginalScheme(); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(4321); }
@Test public void hostHeaderWithoutPortAndPortHeaderCauseUriBuildersHostSetAndPortSet() { givenHeaders( "X-Forwarded-Host", "gravitee.io", "X-Forwarded-Port", "1234" ); filter.filter(containerRequestContext); verifyUriBuildersKeptOriginalScheme(); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(1234); }
@Test public void hostHeaderWithPortAndPortHeaderCauseUriBuildersHostSetAndPortSetFromPortHeader() { givenHeaders( "X-Forwarded-Host", "gravitee.io:4321", "X-Forwarded-Port", "1234" ); filter.filter(containerRequestContext); verifyUriBuildersKeptOriginalScheme(); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(1234); }
@Test public void protoHeaderAndHostHeaderWithoutPortCauseUriBuildersSchemeSetHostSetAndPortReset() { givenHeaders( "X-Forwarded-Proto", "https", "X-Forwarded-Host", "gravitee.io" ); filter.filter(containerRequestContext); verifyUriBuildersChangedSchemeTo("https"); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(PROTOCOL_DEFAULT_PORT); }
@Test public void protoHeaderAndHostHeaderWithPortCauseUriBuildersSchemeSetHostSetAndPortSet() { givenHeaders( "X-Forwarded-Proto", "https", "X-Forwarded-Host", "gravitee.io:4321" ); filter.filter(containerRequestContext); verifyUriBuildersChangedSchemeTo("https"); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(4321); }
@Test public void protoHeaderAndPortHeaderCauseUriBuildersSchemeSetAndPortSet() { givenHeaders( "X-Forwarded-Proto", "https", "X-Forwarded-Port", "1234" ); filter.filter(containerRequestContext); verifyUriBuildersChangedSchemeTo("https"); verifyUriBuildersKeptOriginalHost(); verifyUriBuildersChangedPortTo(1234); }
@Test public void protoHeaderHostHeaderWithoutPortAndPortHeaderCauseUriBuildersSchemeSetHostSetAndPortSet() { givenHeaders( "X-Forwarded-Proto", "https", "X-Forwarded-Host", "gravitee.io", "X-Forwarded-Port", "1234" ); filter.filter(containerRequestContext); verifyUriBuildersChangedSchemeTo("https"); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(1234); }
@Test public void protoHeaderHostHeaderWithPortAndPortHeaderCauseUriBuildersSchemeSetHostSetAndPortSet() { givenHeaders( "X-Forwarded-Proto", "https", "X-Forwarded-Host", "gravitee.io:4321", "X-Forwarded-Port", "1234" ); filter.filter(containerRequestContext); verifyUriBuildersChangedSchemeTo("https"); verifyUriBuildersChangedHostTo("gravitee.io"); verifyUriBuildersChangedPortTo(1234); }