/** * Verify GET with escaped query parameters * * @throws Exception */ @Test public void getWithEscapedMappedQueryParams() throws Exception { Map<String, String> inputParams = new HashMap<String, String>(); inputParams.put("name", "us er"); inputParams.put("number", "100"); final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = get(url, inputParams, true); assertTrue(request.ok()); assertEquals("GET", method.get()); assertEquals("us er", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
/** * Verify PUT with query parameters * * @throws Exception */ @Test public void putWithVarargsQueryParams() throws Exception { final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = put(url, false, "name", "user", "number", "100"); assertTrue(request.ok()); assertEquals("PUT", method.get()); assertEquals("user", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
/** * Verify data is send when receiving response headers without first calling * {@link HttpRequest#code()} * * @throws Exception */ @Test public void sendHeadersWithoutCode() throws Exception { final AtomicReference<String> body = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { body.set(new String(read())); response.setHeader("h1", "v1"); response.setHeader("h2", "v2"); response.setStatus(HTTP_OK); } }; HttpRequest request = post(url).ignoreCloseExceptions(false); Map<String, List<String>> headers = request.send("hello").headers(); assertEquals("v1", headers.get("h1").get(0)); assertEquals("v2", headers.get("h2").get(0)); assertEquals("hello", body.get()); }
/** * Verify POST with query parameters * * @throws Exception */ @Test public void postWithMappedQueryParams() throws Exception { Map<String, String> inputParams = new HashMap<String, String>(); inputParams.put("name", "user"); inputParams.put("number", "100"); final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = post(url, inputParams, false); assertTrue(request.ok()); assertEquals("POST", method.get()); assertEquals("user", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
@Test void stats() throws IOException, ServletException { Request baseReq = mock(Request.class); HttpChannelState s = new HttpChannelState(null){}; when(baseReq.getHttpChannelState()).thenReturn(s); Response resp = mock(Response.class); when(baseReq.getResponse()).thenReturn(resp); when(resp.getContentCount()).thenReturn(772L); handler.handle("/testUrl", baseReq, new MockHttpServletRequest(), new MockHttpServletResponse()); assertThat(registry.mustFind("jetty.requests").functionTimer().count()).isEqualTo(1L); assertThat(registry.mustFind("jetty.responses.size").functionCounter().count()).isEqualTo(772.0); }
@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // invoke RpcResponse rpcResponse = doInvoke(request); // serialize response byte[] responseBytes = HessianSerializer.serialize(rpcResponse); response.setContentType("text/html;charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); baseRequest.setHandled(true); OutputStream out = response.getOutputStream(); out.write(responseBytes); out.flush(); }
/** * Verify GET with escaped query parameters * * @throws Exception */ @Test public void getWithEscapedVarargsQueryParams() throws Exception { final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = get(url, true, "name", "us er", "number", "100"); assertTrue(request.ok()); assertEquals("GET", method.get()); assertEquals("us er", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
/** * Get header parameter values * * @throws Exception */ @Test public void getParameters() throws Exception { handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); response.setHeader("a", "value;b=c;d=e"); } }; HttpRequest request = get(url); assertTrue(request.ok()); Map<String, String> params = request.parameters("a"); assertNotNull(params); assertEquals(2, params.size()); assertEquals("c", params.get("b")); assertEquals("e", params.get("d")); }
/** * Make a GET request with an empty body response * * @throws Exception */ @Test public void getUrlEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_OK); } }; HttpRequest request = get(new URL(url)); assertNotNull(request.getConnection()); int code = request.code(); assertTrue(request.ok()); assertFalse(request.created()); assertFalse(request.noContent()); assertFalse(request.badRequest()); assertFalse(request.serverError()); assertFalse(request.notFound()); assertEquals("GET", method.get()); assertEquals("OK", request.message()); assertEquals(HTTP_OK, code); assertEquals("", request.body()); }
/** * Verify PUT with escaped query parameters * * @throws Exception */ @Test public void putWithEscapedMappedQueryParams() throws Exception { Map<String, String> inputParams = new HashMap<String, String>(); inputParams.put("name", "us er"); inputParams.put("number", "100"); final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = put(url, inputParams, true); assertTrue(request.ok()); assertEquals("PUT", method.get()); assertEquals("us er", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
/** * Verify POST with query parameters * * @throws Exception */ @Test public void postWithVaragsQueryParams() throws Exception { final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = post(url, false, "name", "user", "number", "100"); assertTrue(request.ok()); assertEquals("POST", method.get()); assertEquals("user", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
/** * Make a GET request with a URL that needs encoding * * @throws Exception */ @Test public void getUrlEncodedWithUnicode() throws Exception { String unencoded = "/\u00DF"; final AtomicReference<String> path = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { path.set(request.getPathInfo()); response.setStatus(HTTP_OK); } }; HttpRequest request = get(encode(url + unencoded)); assertTrue(request.ok()); assertEquals(unencoded, path.get()); }
/** * Make a GET request with a URL that needs encoding * * @throws Exception */ @Test public void getUrlEncodedWithPercent() throws Exception { String unencoded = "/%"; final AtomicReference<String> path = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { path.set(request.getPathInfo()); response.setStatus(HTTP_OK); } }; HttpRequest request = get(encode(url + unencoded)); assertTrue(request.ok()); assertEquals(unencoded, path.get()); }
/** * Get header parameter value * * @throws Exception */ @Test public void getEmptyParameters() throws Exception { handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); response.setHeader("a", "b;"); } }; HttpRequest request = get(url); assertTrue(request.ok()); assertNull(request.parameter("a", "c")); assertTrue(request.parameters("a").isEmpty()); }
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String header = request.getHeader("Authorization"); if(header == null || !header.equals(authenticationHeader)) { response.addHeader("WWW-Authenticate", "Basic"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setContentType("text/plain; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("Login to access API"); baseRequest.setHandled(true); } }
/** * Make an OPTIONS request with an empty body response * * @throws Exception */ @Test public void optionsUrlEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_OK); } }; HttpRequest request = options(new URL(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); assertEquals("OPTIONS", method.get()); assertEquals("", request.body()); }
/** * Make a HEAD request with an empty body response * * @throws Exception */ @Test public void headEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_OK); } }; HttpRequest request = head(url); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); assertEquals("HEAD", method.get()); assertEquals("", request.body()); }
/** * Make a HEAD request with an empty body response * * @throws Exception */ @Test public void headUrlEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_OK); } }; HttpRequest request = head(new URL(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); assertEquals("HEAD", method.get()); assertEquals("", request.body()); }
/** * Make a PUT request with an empty body response * * @throws Exception */ @Test public void putUrlEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_OK); } }; HttpRequest request = put(new URL(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); assertEquals("PUT", method.get()); assertEquals("", request.body()); }
/** * Make a PUT request with an empty body response * * @throws Exception */ @Test public void traceEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_OK); } }; HttpRequest request = trace(url); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); assertEquals("TRACE", method.get()); assertEquals("", request.body()); }
/** * Make a TRACE request with an empty body response * * @throws Exception */ @Test public void traceUrlEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_OK); } }; HttpRequest request = trace(new URL(url)); assertNotNull(request.getConnection()); assertTrue(request.ok()); assertFalse(request.notFound()); assertEquals("TRACE", method.get()); assertEquals("", request.body()); }
/** * Make a POST request with an empty request body * * @throws Exception */ @Test public void postEmpty() throws Exception { final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); response.setStatus(HTTP_CREATED); } }; HttpRequest request = post(url); int code = request.code(); assertEquals("POST", method.get()); assertFalse(request.ok()); assertTrue(request.created()); assertEquals(HTTP_CREATED, code); }
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if(baseRequest.getMethod().equals("OPTIONS")) { response.setStatus(HttpServletResponse.SC_OK); response.setHeader("Access-Control-Allow-Origin", "*"); String methods = "POST, OPTIONS"; if(allowGet) methods += ", GET"; response.setHeader("Access-Control-Allow-Methods", methods); response.setHeader("Access-Control-Max-Age", "1000"); String req = request.getHeader("Access-Control-Request-Headers"); if(req != null) { response.setHeader("Access-Control-Allow-Headers", req); } baseRequest.setHandled(true); } }
/** * Make a POST request with a non-empty request body * * @throws Exception */ @Test public void postNonEmptyString() throws Exception { final AtomicReference<String> body = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { body.set(new String(read())); response.setStatus(HTTP_OK); } }; int code = post(url).send("hello").code(); assertEquals(HTTP_OK, code); assertEquals("hello", body.get()); }
/** * Get header parameter value * * @throws Exception */ @Test public void getMultipleParametersQuoted() throws Exception { handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); response.setHeader("a", "b;c=\"d\";e=\"f\""); } }; HttpRequest request = get(url); assertTrue(request.ok()); assertEquals("d", request.parameter("a", "c")); assertEquals("f", request.parameter("a", "e")); }
/** * Make a POST request with multiple files in the body * * @throws Exception */ @Test public void postMultipleFiles() throws Exception { final AtomicReference<String> body = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { body.set(new String(read())); response.setStatus(HTTP_OK); } }; File file1 = File.createTempFile("post", ".txt"); new FileWriter(file1).append("hello").close(); File file2 = File.createTempFile("post", ".txt"); new FileWriter(file2).append(" world").close(); int code = post(url).send(file1).send(file2).code(); assertEquals(HTTP_OK, code); assertEquals("hello world", body.get()); }
/** * Make a POST request with a non-empty request body * * @throws Exception */ @Test public void postNonEmptyByteArray() throws Exception { final AtomicReference<String> body = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { body.set(new String(read())); response.setStatus(HTTP_OK); } }; byte[] bytes = "hello".getBytes(CHARSET_UTF8); int code = post(url).contentLength(Integer.toString(bytes.length)) .send(bytes).code(); assertEquals(HTTP_OK, code); assertEquals("hello", body.get()); }
/** * Make a post of form data * * @throws Exception */ @Test public void postFormWithNoCharset() throws Exception { final AtomicReference<String> body = new AtomicReference<String>(); final AtomicReference<String> contentType = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { body.set(new String(read())); contentType.set(request.getContentType()); response.setStatus(HTTP_OK); } }; Map<String, String> data = new LinkedHashMap<String, String>(); data.put("name", "user"); data.put("number", "100"); int code = post(url).form(data, null).form("zip", "12345").code(); assertEquals(HTTP_OK, code); assertEquals("name=user&number=100&zip=12345", body.get()); assertEquals("application/x-www-form-urlencoded", contentType.get()); }
/** * Make a post with an empty form data map * * @throws Exception */ @Test public void postEmptyForm() throws Exception { final AtomicReference<String> body = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { body.set(new String(read())); response.setStatus(HTTP_OK); } }; int code = post(url).form(new HashMap<String, String>()).code(); assertEquals(HTTP_OK, code); assertEquals("", body.get()); }
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { System.out.println("New Voterequest!"); Object link = (String) httpServletRequest.getParameter("link"); int vote = Integer.parseInt(httpServletRequest.getParameter("vote")); System.out.println(link); System.out.println(vote); httpServletResponse.setContentType("application/json; charset=UTF-8"); PrintWriter printout = httpServletResponse.getWriter(); JSONObject JObject = new JSONObject(); JObject.put("ok", "300"); printout.print(JObject); printout.flush(); }
/** * Make a GET request for a non-empty response body * * @throws Exception */ @Test public void getNonEmptyString() throws Exception { handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); write("hello"); } }; HttpRequest request = get(url); assertEquals(HTTP_OK, request.code()); assertEquals("hello", request.body()); assertEquals("hello".getBytes().length, request.contentLength()); assertFalse(request.isBodyEmpty()); }
/** * Make a GET request with basic proxy authentication specified * * @throws Exception */ @Test public void basicProxyAuthentication() throws Exception { final AtomicBoolean finalHostReached = new AtomicBoolean(false); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { finalHostReached.set(true); response.setStatus(HTTP_OK); } }; assertTrue(get(url).useProxy("localhost", proxyPort).proxyBasic("user", "p4ssw0rd").ok()); assertEquals("user", proxyUser.get()); assertEquals("p4ssw0rd", proxyPassword.get()); assertEquals(true, finalHostReached.get()); assertEquals(1, proxyHitCount.get()); }
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException { /* * Writing the response, for the stats request over netty */ System.out.println("New Statrequest!"); response.setContentType("application/json; charset=UTF-8"); PrintWriter printout = response.getWriter(); JSONObject JObject = new JSONObject(); JObject.put("down", "1000"); JObject.put("up", "300"); printout.print(JObject); printout.flush(); }
/** * Verify PUT with query parameters * * @throws Exception */ @Test public void putWithMappedQueryParams() throws Exception { Map<String, String> inputParams = new HashMap<String, String>(); inputParams.put("name", "user"); inputParams.put("number", "100"); final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = put(url, inputParams, false); assertTrue(request.ok()); assertEquals("PUT", method.get()); assertEquals("user", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
/** * Make a GET and get response as a buffered reader * * @throws Exception */ @Test public void getBufferedReader() throws Exception { handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); write("hello"); } }; HttpRequest request = get(url); assertTrue(request.ok()); BufferedReader reader = request.bufferedReader(); assertEquals("hello", reader.readLine()); reader.close(); }
/** * Make a GET and get response as a input stream reader * * @throws Exception */ @Test public void getReaderWithCharset() throws Exception { handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); write("hello"); } }; HttpRequest request = get(url); assertTrue(request.ok()); BufferedReader reader = new BufferedReader(request.reader(CHARSET_UTF8)); assertEquals("hello", reader.readLine()); reader.close(); }
/** * Determines if a request is permitted to be executed. The server may require authentication * and the login mechanism might have failed. This check verifies that only authenticated * users are permitted through when the server is requiring authentication. When a user * is disallowed, a status code and response will be automatically written to the provided * <code>response</code> and the caller should return immediately. * * @param serverConfig The server's configuration * @param request The user's request * @param response The response to the user's request * @return True if request can proceed, false otherwise. */ public boolean isUserPermitted(AvaticaServerConfiguration serverConfig, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException { // Make sure that we drop any unauthenticated users out first. if (null != serverConfig) { if (AuthenticationType.SPNEGO == serverConfig.getAuthenticationType()) { String remoteUser = request.getRemoteUser(); if (null == remoteUser) { response.setStatus(HttpURLConnection.HTTP_UNAUTHORIZED); response.getOutputStream().write(UNAUTHORIZED_ERROR.serialize().toByteArray()); baseRequest.setHandled(true); return false; } } } return true; }
/** * Verify HEAD with escaped query parameters * * @throws Exception */ @Test public void headWithEscapedMappedQueryParams() throws Exception { Map<String, String> inputParams = new HashMap<String, String>(); inputParams.put("name", "us er"); inputParams.put("number", "100"); final Map<String, String> outputParams = new HashMap<String, String>(); final AtomicReference<String> method = new AtomicReference<String>(); handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { method.set(request.getMethod()); outputParams.put("name", request.getParameter("name")); outputParams.put("number", request.getParameter("number")); response.setStatus(HTTP_OK); } }; HttpRequest request = head(url, inputParams, true); assertTrue(request.ok()); assertEquals("HEAD", method.get()); assertEquals("us er", outputParams.get("name")); assertEquals("100", outputParams.get("number")); }
/** * Get header parameter values * * @throws Exception */ @Test public void getMixQuotedParameters() throws Exception { handler = new RequestHandler() { @Override public void handle(Request request, HttpServletResponse response) { response.setStatus(HTTP_OK); response.setHeader("a", "value; b=c; d=\"e\""); } }; HttpRequest request = get(url); assertTrue(request.ok()); Map<String, String> params = request.parameters("a"); assertNotNull(params); assertEquals(2, params.size()); assertEquals("c", params.get("b")); assertEquals("e", params.get("d")); }