@Test public void testDoDeleteReturnsError() throws Exception { /* setup mock request to return URI */ when(request.getPathInfo()).thenReturn("?"); // send POST instead of GET so we should get an error when(request.getMethod()).thenReturn("DELETE"); FilterScriptManagerServlet servlet = new FilterScriptManagerServlet(); servlet.service(request, response); // a 405 because POST with those arguments is invalid verify(response).setStatus(405); // test that the usage docs were output verify(response).getWriter(); verify(responseWriter).write(getUsageDoc()); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPut(request, response); }
if (!isValidAction(request, response)) { return; handleListAction(request, response); } else if ("DOWNLOAD".equals(action)) { handleDownloadAction(request, response);
if (!isValidAction(request, response)) { return; handleUploadAction(request, response); } else if ("ACTIVATE".equals(action)) { handleActivateAction(request, response); } else if ("CANARY".equals(action)) { handleCanaryAction(request, response); } else if ("DEACTIVATE".equals(action)) { handledeActivateAction(request, response);
private void handleUploadAction(HttpServletRequest request, HttpServletResponse response) throws IOException { String filter = handlePostBody(request, response); if (filter != null) { FilterInfo filterInfo = null; try { filterInfo = FilterVerifier.getInstance().verifyFilter(filter); } catch (IllegalAccessException e) { logger.error(e.getMessage(), e); setUsageError(500, "ERROR: Unable to process uploaded data. " + e.getMessage(), response); } catch (InstantiationException e) { logger.error(e.getMessage(), e); setUsageError(500, "ERROR: Bad Filter. " + e.getMessage(), response); } filterInfo = scriptDAO.addFilter(filter, filterInfo.getFilterType(), filterInfo.getFilterName(), filterInfo.getFilterDisablePropertyName(), filterInfo.getFilterOrder()); if (filterInfo == null) { setUsageError(500, "ERROR: Unable to process uploaded data.", response); return; } response.sendRedirect(redirectPath.get()); } }
setUsageError(404, "ERROR: No scripts found for endpoint: " + filter_id, response); } else { List<Map<String, Object>> scriptsJson = new ArrayList<Map<String, Object>>(); for (FilterInfo script : scripts) { Map<String, Object> scriptJson = createEndpointScriptJSON(script); scriptsJson.add(scriptJson);
private String handlePostBody(HttpServletRequest request, HttpServletResponse response) throws IOException { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); org.apache.commons.fileupload.FileItemIterator it = null; try { it = upload.getItemIterator(request); while (it.hasNext()) { FileItemStream stream = it.next(); InputStream input = stream.openStream(); // NOTE: we are going to pull the entire stream into memory // this will NOT work if we have huge scripts, but we expect these to be measured in KBs, not MBs or larger byte[] uploadedBytes = getBytesFromInputStream(input); input.close(); if (uploadedBytes.length == 0) { setUsageError(400, "ERROR: Body contained no data.", response); return null; } return new String(uploadedBytes); } } catch (FileUploadException e) { throw new IOException(e.getMessage()); } return null; }
@Test public void testErrorWithUsageIfIncorrectArguments2() throws Exception { /* setup mock request to return URI */ when(request.getPathInfo()).thenReturn("?action=UNKNOWN"); when(request.getParameter("action")).thenReturn("UNKNOWN"); when(request.getMethod()).thenReturn("GET"); FilterScriptManagerServlet servlet = new FilterScriptManagerServlet(); servlet.service(request, response); // a 400 because the resource exists, but arguments are incorrect verify(response).setStatus(400); // test that the usage docs were output verify(response).getWriter(); verify(responseWriter).write("ERROR: Unknown action type.\n\n"); verify(responseWriter).write(getUsageDoc()); }
@Test public void testErrorWithUsageIfIncorrectMethod() throws Exception { /* setup mock request to return URI */ when(request.getPathInfo()).thenReturn("?action=LIST&filter=unknown:type"); when(request.getParameter("action")).thenReturn("LIST"); // send POST instead of GET so we should get an error when(request.getMethod()).thenReturn("POST"); FilterScriptManagerServlet servlet = new FilterScriptManagerServlet(); servlet.service(request, response); // a 405 because POST with those arguments is invalid verify(response).setStatus(405); // test that the usage docs were output verify(response).getWriter(); verify(responseWriter).write("ERROR: Invalid HTTP method for action type.\n\n"); verify(responseWriter).write(getUsageDoc()); }
@Test public void testErrorWithUsageIfIncorrectArguments1() throws Exception { /* setup mock request to return URI */ when(request.getPathInfo()).thenReturn("?"); when(request.getMethod()).thenReturn("GET"); FilterScriptManagerServlet servlet = new FilterScriptManagerServlet(); servlet.service(request, response); System.out.println("--------------------------------------------------------"); System.out.println(getUsageDoc()); System.out.println("--------------------------------------------------------"); // a 400 because the resource exists, but arguments are incorrect verify(response).setStatus(400); // test that the usage docs were output verify(response).getWriter(); verify(responseWriter).write("ERROR: Invalid arguments.\n\n"); verify(responseWriter).write(getUsageDoc()); }
private FilterScriptManagerServlet getEndpointScriptManagerImplementation(ZuulFilterDAO dao) { return new FilterScriptManagerServlet(dao, new DynamicBooleanProperty("test.admin.enabled", true)); }