### Question:
ApplicationGatewayFilter implements ContainerRequestFilter { @Override public void filter(final ContainerRequestContext requestContext) throws IOException { final String path = requestContext.getUriInfo().getRequestUri().getPath(); final boolean isStatusResource = path.matches("/status(/.*)?"); if (!isStatusResource) { final String applicationGatewayToken = applicationGatewayFilterConfiguration.applicationGatewayToken(); if (!applicationGatewayToken.isEmpty()) { final String receivedApplicationGatewayToken = requestContext .getHeaderString(APPLICATION_GATEWAY_TOKEN_HEADER); final boolean hasApplicationGatewayTokenMatch = applicationGatewayToken .equals(receivedApplicationGatewayToken); if (!hasApplicationGatewayTokenMatch) { requestContext.abortWith(Response.status(Status.UNAUTHORIZED).build()); } } } } @Inject  ApplicationGatewayFilter(final ApplicationGatewayFilterConfiguration applicationGatewayFilterConfiguration); @Override void filter(final ContainerRequestContext requestContext);  }### Answer:
@Test public void shouldAllowRequest_withGatewayTokenHeader() throws Exception { final String applicationToken = Randoms.randomString(); when(mockApplicationGatewayFilterConfiguration.applicationGatewayToken()).thenReturn(applicationToken); setUpRequestPath("http: when(mockContainerRequestContext.getHeaderString(APPLICATION_GATEWAY_TOKEN_HEADER)) .thenReturn(applicationToken); applicationGatewayFilter.filter(mockContainerRequestContext); verify(mockContainerRequestContext, never()).abortWith(any(Response.class)); }
@Test public void shouldAbortRequest_withoutGatewayTokenHeader() throws Exception { final String applicationToken = Randoms.randomString(); when(mockApplicationGatewayFilterConfiguration.applicationGatewayToken()).thenReturn(applicationToken); setUpRequestPath("http: applicationGatewayFilter.filter(mockContainerRequestContext); verify(mockContainerRequestContext).abortWith(any(Response.class)); }
@Test public void shouldAllowAnyRequest_whenConfiguredGatewayTokenIsEmpty() throws Exception { when(mockApplicationGatewayFilterConfiguration.applicationGatewayToken()).thenReturn(""); setUpRequestPath("http: applicationGatewayFilter.filter(mockContainerRequestContext); verify(mockContainerRequestContext, never()).abortWith(any(Response.class)); }
@Test public void shouldAllowStatusRequest_withoutGatewayTokenHeader() throws Exception { final String applicationToken = Randoms.randomString(); when(mockApplicationGatewayFilterConfiguration.applicationGatewayToken()).thenReturn(applicationToken); setUpRequestPath("http: applicationGatewayFilter.filter(mockContainerRequestContext); verify(mockContainerRequestContext, never()).abortWith(any(Response.class)); }
@Test public void shouldAllowStatusSubresourceRequest_withoutGatewayTokenHeader() throws Exception { final String applicationToken = Randoms.randomString(); when(mockApplicationGatewayFilterConfiguration.applicationGatewayToken()).thenReturn(applicationToken); setUpRequestPath("http: applicationGatewayFilter.filter(mockContainerRequestContext); verify(mockContainerRequestContext, never()).abortWith(any(Response.class)); }