ErrorResponses { static Map<String, Object> buildHttpRequestContext(HttpServletRequest req) { Map<String, Object> result = new TreeMap<>(); StringBuilder uriBuilder = new StringBuilder(req.getServletPath()); if (req.getPathInfo() != null) { uriBuilder.append(req.getPathInfo()); } if (req.getQueryString() != null) { uriBuilder.append('?').append(req.getQueryString()); } result.put("relativeURI", uriBuilder.toString()); result.put("method", req.getMethod()); Enumeration<String> headerIt = req.getHeaderNames(); while (headerIt.hasMoreElements()) { String header = headerIt.nextElement(); result.put(header, req.getHeader(header)); } return result; } private  ErrorResponses(); static ErrorResponse noRequestBody(HttpServletRequest httpServletRequest); static ErrorResponse badRequest(HttpServletRequest httpServletRequest, String message); static ErrorResponse badRequest(HttpServletRequest httpServletRequest, String message, Exception cause); static ErrorResponse badRequest(HttpServletRequest httpServletRequest, Exception cause); static ErrorResponse internalServerError(HttpServletRequest httpServletRequest, Exception cause); static void sendError(HttpServletResponse httpServletResponse, ErrorResponse.ErrorResponseBuilder errorResponseBuilder); static void sendError(HttpServletResponse httpServletResponse, ErrorResponse errorResponse); static Map<String, Object> buildWebRequestContext(WebRequest webRequest);  }
@Test public void testBuildOfHttpRequest() { HttpServletRequest httpRequest = mock(HttpServletRequest.class); when(httpRequest.getServletPath()).thenReturn("/servletPath"); when(httpRequest.getPathInfo()).thenReturn("/pathInfo"); when(httpRequest.getHeaderNames()).thenReturn(enumerationOf("Content-Type")); Map<String, Object> mappedRequest = ErrorResponses.buildHttpRequestContext(httpRequest); assertThat(mappedRequest.get("relativeURI"), is(equalTo("/servletPath/pathInfo"))); }