### Question:
TomcatFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; String uri = req.getRequestURI(); String decodedUri = URLDecoder.decode(uri, "UTF-8"); final boolean isRequestEncoded = !uri.equals(decodedUri); Message message = messageService.createMessage("org.zowe.apiml.gateway.requestContainEncodedSlash", uri); if (!allowEncodedSlashes && isRequestEncoded) { res.setStatus(HttpStatus.BAD_REQUEST.value()); res.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); try { mapper.writeValue(res.getWriter(), message.mapToView()); } catch (IOException e) { apimlLog.log("org.zowe.apiml.security.errorWrittingResponse", e.getMessage()); throw new ServletException("Error writing response", e); } } else { chain.doFilter(request, response); } }  @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }### Answer:
@Test public void shouldRejectEncodedSlashRequestsWhenConfiguredToReject() throws IOException, ServletException { filter = new TomcatFilter(messageService, objectMapper); ReflectionTestUtils.setField(filter, ALLOW_ENCODED_SLASHES_FIELD, false); request.setRequestURI(ENCODED_REQUEST_URI); filter.doFilter(request, response, filterChain); assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatus()); assertEquals(MediaType.APPLICATION_JSON_UTF8_VALUE, response.getContentType()); Message message = messageService.createMessage("org.zowe.apiml.gateway.requestContainEncodedSlash", request.getRequestURI()); verify(objectMapper).writeValue(response.getWriter(), message.mapToView()); }
@Test public void shouldAllowNonEncodedSlashRequestsAndMoveToNextFilterWhenConfiguredToReject() throws IOException, ServletException { filter = new TomcatFilter(messageService, objectMapper); ReflectionTestUtils.setField(filter, ALLOW_ENCODED_SLASHES_FIELD, false); request.setRequestURI(NORMAL_REQUEST_URI); filter.doFilter(request, response, filterChain); assertEquals(HttpStatus.OK.value(), response.getStatus()); verify(filterChain).doFilter(request, response); }
@Test public void shouldAllowAnyRequestAndMoveToNextFilterWhenConfiguredToAllow() throws IOException, ServletException { filter = new TomcatFilter(messageService, objectMapper); ReflectionTestUtils.setField(filter, ALLOW_ENCODED_SLASHES_FIELD, true); request.setRequestURI(NORMAL_REQUEST_URI); filter.doFilter(request, response, filterChain); assertEquals(HttpStatus.OK.value(), response.getStatus()); verify(filterChain).doFilter(request, response); request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); request.setRequestURI(ENCODED_REQUEST_URI); filter.doFilter(request, response, filterChain); assertEquals(HttpStatus.OK.value(), response.getStatus()); verify(filterChain).doFilter(request, response); }
@Test(expected = ServletException.class) public void shouldThrowServletExceptionOnIOExceptionWhenWritingResponse() throws IOException, ServletException { HttpServletRequest mockedRequest = mock(HttpServletRequest.class); HttpServletResponse mockedResponse = mock(HttpServletResponse.class); filter = new TomcatFilter(messageService, objectMapper); ReflectionTestUtils.setField(filter, ALLOW_ENCODED_SLASHES_FIELD, false); when(mockedRequest.getRequestURI()).thenReturn(ENCODED_REQUEST_URI); when(mockedResponse.getWriter()).thenThrow(new IOException()); filter.doFilter(mockedRequest, mockedResponse, filterChain); }