ApplicationPathFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) throws IOException { String applicationPath = getApplicationPath(); if (applicationPath != null) { UriInfo requestUriInfo = requestContext.getUriInfo(); UriBuilder baseUriBuilder = requestUriInfo.getBaseUriBuilder(); baseUriBuilder.path(applicationPath); baseUriBuilder.path("/"); URI updatedBaseUri = baseUriBuilder.build(); URI requestUri = requestUriInfo.getRequestUri(); requestContext.setRequestUri(updatedBaseUri, requestUri); } }  @Override void filter(ContainerRequestContext requestContext); static final int PRIORITY; }
@Test public void filter_NoAppPathGiven_ShouldAccessRequestContext() throws IOException { createApplicationPathFilter(new ApplicationWithoutPath()).filter(null); }
@Test public void filter_AppPathGiven_ShouldAccessRequestContext() throws IOException { assertThrows(NullPointerException.class, () -> createApplicationPathFilter(new ApplicationPathTest()).filter(null)); }
@Test public void filter_EmptyAppPathGiven_ShouldNotUpdateBaseUri() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(new ApplicationPathEmpty()).filter(reqContext); verify(reqContext, never()).setRequestUri(any(), any()); verify(reqContext, never()).setRequestUri(any()); }
@Test public void filter_NoAppPathGiven_ShouldNotUpdateBaseUri() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(new ApplicationWithoutPath()).filter(reqContext); verify(reqContext, never()).setRequestUri(any(), any()); verify(reqContext, never()).setRequestUri(any()); }
@Test public void filter_NoAppPathRootGiven_ShouldNotUpdateBaseUri() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(new ApplicationPathRoot()).filter(reqContext); verify(reqContext, never()).setRequestUri(any(), any()); verify(reqContext, never()).setRequestUri(any()); }
@Test public void filter_NullAppConfigGiven_ShouldNotUpdateBaseUri() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(null).filter(reqContext); verify(reqContext, never()).setRequestUri(any(), any()); verify(reqContext, never()).setRequestUri(any()); }
@Test public void filter_AppPathGiven_ShouldUpdateBaseUri() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(new ApplicationPathTest()).filter(reqContext); verify(reqContext, times(1)).setRequestUri(TEST_UPDATED_BASE_URI, REQUEST_URI); verify(reqContext, never()).setRequestUri(any()); }
@Test public void filter_AppPathWithSlashGiven_ShouldUpdateBaseUriWithoutSlashes() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(new ApplicationPathTestWithLeadingAndTrailingSlashes()).filter(reqContext); verify(reqContext, times(1)).setRequestUri(TEST_UPDATED_BASE_URI, REQUEST_URI); verify(reqContext, never()).setRequestUri(any()); }
@Test public void filter_AppPathWithSlashAsteriskGiven_ShouldUpdateBaseUriWithoutSlashesAsterisk() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(new ApplicationPathTestSlashAsterisk()).filter(reqContext); verify(reqContext, times(1)).setRequestUri(TEST_UPDATED_BASE_URI, REQUEST_URI); verify(reqContext, never()).setRequestUri(any()); }
@Test public void filter_AppPathWithMultiplePathsGiven_ShouldUpdateBaseUriWithAllPaths() throws IOException { ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI); createApplicationPathFilter(new ApplicationPath1AndPath2()).filter(reqContext); verify(reqContext, times(1)).setRequestUri(URI.create("existing-base-uri/path1/path2/"), REQUEST_URI); verify(reqContext, never()).setRequestUri(any()); }